Go Down

Topic: Serial communication with php (Read 4 times) previous topic - next topic


Hi Folks, not sure if I'm posting in the correct location but couldn't find a more suitable section.  Anyways, I'm having problems on MAC OS snow leopard accessing the serial port that my adruino is connected to.  I know it's sending data as I've installed 'Serial Tools' and can see some stuff sent back.  I can also type the following in a terminal 'screen /dev/tty.usbmodem411 9600' and also get data.  However, in my simple php script which just calls fopen("tty.usbmodem411", rb), it appears that the connection is never made- some sort of infinite loop.  Am wondering if php is blocking and if there's someway to change the behavior.  Couldn't find anything in the documentation.  Also wrote a simple C function to do the same thing but it doesn't work either, also using fopen.  Any other function calls that could give me access so I can read the incoming data?  Thanks!

M. Schmitt

Serial communication doesn't work with this naive approach. (I've been there myself, on Perl.) There are serial libraries for most languages that help you implement serial communication.


Thanks for the heads up, since the php file operations are all based on the equivalent C functions I'm assuming I need to go a little bit deeper in.  Looks like I may need to use the dio or Direct IO Functions with PHP, will give it a try tonight.


DIO (latest beta version) works well for me with Arduino, not using Mac however, YMMV.


I just started messing with PHP dio myself a day or two ago, and I've gotten it working against the Arduino serial port (only for reading so far). And I am on a Mac (OS X 10.5).

However, I'm a little perplexed by my inability to save a file descriptor resource (returned by dio_open()) in a PHP session variable in a way that it can be used in a subsequent PHP invocation. What I'd like to do is essentially this:

Code: [Select]
(); /* enable session vars */
if (isset($_GET['action'])) {
$action $_GET['action'];
  switch (
$action) {
/* open the tty, configure the IO */
$fd dio_open('/dev/tty.usbserial-A6008mSD',
    if (!
$fd) {
/* handle the failure to open the file */
} else {
dio_tcsetattr($fd, array('baud' => 9600,
'bits' => 8,
'stop' => 1,
'parity' => 0));
/* save the file descriptor in a session var */
$_SESSION['fd'] = $fd;
$s dio_read($fd100);
$fd $_SESSION['fd'];
$s dio_read($fd100);
/* other cases */

The initial dio_read(), immediately after the dio_open() in the 'start' branch, works just fine. But when the script executes again and follows the 'run' branch, PHP complains about dio_read() needing a resource as its first argument, but that it's been given an integer instead. (I checked, and the value that I pull out of $_SESSION['fd'] in the 'run' branch is in fact 0.)

So I had to modify my logic to open, configure, read, and close the file each time the script runs -- which is not only ugly, but also seems like a good way to lose data that arrived on the serial port between script invocations.

Anyone seen this before, and know a way to work around it? (Or see something obvious that I'm doing wrong?)



Go Up