Возврат данных последовательного порта PHP из Arduino

Интересно, есть ли способ выполнить чтение моего последовательного порта через PHP – это работает 🙂

Практикуя свои навыки Arduino, я разработал простой светодиодный экран ON / OFF. Он работает, введя или выключая последовательный монитор.

Следующим шагом я собрал веб-страницу, чтобы действовать как интерфейс GUI, чтобы щелкнуть ссылку и выполнить функцию включения и выключения выше. Этот веб-интерфейс GUI работает через PHP. Я использую класс PHP SERIAL для взаимодействия с последовательным портом, который использует Arduino.

Проблема в том, что мне нужно найти способ получения обратной связи от последовательного порта. Используя последовательный монитор Arduino IDE, я могу видеть свои печатные сообщения в ответ на каждый из моих последовательных входов, и мне нужно получить такую ​​же обратную связь в моем PHP-коде.

Класс PHP Serial предлагает функцию readPort (), но я не возвращаю свои данные.

UPDATE : [2]:

ARDUINO:

const int greenPin = 2; const int bluePin = 3; const int redPin = 4; int currentPin = 0; //current pin to be faded int brightness = 0; //current brightness level void setup(){ Serial.begin(9600); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(redPin, OUTPUT); } void loop(){ //if there's any serial data in the buffer, read a byte if( Serial.available() > 0 ){ int inByte = Serial.read(); //respond only to the values 'r', 'g', 'b', or '0' through '9' if(inByte == 'r') currentPin = redPin; if(inByte == 'g') currentPin = greenPin; if(inByte == 'b') currentPin = bluePin; if(inByte >= '0' && inByte <= '9'){ //map the incoming byte value to the range of the analogRead() command brightness = map(inByte, '0', '9', 0, 255); //set the current pin to the current brightness: analogWrite(currentPin, brightness); } Serial.print("Current Pin : "); Serial.println(currentPin); Serial.print("Brightness : "); Serial.println(brightness); }//close serial check } 

PHP / HTML:

 <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> для <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> - <?php require("php_serial.class.php"); // include("php_serial.class.php"); // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyACM0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); print "<pre>"; print_r($read); print "</pre>"; // Print out the data echo $read; // print exec("echo 'r9g9b9' > /dev/ttyACM0"); print "RESPONSE(1): {$read}<br><br>"; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> <?php if( isset($_REQUEST['LED']) ) response(); ?> <form action='index.php' method='POST'> <select id='led' name='LED'> <option id='nil'>-</option> <option id='red'>RED</option> <option id='green'>GREEN</option> <option id='blue'>BLUE</option> <option id='all'>ALL</option> </select> <input type='submit' value='SET'> </form> <?php print "Hi, Earthlings!"; function response(){ $CMDString = ""; $execute = false; if( isset($_REQUEST['LED']) ){ switch ($_REQUEST['LED']) { case 'RED': $CMDString = 'r9'; $execute = true; exec("echo 'r9g0b0' > /dev/ttyACM0"); print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'GREEN': $CMDString = 'g9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'BLUE': $CMDString = 'b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; case 'ALL': $CMDString = 'r9g9b9'; $execute = true; print "<br>FOUND: {$_REQUEST['LED']}"; break; default: print exec("echo 'r0g0b0' > /dev/ttyACM0"); $execute = false; break; } if($execute){ print exec("echo '{$CMDString}' > /dev/ttyACM0"); print "<br><br>executing: {$CMDString}"; } } } ?> 

    Предполагаю, что вы работаете в Linux.

    Сначала настройте свой последовательный порт:

     stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts 

    Затем вы можете использовать хороший старый fread / fwrite

     $fp =fopen("/dev/ttyACM0", "w+"); if( !$fp) { echo "Error";die(); } fwrite($fp, $_SERVER['argv'][1] . 0x00); echo fread($fp, 10); fclose($fp); 

    Есть только одна вещь, которую вы должны помнить. Arduino будет перезагружаться при каждом подключении . Если вы не знаете, что это смутит вас. Например, если вы подключаетесь (fopen) и мгновенно отправляете данные, Arduino пропустит его, потому что он загружается (что занимает секунду или два). Поэкспериментируйте со сном, чтобы дать ему некоторое время. Если вы хотите отключить перезапуск, используйте конденсатор 10uF от GRD до RST.

    Удачи

    пс. вы можете устранить неисправность с помощью «экрана»,

     screen /dev/ttyACM0 9600 

    Сообщение о настройке PHP с Arduino http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/ и еще один здесь http://systemsarchitect.net/arduino-and-php- serial-communication-with-a-protocol / .

    / dev / ttyUSB0 работает с пользователем root, если вы используете apache, попробуйте chown / dev / ttyUSB0 для apache или пользователь выполнит вход в систему.

     $ sudo chown apache2:apache2 /dev/ttyACM0 OR $ sudo chown yourusername:yourusername /dev/ttyACM0 

    а затем повторите попытку или попробуйте выйти из ubuntu и войти в систему как пользователь root.

    Вероятно, вы пытаетесь прочитать, когда нет данных о последовательном порту. Вам нужно реализовать код JavaScript для вызова PHP- кода, который может читаться через регулярные промежутки времени.

    Когда у вас есть данные, вы должны обработать их.

    readPort() отлично работает для меня. Если скорость передачи, четность и т. Д. Настроена правильно, то вам не должно быть проблем с чтением последовательного порта.

    Вот пример использования библиотеки для Arduino. Это сработало для меня некоторое время назад:

     <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> для <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?> - <?php include "php_serial.class.php"; // Let's start the class $serial = new phpSerial(); // First we must specify the device. This works on both Linux and Windows (if // your Linux serial device is /dev/ttyS0 for COM1, etc.) $serial->deviceSet("/dev/ttyUSB0"); // Set for 9600-8-N-1 (no flow control) $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); // Then we need to open it $serial->deviceOpen(); // Read data $read = $serial->readPort(); // Print out the data echo $read; // If you want to change the configuration, the device must be closed. $serial->deviceClose(); ?>