PHP Serial Port Data Return from Arduino

PHP serial port data return from Arduino

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/.

Serial Communication Arduino to PHP

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



Related Topics



Leave a reply



Submit