php serial communication in linux

I have installed xampp in fedora 13.I am trying to communicate with microcontroller through serial port using php serial class.
My code is example.php(find the attachment).On opening example.php through firefox the code is being displayed again as shown

eviceSet("0"); $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none"); //Device does not support flow control of any kind, so set it to none. //Now we "open" the serial port so We can write to it $serial->deviceOpen(); $serial->sendMessage("*1" ); //sleep(1); // echo "hi"; $serial->deviceClose(); ?>

as u can see the php code is not getting executed. most of it is displayed again in the browser as shown.
The commands are not being sent to the microcontroller .
Please help me
And is there any other way to communicate to serial port other than php class . Can you please suggest any alternative method ?
thank you

php_serial.class.php (13.6 KB)

example1.php (678 Bytes)

is there any other way to communicate to serial port other thanphp class

Sure. Plenty of other choices. Although, in this case, I think you will learn a lot about the Arduino and serial communication if you work through the problem, whatever it is, that you are having.

What is the problem?

The commands are not reaching the microcontroller . Can you please give me the code in alternate methods ? thanks for the fast reply

Opening the serial port resets that Arduino, unless you have modified the Arduino to not reset when the serial port is opened. Have you?

As it is now, you open some serial port. Without opening a bunch of files, I can't see which one. Nor do I have any clue if that is the right one. As soon as the port is opened, while the Arduino is resetting, you send two characters and close the serial port, resetting the Arduino again.

No matter which language you use to open the serial port, the Arduino will reset. No matter what language you use to close the serial port, the Arduino will reset. Unless you modify it not to.

The key, then, is to open the port, wait while the Arduino resets, and then send it data. When you are done with the Arduino, close the serial port.

:-)))))

If I understand you right, you just found a php script for serial communication and opened it with your webbrowser. If so, here are some hints that may help you:

  • a php script can basically well be executed without browser. Remove the starting "<?php" and the ending "?>", than it may already work. (by executing it with a php interpreter)

  • if you want to run an embedded php script, you cannot just open it in a browser from your local drive. It must be requested via a webserver, and the webserver, if configured properly, will do the php execution for you. But this is surely not what you are looking for, as it is unnecessary complicated.

hey thanks a lot guys ..sorry to bother you again .

i was opening the file directly from browser from the location htdocs ...i changed the file location to htdocs/xampp and it has started executing but there are some warnings as below :

Warning: Specified serial port is not valid in /opt/lampp/htdocs/xampp/php_serial.class.php on line 147
Warning: Unable to set the baud rate : the device is either not set or opened in /opt/lampp/htdocs/xampp/php_serial.class.php on line 241
Warning: Unable to set parity : the device is either not set or opened in /opt/lampp/htdocs/xampp/php_serial.class.php on line 295

... I have used the command : chmod 0777 /dev/ttyUSB0 to give permissions . I have also tried to add the apache user "prudhvi" to the dialout group by using command : $ usermod -a -G dialout prudhvi .

But it doesnt work . When I send a command directly from the terminal using the command : echo 1 > /dev/ttyUSB0 it works and '1' is transmitted to the serial port . But using php I get the above warnings .
I have used the "$whoami" to check name of user and added that user "prudhvi" to the dialout group . It still doesnt work . Please help me guys.

prudhvi09:
When I send a command directly from the terminal using the command : echo 1 > /dev/ttyUSB0 it works and '1' is transmitted to the serial port .

This is most helpful information. So it basically works already.

I guess, the problem is, that your php script is just using something else but /dev/ttyUSB0. It is a dams thing, that error output always misses to print out important informations. Extend your error message to print out the name of the port it is using. Then you should see the problem right away.