<?php $fd = dio_open('/dev/ttyS0', O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array('baud' => 9600, 'bits' => 8, 'stop' => 1, 'parity' => 0)); while (1) { $data = dio_read($fd, 256); if ($data) { echo $data; } }
case SIGINT: global $running; $running = false; } } $fd = dio_open('/dev/ttyAMA0', O_RDONLY | O_NOCTTY); # | O_NONBLOCK); dio_fcntl($fd, F_SETFL, O_SYNC); dio_tcsetattr($fd, array('baud' => 19200, 'bits' => 8, 'stop' => 1, 'parity' => 0)); $stats = array(); $requests = array(); $answers = array(); $running = true; $buf = ""; while ($running) { $data = dio_read($fd); if (substr(bin2hex($data), 0, 4) == "0000") { $add = ""; if (substr(bin2hex($buf), 0, 4) == "0000" and substr(bin2hex($buf), 0, 6) != "000000") { $add = "00"; } $c = $add . bin2hex($buf); if (!isset($stats[$c])) { $stats[$c] = 0; } $stats[$c]++; file_put_contents("read.log", $c . "\n", FILE_APPEND); $buf = $data; } else { $buf .= $data; }
dio_tcsetattr($bbSerialPort, array('baud' => $baudRate, 'bits' => $bits, 'stop' => $spotBit, 'parity' => 0)); } if (!$bbSerialPort) { echoFlush("Could not open Serial port {$portName} "); exit; } // send data $dataToSend = $command; echoFlush("Writing to serial port data: \"{$dataToSend}\""); $bytesSent = dio_write($bbSerialPort, $dataToSend); echoFlush("Sent: {$bytesSent} bytes"); //dio_write($bbSerialPort, "111111111111" ); //date_default_timezone_set ("Europe/London"); $runForSeconds = new DateInterval("PT10S"); //10 seconds $endTime = (new DateTime())->add($runForSeconds); echoFlush("Waiting for {$runForSeconds->format('%S')} seconds to recieve data on serial port"); while (new DateTime() < $endTime) { dio_write($bbSerialPort, $command); $data = dio_read($bbSerialPort, 10); //this is a blocking call if ($data) { echoFlush("Data Recieved: " . $data); } } echoFlush("Closing Port"); dio_close($bbSerialPort); } catch (Exception $e) { echoFlush($e->getMessage()); exit(1); }
public function read($length = self::DEFAULT_LENGTH) { $bytes = dio_read($this->serial, $length); return $bytes; }
function callAT($cmd, $modem, $log = false, $end = 0xd) { $cmd = trim($cmd); $cmd = $cmd . chr($end); if ($log) { webLog($cmd, ">", true); } dio_write($modem, $cmd); //sleep(1); usleep(500000); $cmd = dio_read($modem); $cmd = trim($cmd); if ($log) { webLog($cmd, "<"); } return $cmd; }