简体   繁体   English

Linux中的PHP串行通讯

[英]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. 我已经在fedora 13中安装了xampp,我正在尝试使用php串行类通过串行端口与微控制器进行通信。 My code is example.php 我的代码是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 . php脚本得到执行,但给出以下警告。

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 警告:在第147行的/opt/lampp/htdocs/xampp/php_serial.class.php中指定的串行端口无效警告:无法设置波特率:在/ opt / lampp / htdocs中未设置或打开设备/第241行上的/xampp/php_serial.class.php警告:无法设置奇偶校验:第295行上的/opt/lampp/htdocs/xampp/php_serial.class.php中未设置或打开设备

... I have used the command : chmod 0777 /dev/ttyUSB0 to give permissions . ...我已经使用命令:chmod 0777 / dev / ttyUSB0授予权限。 I have also tried to add the apache user "prudhvi" to the dialout group by using command : $ usermod -a -G dialout prudhvi 我还尝试通过使用以下命令将apache用户“ prudhvi”添加到拨出组:$ 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 . 当我使用以下命令直接从终端发送命令:echo 1> / dev / ttyUSB0时,它将起作用,并且'1'传输到串行端口。 But using php I get the above warnings . 但是使用php时,我得到了以上警告。

I have used the "$whoami" to check name of user and added that user "prudhvi" to the dialout group . 我已经使用“ $ whoami”检查用户名,并将该用户“ prudhvi”添加到了拨出组。 It still doesnt work . 它仍然不起作用。 Please help me guys. 请帮助我。

I did this once with Debian to control an Arduino board with a PHP script and initially ran into the same problem. 我曾经与Debian一起使用PHP脚本控制Arduino板,但最初遇到了同样的问题。

In Debian, you need to add the Apache user to the dialout group in order to allow it to make serial connection requests. 在Debian中,您需要将Apache用户添加到拨出组中,以允许它发出串行连接请求。 I would assume the same is true for Fedora. 我认为Fedora也是如此。

In Debian the command is: 在Debian中,命令为:

useradd -G dialout www-data

However I believe Fedora names the Apache user as apache instead. 但是我相信Fedora将Apache用户命名为apache。 I don't have a Fedora machine to test on, but I would assume the command you need to run is: 我没有要测试的Fedora机器,但是我认为您需要运行的命令是:

useradd -G dialout apache

You will then need to restart your xampp server. 然后,您将需要重新启动xampp服务器。

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 http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/ http://fedoraproject.org/wiki/Administration_Guide_Draft/Apache#Apache_File_Security

Neal 尼尔

Could you post the lines near / related to "/opt/lampp/htdocs/xampp/php_serial.class.php on line 147"? 您能否在“第147行上的/opt/lampp/htdocs/xampp/php_serial.class.php”附近发布相关行?

I suspect that you are trying to set the device incorrectly (as Marc indicated). 我怀疑您试图错误地设置设备(如Marc所示)。 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. 我不确定您正在运行的脚本是否提供了特定于您尝试附加到正在使用的端口的错误。

First test a hello world type php script to testyour basic installation. 首先测试您好世界类型的php脚本以测试您的基本安装。

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. 然后,验证Web服务器/ PHP引擎是否以用户身份运行,该用户位于允许访问与串行端口相对应的适用的/ dev / ttyWHATEVER设备文件的组中。 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. 在代码中添加一些故障检查/报告。

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 马克·马克·B(Marc B)的评论引起了我的注意,他因此死了: 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"); 我相信调用$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 . 可以修复它,但是您可能必须修改php_serial.class.php的源php_serial.class.php才能在/dev/ttyUSB而不是/dev/ttyS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM