linux下的php串口通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10373308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:04:29  来源:igfitidea点击:

php serial communication in linux

phplinuxserial-communication

提问by prudhvi09

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

我已经在 Fedora 13 中安装了 xampp。我正在尝试使用 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 中设置或打开/xampp/php_serial.class.php 第 241 行警告:无法设置奇偶校验:设备未在第 295 行的 /opt/lampp/htdocs/xampp/php_serial.class.php 中设置或打开

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

...我已使用命令:chmod 0777 /dev/ttyUSB0 授予权限。我还尝试使用以下命令将 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 . But using php I get the above warnings .

但它不起作用。当我使用以下命令直接从终端发送命令时: echo 1 > /dev/ttyUSB0 它可以工作并且“1”被传输到串行端口。但是使用 php 我得到了上述警告。

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.

我使用“$whoami”来检查用户名并将该用户“prudhvi”添加到拨号组。它仍然不起作用。请帮帮我。

回答by Chris Stratton

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

首先测试一个 hello world 类型的 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. It would be surprising if that were true by default - you'll probably have to add it to the 'dialout' or similar group.

然后验证 web 服务器/php 引擎是否以允许访问与串行端口对应的适用 /dev/ttyWHATEVER 设备文件的组中的用户身份运行。如果默认情况下这是真的,那将是令人惊讶的 - 您可能必须将其添加到“拨出”或类似组中。

Add some fault checking / reporting to your code.

向您的代码添加一些错误检查/报告。

回答by Pilipo

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

您能否发布与“/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.

我怀疑您试图错误地设置设备(如 Marc 所示)。或者该端口已经从您同时进行的其他测试中使用。我不确定您正在运行的脚本是否提供了特定于您尝试附加到已在使用的端口的错误。

回答by nealio82

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. I would assume the same is true for Fedora.

在 Debian 中,您需要将 Apache 用户添加到 dialout 组中,以允许其发出串行连接请求。我认为 Fedora 也是如此。

In Debian the command is:

在 Debian 中,命令是:

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:

但是我相信 Fedora 将 Apache 用户命名为 apache。我没有要测试的 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

尼尔

回答by zebediah49

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

归功于 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");will fix it, but you may have to modify the source of php_serial.class.phpto work on /dev/ttyUSBinstead of /dev/ttyS.

我相信,通话$serial->deviceSet("/dev/ttyUSB0");将修复它,但你可能需要修改源php_serial.class.php工作就/dev/ttyUSB不是/dev/ttyS