Go Down

Topic: Serial communication with PHP (Read 31719 times) previous topic - next topic

Coding Badly

Feb 03, 2010, 06:44 am Last Edit: Feb 03, 2010, 06:44 am by bcook Reason: 1

Have you tried opening the port in binary mode?

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

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.


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.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


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!


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


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.


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?



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?

Coding Badly

Does this help...



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)

Coding Badly

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.


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: [Select]

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




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.


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

sorry for my eng


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!


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!


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



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:


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


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?

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

Go Up