System() in php

hello guys, i’m using system() command in php to execute command in terminal through php but it seems like there’s a problem occured. Below is the simple code in php


<?php
$command = system('perl -e' print "\\xF5\\x01\\x00\\x01\\x03\\x00\\x03\\xF5"'>/dev/ttyUSB0 ');
?>

it suppose send command to the serial port and light up my LED but it seems like nothing happened. Please share your idea guys. Thank you.

You’re having a quote issue, as you might be able to see above.

Escape the single-quotes which PHP should see as “just another character” instead of “end the string”, and you’ll be fine (hopefully):

$command = system('perl -e \\' print "\\xF5\\x01\\x00\\x01\\x03\\x00\\x03\\xF5" \\' > /dev/ttyUSB0'); 

If that looks messy to you (it does to me), consider the following:


$PerlCommand = <<<CMD
    print "\\xF5\\x01\\x00\\x01\\x03\\x00\\x03\\xF5";
CMD;
$command = system("perl -e '{$PerlCommand}' > /dev/ttyUSB0");

Although I believe you can do the same thing without using perl at all, by simply writing to /dev/ttyUSB0 manually within the terminal, or possibly with PHP if you want to make it even simpler.

Hello jake,thanks for replying. Actually i did tried to run the command in terminal and it works so i guess the quote is not a big deal.