BayEOS-PHP
 All Data Structures Namespaces Files Functions Variables Pages
QLI.php
Go to the documentation of this file.
1 <?php
2 require 'BayEOSSerialPHP.php';
3 
8 define("QLI_BEGIN",pack("C",0x2));
9 define("QLI_END",pack("C",0x3));
10 define("XON",pack("C",0x11));
11 define("XOFF",pack("C",0x13));
12 class QLI extends phpSerial {
13  private $stack;
14  private $datetime_format;
15  private $tz;
16  private $indexmap;
22  function QLI($tz,$datetime_format,$indexmap=FALSE) {
23  $this->stack=array();
24  $this->tz=$tz;
25  $this->datetime_format=$datetime_format;
26  $this->indexmap=$indexmap;
27  parent::phpSerial();
28  }
29 
36  public function confDefaults($device = '/dev/ttyUSB0') {
37  $this -> deviceSet($device);
38  $this -> confBaudRate(9600);
39  $this -> confParity('none');
40  $this -> confCharacterLength(8);
41  $this -> confStopBits(1);
42  $this -> confFlowControl('none');
43  }
44 
52  public function open($waitForOpened=0.1) {
53  $this -> deviceOpen();
54  usleep((int) ($waitForOpened * 1000000));
55  }
56 
61  public function close() {
62  $this -> deviceClose();
63  }
64 
65 
66  public function read($count = 0){
67  $data = $this -> readPort($count);
68  //echo 'READ: '.array_pop(unpack('H*',$data))."\n";
69  if(! $data) return count($this->stack);
70  if(! isset($this->stack[0])){
71  $delim_pos=strpos($data,QLI_BEGIN);
72  if($delim_pos===FALSE) return 0;
73  else $data=substr($data,$delim_pos+1);
74  }
75 
76  $data=explode(QLI_BEGIN,$data);
77  $offset=count($this->stack)-1;
78  if($offset==-1) $offset=0;
79  for($i=0;$i<count($data);$i++){
80  $index=$i+$offset;
81  if(! isset($this->stack[$index]['frame']))
82  $this->stack[$index]['frame']='';
83  $this->stack[$index]['frame'].=$data[$i];
84  //echo "Stack[$index]: ".array_pop(unpack('H*',$this->stack[$index]['frame']))."\n";
85  $this->stack[$index]['ok']=$this->_parseFrame($index);
86  if(! $this->stack[$index]['ok'] && ($i+1)<count($data)){
87  //Invalid Frame!
88  echo "Invalid Frame\n";
89  unset($this->stack[$index]);
90  $offset--;
91  }
92  }
93 
94  $anz=count($this->stack);
95  //print_r($this->stack);
96  //echo "Stackcount: $anz\n";
97  if(! $this->stack[$anz-1]['ok']) $anz--;
98  //echo "read: $anz\n";
99  return $anz;
100  }
101 
102  private function _parseFrame($index){
103  $frame=$this->stack[$index]['frame'];
104  if(strpos($frame,QLI_END)===FALSE) return FALSE;
105  $pos=strpos($frame," ");
106  $length = substr($frame, 0, $pos);
107  if(strlen($frame)>$length) return FALSE;
108  $frame=substr($frame,$pos+1);
109  $pos=strpos($frame,"\r");
110  $ts=substr($frame, 0, $pos);
111  $ts_obj=DateTime::createFromFormat($this->datetime_format,$ts,new DateTimeZone($this->tz));
112  if(! $ts_obj) return FALSE;
113  $this->stack[$index]['ts']=floatval($ts_obj->format("U"));
114 
115  $frame=substr($frame,$pos+1);
116  $pos=strpos($frame,"\n");
117  $pos2=strpos($frame,QLI_END);
118  $frame=substr($frame,($pos+1),($pos2-$pos-1));
119  $data=explode("\r\n",$frame);
120 
121  $this->stack[$index]['values']=array();
122  for($i=0;$i<count($data);$i++){
123  if(strpos($data[$i],"=")===FALSE) continue;
124  $tmp=explode("=",$data[$i]);
125  //echo "parseFrame: '$data[$i]'\n";
126  if(is_numeric($tmp[1])){
127  if(is_array($this->indexmap)){
128  if(isset($this->indexmap[$tmp[0]]))
129  $this->stack[$index]['values'][$this->indexmap[$tmp[0]]]=$tmp[1];
130  }
131  else
132  $this->stack[$index]['values'][$i]=$tmp[1];
133  }
134  }
135  //print_r($this->stack[$index]);
136  return TRUE;
137  }
138 
139 
140 /*
141  * Will return an array with the following keys:
142  * ts -> seconds since 1.1.1970
143  * value -> array with values
144  * frame -> unparsed data
145  *
146  */
147 
148 
149  public function getFrame($timeout=120){
150  while($this->read()==0 && $timeout>0){
151  $timeout-=0.01;
152  usleep(10000);
153  }
154  if($timeout<0) return FALSE;
155  return array_shift($this->stack);
156  }
157 
158 
159 }
160 
161 
162 
163 
164 ?>
getFrame($timeout=120)
Definition: QLI.php:149
const QLI_BEGIN
Definition: QLI.php:8
$frame
Definition: testPHP.php:14
Definition: QLI.php:12
deviceOpen($mode="r+b")
close()
Definition: QLI.php:61
confFlowControl($mode)
confStopBits($length)
$count
Definition: BayEOSWriter.php:8
readPort($count=0)
read($count=0)
Definition: QLI.php:66
$pos
Definition: dumpCat.php:12
confBaudRate($rate)
confDefaults($device= '/dev/ttyUSB0')
Definition: QLI.php:36
QLI($tz, $datetime_format, $indexmap=FALSE)
Definition: QLI.php:22
const QLI_END
Definition: QLI.php:9
deviceSet($device)
confCharacterLength($int)
confParity($parity)
open($waitForOpened=0.1)
Definition: QLI.php:52