Giuliano Posted August 20, 2012 Share Posted August 20, 2012 Hello everybody. I have a script that connects with a weight scale serial port, reads the data and prints the result. Here it is: error_reporting(E_ALL); exec('mode COM6: baud=4800 data=7 stop=1 parity=n xon=on'); $fd = dio_open('COM6:', O_RDWR); if(!$fd) { echo "Error!"; } else { sleep(5); $buffer = dio_read($fd); if ($buffer) { echo $buffer; } } The script should print the buffer when the weight scale print some result. However PHP keeps waiting forever. No error. BUT, if I start a connection with COM6 using Hyperterminal or PUTTY before I run the script, it works perfectly. Any ideas? I'm using Windows, Apache 2.2 and PHP 5.3 Thank you and sorry for my english. Quote Link to comment Share on other sites More sharing options...
btherl Posted August 21, 2012 Share Posted August 21, 2012 Maybe you can call putty first to get the port into the correct state. Perhaps with plink.exe. Or you could put your entire connection through plink.exe using proc_open(). Maybe it'll work Quote Link to comment Share on other sites More sharing options...
Giuliano Posted August 21, 2012 Author Share Posted August 21, 2012 btherl, thank you for replying. Using your idea, I executed the following script: error_reporting(E_ALL); $command = exec('c:\fontes\web\sisbalance\lib\plink -serial COM6 -sercfg 4800,7,n,1,n'); With that, PHP hangs forever, just like the other script, because he waits for serial response. So I tried to send the output to a txt file, like this: error_reporting(E_ALL); $command = exec('c:\fontes\web\sisbalance\lib\plink -serial COM6 -sercfg 4800,7,n,1,n >> c:\fontes\web\sisbalance\lib\log.txt 2>&1 &'); PHP still hangs forever. However, the serial port data is sent correctly to log.txt. My question is: Is there a way to execute the script using plink.exe without hanging forever? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
btherl Posted August 21, 2012 Share Posted August 21, 2012 Hi Giuliano, What about using proc_open() instead? There is an example in the php documentation for how to use it: http://php.net/manual/en/function.proc-open.php The problem with exec() is it will wait for plink to terminate before returning the output. And plink never terminates. But proc_open() will let you run plink at the same time as php is running. In your test above you still get the output in the log file because plink is running and writing to the log, even though php is suspended waiting for plink to finish. Quote Link to comment Share on other sites More sharing options...
Giuliano Posted August 22, 2012 Author Share Posted August 22, 2012 Hello btherl! Do you think the script bellow is correct? I'm trying to execute it, and still PHP is hanging forever. error_reporting(E_ALL); $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "error-output.txt", "a") // stderr is a file to write to ); $cwd = NULL; $env = array('some_option' => 'aeiou'); $process = proc_open('c:/fontes/web/sisbalance/lib/plink -serial COM6', $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { fclose($pipes[0]); echo stream_get_contents($pipes[1]); fclose($pipes[1]); $return_value = proc_close($process); echo "command returned $return_value\n"; } Quote Link to comment Share on other sites More sharing options...
btherl Posted August 23, 2012 Share Posted August 23, 2012 I notice you're not using sercfg in the last code you posted, is there a reason for that? You could try leaving all the pipes open. Plink might be unhappy to have stdin closed. But I think the most important thing is to make sure you are using non-blocking I/O. This function might work: http://php.net/manual/en/function.stream-set-blocking.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.