Hello everybody.

I've developed a script that communicates with a weight scale serial port and prints the data to the user.

Here's how I make the connection:

$port='COM6';
$baud='4800';
$data='7';
$stop='1';
$parity='N';
$xon='on';

$command = exec('mode '.$port.': baud='.$baud.' data='.$data.' stop='.$stop.' parity='.$parity.' xon='.$xon.'');

$fd = dio_open($porta.':', O_RDWR | O_NOCTTY | O_NONBLOCK);

if(!$fd)
{
	echo "Error to access {$port}!";
}
else
{
	sleep(5);

while (true)
{
	$buffer = dio_read($fd);

	if ($buffer)
	{
		echo $buffer;
	}		

	sleep(1);
}

dio_close($fd);
}	

The problem is that the script only works correctly after I open a connection with Hyperterminal or PUTTY. It looks like the PHP itself doesn't open the connection with port COM6. If I try to run the script without opening a connection without Hyperterminal or PUTTY, it doesn't read anything.

Someone know why is this happening? I'm using Windows, Apache 2.2 and PHP 5.3

Thanks in advance and sorry for my english.

    Do you have display_errors set to On and error_reporting set to E_ALL? One problem I see is that you're referencing an undefined variable called $porta.

      bradgrafelman, thank you for replying.
      Actually, I've written wrong the code in the post. The variable name is really $port in my script.
      My display_errors is set to On and error_reporting set to E_ALL.

      I've done this small script to test:

      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, the script prints the result correctly.

        Write a Reply...