title: Write hex to serial port from PHP
body: <p>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:</p>
  
  <pre><code>echo -en "\x63\x69\x72\x62\x0d" &gt; /dev/ttyUSB0
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>var_dump( shell_exec('echo -en "\x63\x69\x72\x62\x0d" &gt; /dev/ttyUSB0') );
  </code></pre>
  
  <p>I have tried a lot of things to get this working:</p>
  
  <ul>
  <li>I have added www-data (PHP user) to group dialout (group for /dev/ttyUSB0)</li>
  <li>I have set chmod 777 for /dev/ttyUSB0</li>
  <li>I have verified that PHP can write to /dev/ttyUSB0 (is_writable())</li>
  <li>I have tried a lot of different stty settings (ex: stty -F /dev/ttyUSB0 -isig -icanon)</li>
  <li>I have tried different ways in PHP to write to the serial port (PhpSerial library, pack() and fwrite(), printf() etc)
  Among other things.</li>
  </ul>
  
  <p>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.</p>
  
  <p>I have tried running the PHP file from command line (CLI, both PHP5 and PHP7) and using the web server but nothing works.</p>
  
  <p>I have tried all the things I can think of, and found online, but I can still not get it to work.</p>
  
  <p>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:</p>
  
  <pre><code>var_dump( shell_exec('whoami') );
  </code></pre>
  
  <p><strong>Update #1</strong>
  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:</p>
  
  <pre><code>sudo -u www-data bash -c '/home/test/cmd'
  </code></pre>
  
  <p><strong>Update #2</strong>
  Tried the same thing on another computer, running Kodi, with the same negative result. The command works but not when run using PHP.</p>