2 require
'BayEOSSerialPHP.php';
22 define(
"XBEE_ESCAPE",pack(
"C",0x7d));
23 define(
"XBEE_DELIM",pack(
"C",0x7e));
24 define(
"XON",pack(
"C",0x11));
25 define(
"XOFF",pack(
"C",0x13));
60 public function open($waitForOpened=0.1) {
62 usleep((
int) ($waitForOpened * 1000000));
89 if(!
$data)
return count($this->stack);
90 if(! isset($this->stack[0])){
92 if($delim_pos===FALSE)
return 0;
97 $offset=count($this->stack)-1;
98 if($offset==-1) $offset=0;
99 for($i=0;$i<count(
$data);$i++){
101 if(! isset($this->stack[$index][
'ts']))
102 $this->stack[$index][
'ts']=microtime(TRUE);
103 if(! isset($this->stack[$index][
'rawframe']))
104 $this->stack[$index][
'rawframe']=
'';
105 $this->stack[$index][
'rawframe'].=
$data[$i];
106 $this->stack[$index][
'frame']=$this->_unescape($this->stack[$index][
'rawframe']);
107 $this->stack[$index][
'ok']=$this->_parseFrame($this->stack[$index][
'frame']);
108 if(! $this->stack[$index][
'ok'] && ($i+1)<count(
$data)){
110 unset($this->stack[$index]);
112 fwrite(STDERR,date(
'Y-m-d H:i:s').
' '.$this->_device.
" : Invalid frame\n");
116 $anz=count($this->stack);
118 if(! $this->stack[$anz-1][
'ok']) $anz--;
123 $this->
send(pack(
'C7',0x00,0x04,0x08,0x01,0x49,0x44,0x69));
128 if($f[2]==pack(
"C",0x88) && $f[4]==
'I' && $f[5]==
'D' && $f[6]==pack(
"C",0x0)){
129 return array_pop(unpack(
'n',substr($f,7,2)));
132 die(
"Could not get PANID\n");
135 private function _parseFrame(
$frame){
136 if(strlen(
$frame)<3)
return FALSE;
137 $length = substr(
$frame, 0, 2);
138 $checksum = substr(
$frame, -1);
139 $cmdData = substr(
$frame, 2, -1);
141 $calculatedLength = pack(
'n',strlen($cmdData));
144 if ($checksum === $calculatedChecksum && $length === $calculatedLength) {
162 for ($i = 0; $i < strlen(
$data); $i++) {
163 $checksum += ord(
$data[$i]);
165 $checksum = $checksum & 0xFF;
166 $checksum = 0xFF - $checksum;
167 $checksum = chr($checksum);
173 while($this->
read()==0 && $timeout>0){
177 if($timeout<0)
return FALSE;
178 return array_shift($this->stack);
181 private function _escape($rawData){
183 for($i=0;$i<strlen($rawData);$i++){
186 else $res.=$rawData[$i];
191 private function _unescape($rawData){
194 for($i=0;$i<strlen($rawData);$i++){
198 if($i<strlen($rawData))
199 $res.=pack(
"C",0x20) ^ $rawData[$i];
200 }
else $res.=$rawData[$i];
send($frame, $waitForReply=0)
sendMessage($str, $waitForReply=0.1)
confDefaults($device= '/dev/ttyUSB0')
confCharacterLength($int)