php serial communication in linux

喜你入骨 提交于 2019-12-04 00:55:28

问题


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

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 gets executed but gives the following warnings .

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.


回答1:


I did this once with Debian to control an Arduino board with a PHP script and initially ran into the same problem.

In Debian, you need to add the Apache user to the dialout group in order to allow it to make serial connection requests. I would assume the same is true for Fedora.

In Debian the command is:

useradd -G dialout www-data

However I believe Fedora names the Apache user as apache instead. I don't have a Fedora machine to test on, but I would assume the command you need to run is:

useradd -G dialout apache

You will then need to restart your xampp server.

See the following for reference:

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

Neal




回答2:


Could you post the lines near / related to "/opt/lampp/htdocs/xampp/php_serial.class.php on line 147"?

I suspect that you are trying to set the device incorrectly (as Marc indicated). Either that or the port is already in use from other testing you are conducting at the same time. I'm not sure if the script you are running provides errors specific to ports you are trying to attach to already being in use.




回答3:


First test a hello world type php script to testyour basic installation.

Then verify the web server / php engine is running as a user which is in a group allowed to access the applicable /dev/ttyWHATEVER device file corresponding to the serial port. It would be surprising if that were true by default - you'll probably have to add it to the 'dialout' or similar group.

Add some fault checking / reporting to your code.




回答4:


Credit goes to Marc B's comment for causing me to look this up, and he's dead on: 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 modify the source of php_serial.class.php to work on /dev/ttyUSB instead of /dev/ttyS.



来源:https://stackoverflow.com/questions/10373308/php-serial-communication-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!