title: PHP: trying to read data from Serial COM port ( Windows )
body: <p>I am tired of working with Serial comport to send and receive data ( Windows ). I have tried every possible way. Every time i got able to connect and send data. But when it comes to receiving lines script got hanged. </p>
  
  <p>I am sure device which is connected to comport is working as it returned data when reading through windows power shell.</p>
  
  <p>There are two most common methods i have tried are:</p>
  
  <p>1 <a href="https://github.com/Xowap/PHP-Serial" rel="nofollow noreferrer" title="PhpSerial.php">PhpSerial.php</a>  ( Line: 597 )</p>
  
  <p>2 Direct-Dio library</p>
  
  <p>By using Direct-Dio
  getting Hanged on > $data = dio_read($bbSerialPort, 1024);</p>
  
  <pre><code>&lt;?php
  
  
  $portName = 'com1:';
  $baudRate =9600;
  $bits = 8;
  $spotBit = 1;
  
  header( 'Content-type: text/plain; charset=utf-8' ); 
  ?&gt;
  Serial Port Test
  ================
  &lt;?php
  
  
  function echoFlush($string)
  {
  echo $string . "\n";
  flush();
  ob_flush();
  }
  
  if(!extension_loaded('dio'))
  {
  echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
  exit;
  }
  
  try 
  {
  $output = exec('$port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one',$results);
  exec( '
      ' );
  exec( '$port.WriteLine("&lt;A&gt;")',$results );
  $test = exec( '$port.ReadLine()',$results );
  echo $test;
  
  print_r($results);
  
  //the serial port resource
  $bbSerialPort;
  
  echoFlush(  "Connecting to serial port: {$portName}" );
  
  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') 
  { 
      $bbSerialPort = dio_open($portName, O_RDWR );
      //we're on windows configure com port from command line
      exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
  
  } 
  else //'nix
  {
      $bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
      dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
      //we're on 'nix configure com from php direct io function
      dio_tcsetattr($bbSerialPort, array(
          'baud' =&gt; $baudRate,
          'bits' =&gt; $bits,
          'stop'  =&gt; $spotBit,
          'parity' =&gt; 0
      ));
  }
  
  if(!$bbSerialPort)
  {
      echoFlush( "Could not open Serial port {$portName} ");
      exit;
  }
  
  
  // send data
  
  $dataToSend = "&lt;A&gt;";
  echoFlush( "Writing to serial port data: \"{$dataToSend}\"" );
  $bytesSent = dio_write($bbSerialPort, $dataToSend );
  echoFlush( "Sent: {$bytesSent} bytes" );
  exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=on");
  //date_default_timezone_set ("Europe/London");
  
  $runForSeconds = new DateInterval("PT3S"); //10 seconds
  $endTime = (new DateTime())-&gt;add($runForSeconds);
  
  echoFlush(  "Waiting for {$runForSeconds-&gt;format('%S')} seconds to recieve data on serial port" );
  dio_seek($bbSerialPort, -8, SEEK_END);
  
  
  while (new DateTime() &lt; $endTime) 
  {
  $data ="";
      $data = dio_read($bbSerialPort, 1024); //this is a blocking call
      if ($data) {
          echoFlush(  "Data Received: ". $data );
      }
  }
  $output = exec ( '$port.ReadLine()',$results );
  print_r($results);
   exec ( '$port.Close()' );
  echo "&lt;pre&gt;$output&lt;/pre&gt;";  echoFlush(  "Closing Port" );       dio_close($bbSerialPort);   
  } 
  catch (Exception $e) 
  {
  echoFlush('CATCH');
  echoFlush(  $e-&gt;getMessage() );
  exit(1);
  } 
  
  ?&gt;
  </code></pre>
  
  <p>I din't find any solution on internet.</p>