Linux php serial communication

I installed xampp in fedora 13. I am trying to communicate with the microcontroller via the serial port using the php serial class. My code example.php

include("php_serial.class.php"); $serial = new phpSerial(); $serial->deviceSet("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(); ?> 

The PHP script runs, but gives the following warnings.

Caution: the specified serial port is not valid in / opt / lampp / htdocs / xampp / php _serial.class.php on line 147 Warning. Cannot set baud rate: device is either not installed or open in / opt / lampp / htdocs / xampp / php _serial.class.php on line 241 Warning: parity cannot be set: device is either not installed or open in / opt / lampp / htdocs / xampp / php _serial.class.php on line 295

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

But that does not work. When I send a command directly from the terminal using the command: echo 1> / dev / ttyUSB0, it works, and "1" is sent to the serial port. But using php, I get the above warnings.

I used "$ whoami" to verify the username and added this user "prudhvi" to the dialout group. It is still not working. Please help me guys.

+8
linux php serial-communication
source share
4 answers

I did this once with Debian to manage an Arduino board using a PHP script and first encountered the same problem.

In Debian, you need to add the Apache user to the dialout group to allow him to make serial connection requests. I would suggest that this is true for Fedora.

In Debian, the command:

 useradd -G dialout www-data 

However, I believe that Fedora uses Apache instead of Apache. I do not have a Fedora machine for testing, but I would suggest that you need to run the command:

 useradd -G dialout apache 

Then you need to restart the xampp server.

For reference, see the following:

http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/ http://fedoraproject.org/wiki/Administration_Guide_Draft/Apache#Apache_File_Security

Nile

+3
source share

Could you place the lines next to / opt / lampp / htdocs / xampp / php _serial.class.php on line 147?

I suspect that you are trying to install the device incorrectly (as Mark pointed out). Either this or the port is already being used from another test that you are doing at the same time. I'm not sure that when you run the script, errors specific to the ports that you are trying to connect to the ones that are already used are displayed.

+2
source share

First check the welcome world of the php script to check your basic installation.

Then make sure that the / php web server engine works as a user who is in a group that is allowed access to the corresponding device file / dev / ttyWHATEVER corresponding to the serial port. It would be surprising if this were the default β€” you might have to add it to a β€œdialout” or similar group.

Add error / reporting to your code.

0
source share

The credit goes to Marc B's comment for making me watch this and he died: http://www.phpclasses.org/browse/file/17926.html

 function deviceSet ($device) { if ($this->_dState !== SERIAL_DEVICE_OPENED) { if ($this->_os === "linux") { if (preg_match("@^COM(\d+):?$@i", $device, $matches)) { $device = "/dev/ttyS" . ($matches[1] - 1); } if ($this->_exec("stty -F " . $device) === 0) { $this->_device = $device; $this->_dState = SERIAL_DEVICE_SET; return true; } } elseif ($this->_os === "windows") { if (preg_match("@^COM(\d+):?$@i", $device, $matches) and $this->_exec(exec("mode " . $device)) === 0) { $this->_windevice = "COM" . $matches[1]; $this->_device = "\\.\com" . $matches[1]; $this->_dState = SERIAL_DEVICE_SET; return true; } } trigger_error("Specified serial port is not valid", E_USER_WARNING); return false; } else { trigger_error("You must close your device before to set an other one", E_USER_WARNING); return false; } } 

I believe that calling $serial->deviceSet("/dev/ttyUSB0"); will fix it, but you may have to change the php_serial.class.php source to work with /dev/ttyUSB instead of /dev/ttyS .

0
source share

All Articles