2 require 
'BayEOSSerialPHP.php';
 
    7 define(
"XBEE_ESCAPE",pack(
"C",0x7d));
 
    8 define(
"XBEE_DELIM",pack(
"C",0x7e));
 
    9 define(
"XON",pack(
"C",0x11));
 
   10 define(
"XOFF",pack(
"C",0x13));
 
   12 define(
"API_DATA",pack(
"C",0x1));
 
   13 define(
"API_ACK",pack(
"C",0x2));
 
   15 define(
"TX_OK",pack(
"C",0x1));
 
   16 define(
"TX_CHECKSUM_FAILED",pack(
"C",0x2));
 
   17 define(
"TX_BREAK",pack(
"C",0x3));
 
   53         public function open($waitForOpened=0.1) {
 
   55                   usleep((
int) ($waitForOpened * 1000000));
 
   74             $frame=pack(
"C",strlen(
$frame)).$api.$frame.$this->_calcChecksum($api.$frame);
 
   81             if($res=$this->
getFrame($timeout)===FALSE) 
return 2;                
 
   87             if($res=$this->
getFrame($timeout)===FALSE) 
return 2;
 
   90                 if($res[
'frame']===
TX_OK) 
return 0;
 
   92                 if($res[
'frame']===
TX_BREAK) 
return 3;
 
  101             if(! 
$data) 
return count($this->stack);
 
  102             if(! isset($this->stack[0])){
 
  104                 if($delim_pos===FALSE) 
return 0;
 
  109             $offset=count($this->stack)-1;
 
  110             if($offset==-1) $offset=0;
 
  111             for($i=0;$i<count(
$data);$i++){
 
  113                 if(! isset($this->stack[$index][
'ts']))
 
  114                     $this->stack[$index][
'ts']=microtime(TRUE);
 
  115                 if(! isset($this->stack[$index][
'rawframe'])) 
 
  116                     $this->stack[$index][
'rawframe']=
'';
 
  117                 $this->stack[$index][
'rawframe'].=
$data[$i];
 
  118                 $this->stack[$index][
'frame']=$this->_unescape($this->stack[$index][
'rawframe']);
 
  120                 switch($this->_parseFrame($this->stack[$index][
'frame'])){
 
  122                         $this->stack[$index][
'ok']=TRUE;
 
  125                         unset($this->stack[$index]);
 
  126                         fwrite(STDERR,date(
'Y-m-d H:i:s').
' '.$this->_device.
" : Checksum failure\n");
 
  130                         $this->stack[$index][
'ok']=FALSE;
 
  133                 if(isset($this->stack[$index]) && ! $this->stack[$index][
'ok'] && ($i+1)<count(
$data)){
 
  134                     unset($this->stack[$index]);
 
  136                     fwrite(STDERR,date(
'Y-m-d H:i:s').
' '.$this->_device.
" : Incomplete frame\n");
 
  140             $anz=count($this->stack);
 
  142             if($anz && ! $this->stack[$anz-1][
'ok']) $anz--;
 
  147         private function _parseFrame(
$frame){
 
  148             if(strlen(
$frame)<3) 
return 2;
 
  150             $length = substr(
$frame, 0, 1);
 
  151             $checksum = substr(
$frame, -1);
 
  152             $cmdData = substr(
$frame, 1, -1);
 
  154             $calculatedLength = pack(
'C',strlen($cmdData)-1);
 
  157             if ($checksum === $calculatedChecksum && $length === $calculatedLength) {
 
  159                 $this->_sendAck(
TX_OK);
 
  161             } 
else if($length === $calculatedLength) {
 
  170         private function _sendAck($type){
 
  185             for ($i = 0; $i < strlen(
$data); $i++) {
 
  186                 $checksum += ord(
$data[$i]);
 
  188             $checksum = $checksum & 0xFF;
 
  189             $checksum = 0xFF - $checksum;
 
  190             $checksum = chr($checksum);
 
  196             while($this->
read()==0 && $timeout>0){
 
  200             if($timeout<0) 
return FALSE;                
 
  202             $frame=array_shift($this->stack);
 
  210         private function _escape($rawData){
 
  212             for($i=0;$i<strlen($rawData);$i++){
 
  215                 else $res.=$rawData[$i];
 
  220         private function _unescape($rawData){
 
  223             for($i=0;$i<strlen($rawData);$i++){
 
  227                     if($i<strlen($rawData))
 
  228                         $res.=pack(
"C",0x20) ^ $rawData[$i];
 
  229                 } 
else $res.=$rawData[$i];
 
sendMessage($str, $waitForReply=0.1)
send($frame, $api=API_DATA, $timeout=120)
confDefaults($device= '/dev/ttyUSB0')
confCharacterLength($int)