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!

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.

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:

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?)

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.

The "file" you are referring to appears to be the serial port. Opening and closing the serial port resets the Arduino, so keeping the handle is essential, so you can keep the "file" open.

Quote

I checked, and the value that I pull out of $_SESSION['fd'] in the 'run' branch is in fact 0.

This sounds like you are not persisting session data correctly, then. It's been a year or more since I last used PHP, so I don't remember exactly what you need to do to make this happen, but I think it involves correctly configuring PHP and adding some code to the PHP script.

Opening and closing the serial port resets the Arduino, so keeping the handle is essential

Thanks for mentioning that, I wasn't really aware of it before. Of course, it means that finding a solution to this problem is therefore also essential.

Quote

it might be best to check your session stuff separately from the dio.

Yes, that had occurred to me before I saw your post, so I constructed a simple test to store and retrieve any value that I can pass in a $_GET[] variable, and it works just as I would expect. Invoking your suggested test script also reports "Found 61", as expected.

I wonder if there's any additional configuration I might need to do on my PHP installation to allow it to store more "exotic" datatypes like FD resources...

Edit: Or maybe I need to serialize the FD resource before squirreling it away in the session variable, and deserialize it after retrieving it? (No time this morning to construct a test for this. And in any event, I've always thought that serializing the values stored in $_SESSION[] was automatically performed by PHP...)

Hmm... I may be screwed. Or not -- at least not yet. I can't really tell, since the evidence I currently have is ambiguous.

I found the following in PHP's online documentation:

Quote

Note:It is currently impossible to register resource variables in a session. For example, you cannot create a connection to a database and store the connection id as a session variable and expect the connection to still be valid the next time the session is restored. PHP functions that return a resource are identified by having a return type of resource in their function definition. A list of functions that return resources are available in the resource types appendix.

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, assign values to $_SESSION. For example: $_SESSION['var'] = 'ABC';

Now, what's ambiguous about this is that I found it in the documentation for the session_register() function, which I'm not using. It looks as though the final sentence of this note may be suggesting that $_SESSION will work where session_register() would fail, and I don't see any similar caveat in the documentation for $_SESSION. On the other hand, I would imagine that the same underlying mechanism for persisting a value between script invocations would be used in either case; and the fact that using $_SESSION for storing/retrieving a simple value like 'ABC' works for me, and using it for storing/retrieving a dio FD resource doesn't, leads me to believe that it's just not possible.

@colbec: have you been successful in persisting a dio FD resource via $_SESSION on your system?

Could you please explain how phpSerial solves the problem any better than the dio extension does?

The phpSerial::deviceOpen() method uses fopen() to open the device (svn/trunk/phpSerial.php:177), and fopen() returns a resource. As colbec's most recent post clearly shows, $_SESSION can't be used to make the resource returned by fopen() persist across PHP script invocations.

After a couple of experiments and a bit more reflection, I'm persuaded that the inability to persist a resource (such as a file descriptor, DB connection, etc.) via the PHP session mechanism is due more to the way Apache (in my case) works than it is to a particular shortcoming of PHP.

So I've moved on to a different approach, using a small daemon process (written in Python) to open and maintain the connection with the Arduino, and a pair of FIFOs (named pipes) for communication between PHP and the daemon. It's a little clunky at the moment (mostly because I'm still re-learning a lot of stuff I forgot years ago!), but so far, so good.

If anyone's been following this thread and wants more detail on this daemon-based approach, let me know and I'll post the code once I've got it working the way I want...