PHP serial port data return from Arduino

后端 未结 3 2019
谎友^
谎友^ 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/.

    0 讨论(0)
  • 2020-11-30 07:26

    You are probably trying to read when is no data on the serial port. You need to implement JavaScript code to call PHP code that can read at regular intervals.

    When you have got data, you should process it.

    readPort() has worked fine for me. If the baud rate, parity, etc. is adjusted properly, then you should not have any problem reading the serial port.

    Here is a sample of using the library for Arduino. It worked for me some time ago:

    <?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();
    ?>
    
    0 讨论(0)
  • /dev/ttyUSB0 running with user root, if your using apache try chown /dev/ttyUSB0 to apache or user is logged in.

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

    and then try again, or try logout from ubuntu and login as root user.

    0 讨论(0)
提交回复
热议问题