Jump to content

Serial port connection works only after connecting with hyperterminal or putty


Giuliano

Recommended Posts

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.