public function set_options($options) { if (is_array($options)) { // Check for valid DIO attribute options foreach ($options as $key => $value) { // Validate option name if (!array_key_exists($key, self::$OPTIONS)) { trigger_error("Invalid PHP Direct IO option specified '" . $key . "'", E_USER_WARNING); return false; } // Validate option value if (!in_array($value, self::$OPTIONS[$key])) { trigger_error("Invalid PHP Direct IO value specified for " . $key . " '" . $value . "'", E_USER_WARNING); return false; } } // If all checks pass, set options $this->options = $options; dio_tcsetattr($this->serial, $options); return true; } return false; }
echoFlush("PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php"); exit; } try { //the serial port resource $bbSerialPort; echoFlush("Connecting to serial port: {$portName}"); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $bbSerialPort = dio_open($portName, O_RDWR); //we're on windows configure com port from command line exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on"); } else { $bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK); dio_fcntl($bbSerialPort, F_SETFL, O_SYNC); //we're on 'nix configure com from php direct io function 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 = "4 "; echoFlush("Writing to serial port data: \"{$dataToSend}\""); $bytesSent = dio_write($bbSerialPort, $dataToSend); echoFlush("Sent: {$bytesSent} bytes"); /* //date_default_timezone_set ("Europe/London"); $runForSeconds = new DateInterval("PT10S"); //10 seconds $endTime = (new DateTime())->add($runForSeconds);
<?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; } }