简体   繁体   中英

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). 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. Example PHP code:

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)
  • I have set chmod 777 for /dev/ttyUSB0
  • I have verified that PHP can write to /dev/ttyUSB0 (is_writable())
  • I have tried a lot of different stty settings (ex: 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.

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. The second time, after trying from PHP, the try from the command line is successful.

I have tried running the PHP file from command line (CLI, both PHP5 and PHP7) and using the web server but nothing works.

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. Other command I try, using PHP, works - for example:

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

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. The command works but not when run using 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.

// Grants write permission on /dev/ttyS0 to all users

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

Hope this helps you out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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