Serial Communication Arduino to PHP

2019-08-02 18:57发布

问题:

I´m working on a project in which I want to send sensor data from Arduino via Serial Communication to PHP.

Unfortunately I can not read the Serial Port in PHP. However the other direction (PHP to Arduino) works perfectly. I´m using the php_serial.class.php from Rémy Sanchez, modified by Rizwan Kassim. I´m dependant from the readPort() - function.

I´m working with an Arduino UNO and Apache WAMP-Server on Mac OS X. I should realise the serial connection without Ethernet shield. In further steps I have to save the received data from the serial port in a MySql database. I have indeed seen a couple of entries covering this issue, but they don´t really help me. I have tried so much. I´m sure, someone has some working codes and can upload them. What am I doing wrong? I hope someone will post the code or has a tip!

By the way, here is a video from a guy who succeeded. But I can´t get the codes.... Thanks in advance for your advice!

Greets Fab

回答1:

the php_serial.class.php is kind of broken, i had to adapt it to get a reading out of it, so instead of using the following from the reader method: $content = ""; $i = 0;

                    if ($count !== 0)
                    {
                            do {
                                    if ($i > $count) $content .= fread($this->_dHandle, ($count - $i));
                                    else $content .= fread($this->_dHandle, 128);
                            } while (($i += 128) === strlen($content));
                    }
                    else
                    {
                            do {
                                    $content .= fread($this->_dHandle, 128);
                            } while (($i += 128) === strlen($content));
                    }

i used just this

     //trigger_error("reading 0 ".$i, E_USER_WARNING);
     $content .= fread($this->_dHandle, $count);


     return str_split($content);

then reconstruct the byte string in php