PHP serial port data return from Arduino

后端 未结 3 2023
谎友^
谎友^ 2020-11-30 06:51

I wonder if there is a way to accomplish reading my serial port via PHP - that works :-)

In practising my Arduino skills, I developed a simple LED ON/OFF sketch. It

3条回答
  •  时光说笑
    2020-11-30 07:25

    I assume you work on linux.

    First setup your serial port:

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

    Then you can use good old fashion fread/fwrite

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

    There is only one thing you have to remember. Arduino will restart on every connection. If you don't know that It will confuse you. For instance if you connect (fopen) and instantly send data Arduino will miss it because it's booting (which takes a sec or two). Experiment with sleep to give it some time. If you want to disable restarting use 10uF capacitor from GRD to RST.

    Good luck

    ps. you can troubleshoot with "screen"

    screen /dev/ttyACM0 9600
    

    Post about setting PHP with Arduino http://systemsarchitect.net/connecting-php-with-arduino-via-serial-port-on-linux/ and one more here http://systemsarchitect.net/arduino-and-php-serial-communication-with-a-protocol/.

提交回复
热议问题