简体   繁体   English

从PHP将十六进制写入串行端口

[英]Write hex to serial port from PHP

I'm using a Raspberry PI (Jessi) to send a string of hex characters to a device using the serial port (USB to serial port). 我正在使用Raspberry PI(Jessi)通过串行端口(USB到串行端口)将十六进制字符字符串发送到设备。 I use the following command successfully from the command line: 我从命令行成功使用了以下命令:

echo -en "\x63\x69\x72\x62\x0d" > /dev/ttyUSB0

I want to do the same thing using PHP on the same Raspberry Pi but when I try to send the same command from a PHP file I do not get the desired result. 我想在同一Raspberry Pi上使用PHP做同样的事情,但是当我尝试从一个PHP文件发送相同的命令时,却没有得到想要的结果。 Example PHP code: 示例PHP代码:

var_dump( shell_exec('echo -en "\x63\x69\x72\x62\x0d" > /dev/ttyUSB0') );

I have tried a lot of things to get this working: 我已经尝试了很多方法来使其工作:

  • I have added www-data (PHP user) to group dialout (group for /dev/ttyUSB0) 我已将www-data(PHP用户)添加到组拨出(/ dev / ttyUSB0的组)
  • I have set chmod 777 for /dev/ttyUSB0 我已经将chmod 777设置为/ dev / ttyUSB0
  • I have verified that PHP can write to /dev/ttyUSB0 (is_writable()) 我已经验证PHP可以写入/ dev / ttyUSB0(is_writable())
  • I have tried a lot of different stty settings (ex: stty -F /dev/ttyUSB0 -isig -icanon) 我尝试了很多不同的stty设置(例如:stty -F / dev / ttyUSB0 -isig -icanon)
  • I have tried different ways in PHP to write to the serial port (PhpSerial library, pack() and fwrite(), printf() etc) Among other things. 我尝试过用PHP中的其他方式写入串行端口(PhpSerial库,pack()和fwrite(),printf()等)。

I have not found a way to see exactly what PHP is writing to /dev/ttyUSB0 (screen, minicom) but I know it does something since after I have tried the PHP script the next time I try from the command line it does not work. 我还没有找到一种方法来确切了解PHP正在写入/ dev / ttyUSB0(屏幕,minicom),但是我知道它可以执行某些操作,因为在我下次尝试从命令行尝试PHP脚本后,它不起作用。 The second time, after trying from PHP, the try from the command line is successful. 第二次,从PHP尝试之后,从命令行尝试成功。

I have tried running the PHP file from command line (CLI, both PHP5 and PHP7) and using the web server but nothing works. 我尝试从命令行(CLI,PHP5和PHP7)运行PHP文件并使用Web服务器,但没有任何效果。

I have tried all the things I can think of, and found online, but I can still not get it to work. 我已经尝试了所有我能想到的东西,并在网上找到了,但仍然无法使它正常工作。

What could cause this problem? 是什么导致此问题? The command works, I just want to run it using PHP, but it fails to do so. 该命令有效,我只想使用PHP运行它,但是没有成功。 Other command I try, using PHP, works - for example: 我尝试使用PHP的其他命令有效-例如:

var_dump( shell_exec('whoami') );

Update #1 I created a bash script file with the working command and also added an echo "ok" to get some output back. 更新#1我使用工作命令创建了一个bash脚本文件,并且还添加了回显“ ok”以获取一些输出。 The file works when running it from the command line but when I try to run it from PHP I get the "ok" back but it still fail to effect the connected device. 从命令行运行该文件时,该文件有效,但是当我尝试从PHP运行该文件时,我又获得了“ ok”,但仍然无法影响连接的设备。 I tried to run the bash script from the command line as the www-data user and that works, but still nothing I do using PHP works: 我试图以www-data用户的身份从命令行运行bash脚本,并且该脚本可以运行,但是仍然无法使用PHP进行操作:

sudo -u www-data bash -c '/home/test/cmd'

Update #2 Tried the same thing on another computer, running Kodi, with the same negative result. 更新#2在另一台运行Kodi的计算机上尝试相同的操作,但结果相同。 The command works but not when run using PHP. 该命令有效,但在使用PHP运行时无效。

I ran into a similar problem. 我遇到了类似的问题。 What I eventually figured out was that user httpd (I'm running CentOS -- the apache user might be different on your flavor of Linux) didn't have write permissions to /dev/ttyS0. 我最终弄清楚的是,用户httpd(我正在运行CentOS - apache用户在您的Linux风格上可能有所不同)没有对/ dev / ttyS0的写入权限。

// Grants write permission on /dev/ttyS0 to all users //向所有用户授予对/ dev / ttyS0的写权限

sudo chmod a+w /dev/ttyS0 须藤chmod a + w / dev / ttyS0

It'd be a bit more elegant to figure out how to grant write permission only to the httpd user instead of all users on the machine. 弄清楚如何只向httpd用户而不是计算机上的所有用户授予写许可权,这会稍微优雅一点。

Hope this helps you out! 希望这可以帮助你!

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

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