SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: System() in php

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2013
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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):
    PHP Code:
    $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:
    PHP Code:
    $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.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2013
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    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):
    PHP Code:
    $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:
    PHP Code:
    $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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •