Pages: 1 [2] 3   Go Down
Author Topic: Serial communication with PHP  (Read 17639 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Have you tried opening the port in binary mode?

The fwrite documentation is rather specific on the topic...

Quote
Note: On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.
« Last Edit: February 03, 2010, 12:44:55 am by bcook » Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd be very systamatic trouble shooting the problem.

1). Can you get the arduino to do what you want using hyperterminal (or similar terminal application talking to the serial port) and is the expected response from the arduino received by hyperterminal? If not, then the arduino and its programming might be an issue.

2). Is PHP opening the serial port at the appropriate baud rate for the arduino?

3). Test your PHP program by connecting together the tx/rx lines supplying the arduino, then send a string and see if the PHP program returns the string. If this doesn't work, then there is a PHP issue with sending/receiving data, and possibly an issue with its interaction with apache.  

4). If the issues can't be readily solved, then go ahead and set your webcam up the ol timey cgi way and work on the current issues as time permits.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all for the advice.  I'm at the office now, but when I get home this evening, I'll try out a few things and report back to you all with my findings.  Also, once I do finally figure this out, I'll make a detailed blog post explaining all my pitfalls, to save others in the future!

Thanks again!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pitfall number one might be "Using Windows".

I'm sorry - I just had to say; couldn't stop myself!  ;D

I guess its because I've been using *nix for so long, I've just gotten used to having things work (well, most of the time!).

Anyhow - I just want to offer up something on that portmon output; likely the "2" you are seeing has to do with the character you are sending ("a") plus another character (newline? null?) - however, that doesn't explain your issue...

You might try going old-school with an old computer running DOS (ack!) and QBASIC (double-ack!), so you can get direct port access, just to test your code on the Arduino; once you know that is working correctly, then you can concentrate on your Windows box; alternatively, you could do the same in *nix, and write directly to the USB port in your PHP script via fopen(). At least, once you know what is working, then you know what isn't (profound, I know).
Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I set up another sketch (it's not really important what it looks like) to test if hyperterminal would work sending serial data to the Arduino, and was successful.  I easily toggled my little LED on and off.

This leads me to believe that my PHP is funky.  I saw an earlier comment in this thread that I should try opening the serial connection for binary communication which I will try.  I'll also make sure to determine what baud rate PHP is sending data at so I can be sure that it's in synch w/ Arduino.  Man, I'm so close now I can taste it!

Thanks again all.
Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Still exploring options.

Another quick question:

Could I use an Ethernet Shield in some way to achieve the same results?  Remember basically I'd like to build an Arduino project that I can control via the web.  Since it seems that using PHP to send serial data is proving to be much more challenging than I initially thought, maybe a shield can save me?

Thoughts?

Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It seems like my Arduino might be resetting whenever PHP opens or closes the serial connection to my COM port.  Is there any way I can make it stop?  I've heard that you can add a resistor between the 5 or the 3.3 volt pins and the reset pin to make this a reality.  Any thoughts?
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this help...

http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection
Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh, I believe this has finally solved my issue!  I'm able now to suppress the reset when the PHP code opens the serial port and it seems to be working now.  Thank you all again for your help and advice.  I'm working on the code now to finalize the project.  I've got my servos in the mail this weekend, and by next weekend (time permitting) I'll have a viewable pan/tilt web cam for you all!

Thanks again.
(ps, I'll make a detailed writeup about the whole process so others can learn from my mistakes)
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 198
Posts: 12747
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'll make a detailed writeup about the whole process so others can learn from my mistakes
Thank you!  That would be very helpful to the community.  At this very moment there are two other folks having trouble getting PHP to work with an Arduino.

I have two requests.  Please...
  • Change the Arduino to a serial port above 9 and test.  I suspect PHP does not work support serial ports above 9.  It would be nice to know for certain.
  • When you do your writeup, include the version of PHP you are using.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good job with getting this to work with PHP!

I have one question though, have you tried not closing the file descriptor right after you write to it? Something like

Code:
<?PHP

$fp =fopen("com7", "w");
fwrite($fp, chr(a));

sleep(60);

fclose($fp);

?>

this way you would have time to read the input from the Arduino before it closes the port and resets. It would be fun to know if this way works but in your case, it isn't a viable solution because the php script to control the Arduino would be executed via the web.

In cases where PHP isn't used from the web (running from command line instead of over Apache) and your PHP script uses an infinite loop to receive / send data constantly to the Arduino, this solution could be possible I guess.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

soup can you post your code php and arduino??  I have your same problem..  thanks!! i need help!

sorry for my eng
Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure thing.  I'm actually almost done with my write-up on this project.  When it's all finished I'll post a link to my blog here so you can all check it out.  Thanks again to all who helped me make this a reality.  Can't wait to get started on my next project!

Logged

Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright all, here is the link to my complete writeup for this project.  Keep in mind that I'm even less talented in writing than I am in Arduino projects!

http://anealkhimani.com/2010/02/08/web-enabled-pantilt-webcam-with-arduino-and-php-part-1/

Enjoy!  And I hope this helps someone achieve that awesome project!
Aneal K.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

soup:

Something minor I noticed about your code; it obviously had nothing to do with your problem, but was something to keep in mind (for this small sketch, it really doesn't matter - but for larger projects, it might matter a lot)...

You declare the variable "inByte" for reading from the serial port as an integer...[stop]

soup - I am not asking the following of you, but if you know, please chime in...



Ok - I just went to the Serial.read() function definition on the Arduino site:

http://arduino.cc/en/Serial/Read

I just noticed that it reads:

"Returns...the first byte of incoming serial data available (or -1 if no data is available) - int"

Why isn't the return value of this function a byte, instead of an int? Aren't integers on the Arduino defined as 2 bytes long?:

http://arduino.cc/en/Reference/Int

So - why does Serial.read() return an int, and why doesn't the documentation match up? Also, what is the return value type for Serial.available() - it isn't mentioned in the documentation, either?

 :-?
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Pages: 1 [2] 3   Go Up
Jump to: