php serial communication problems

Hi! I'm facing with some serial communication problems. I want to update textfield data reading from serial port with php. But sometimes the textfields won't to update. This is my code:

error_reporting(E_ALL);

ini_set("display_errors", 1);

define("PORT","/dev/ttyUSB0");

if (isset($_GET['action'])) {

$action=$_GET['action'];

include "php_serial.class.php";

$serial = new phpSerial;

$serial->deviceSet(PORT);

$serial->confBaudRate(9600);

$serial->confParity("none");

$serial->confCharacterLength(8);

$serial->confStopBits(1);

$serial->confFlowControl("none");

$serial->deviceOpen();

$action = $_GET['action'];

if($_GET['action']{1}==0){

$serial->sendMessage($action);

usleep(10000);

echo ($read);

}else if($_GET['action']{1}!=0){

$serial->sendMessage($action);

}

$serial->deviceClose();

}

For example when I try to write and read from serial port, this sometimes doesn't work.

It’s a very long time since I dabbled with PHP but I wonder if your program opens and closes the serial port. Opening the serial port causes the Arduino to reset which takes a few seconds when it is not listening for anything as well as making the Arduino forget whatever it was doing. Your program should open the Serial port and keep it open until it is completely finished with the Arduino.

In my programs I put Serial.println(""); in setup() and my PC program waits until it receives that so it knows the Arduino has successfully restarted.

…R

but I wonder if your program opens and closes the serial port.

That's what $serial->deviceOpen() and $serial->deviceClose() do.

Your program should open the Serial port and keep it open until it is completely finished with the Arduino.

That will be very difficult, since the PHP script is executed each time the web browser refreshes.

OP: It would be far simpler to get an Ethernet shield for the Arduino, and have it serve up the page, without the need to use PHP at all.

PaulS: That will be very difficult, since the PHP script is executed each time the web browser refreshes.

I guess that's why I use Python (or, before that Ruby) rather than PHP.

Is it not possible to write a continuous background program in PHP that can receive values that the server receives?

...R

Is it not possible to write a continuous background program in PHP that can receive values that the server receives?

"The server" doesn't receive data. The server receives GET requests, which define which script to run. The script is run, and the results of executing the script are returned to the client.

It does not make sense to have the script try to interact with hardware over the serial port in order to get data needed to satisfy the request. The script should return data that it has, or obtains from databases, etc.

Some other process running on the hardware that the Arduino is connected to needs to be taking data from the Arduino and putting it in a place that the PHP script can access.

That other process COULD be a PHP script, but that is not what PHP is really for.

PaulS: That other process COULD be a PHP script, but that is not what PHP is really for.

Fair enough.

With my projects the Python (or Ruby) code is the server as well as the means to communicate with the Arduino.

...R

Robin2: Is it not possible to write a continuous background program in PHP that can receive values that the server receives?

php-fpm would usually be used to create persistent processes. Your web server would then use some kind of FastCGI connector (with Apache it's mod_proxy_fcgid) to communicate with that daemon.

All a moot point though because we don't know what the real error is.

Chagrin:
php-fpm would usually be used to create persistent processes …

All sounds very complicated compared to using Python with Bottle or similar.

…R

Robin2: All sounds very complicated compared to using Python with Bottle or similar.

..which all sounds very complicated compared to "pcntl_fork() && exit || posix_setsid();" if we want to ignore the details required for an intelligent web application implementation. There's always more to it than just "making a daemon".

You know the Irish saying "If I was going there, I wouldn't start from here" :)

...R