File source

<?php
/** Windows serial port wrapper 
* open() - opens port
* write() - writes string to port
* read($len) - reads $len-long string from port
* flush() - flushes buffer
* close() - closes port
* setTimeout($t) - sets timeout to $t miliseconds
**/
class Port{
  protected 
$port;
  protected 
$fd false;
  function 
__construct($port){
    
$this->port $port;
  }

  function 
open(){
    
exec("mode ".$this->port." baud=57600 stop=1 parity=n xon=off idsr=on data=8");
    
$this->close();
    
$this->fd fopen($this->port"r+b");
  }

  function 
write($str$flush=true){
    
$w fwrite($this->fd,$str);
    if(!
$w){
      throw new 
Exception("Error writing to port"1);
    }
    if(
$flush){
      
$this->flush();
    }
    return 
$w;
  }

  function 
read($len){
    if(
feof($this->fd)){
      throw new 
Exception("Error reading from port (EOF)"1);
    }
    
$r fread($this->fd$len);
    return 
$r;
  }

  function 
flush(){
    
fflush($this->fd);
  }

  function 
close(){
    if(
$this->fd){
      
fclose($this->fd);
    }
    
$this->fd false;
  }

  function 
__destruct(){
    
$this->close();
  }

  function 
setTimeout($t){
    
$sec floor($t/1000);
    
$usec = ($t $sec*1000)*1000;
  
//  socket_set_timeout($this->fd,$sec,$usec);
  
}
}