4 from serial import Serial
5 from datetime import datetime
10 #buildingFrame = 0; #if it is a start of new frame
32 snsCode = {'light':LGT_SNS,'touch':TCH_SNS,'acc':ACC_SNS,'mag':MAG_SNS,'a2d':ADC_SNS_1}
33 cmdCode = {'start':START, 'stop':STOP, 'set':SET_INT}
34 sizeMap = {INT:2, FLOAT:4, LONG:4, DOUBLE:8}
35 typeMap = {0:INT, 1:LONG, FLOAT:2, DOUBLE:3}
42 class State: #information stored
51 self.time = datetime.utcnow()
55 def __init__(self, config):
66 self.serial = Serial(self.config.device, \
67 baudrate = self.config.baudrate, \
68 parity = self.config.parity, \
69 bytesize = self.config.databits, \
70 stopbits = self.config.stopbits, \
73 self.serial.flushInput()
74 except Exception as ex:
79 if self.serial and self.serial.isOpen():
82 def running(self): # isRunning
83 if self.serial == None:
85 if self.serial.isOpen() == False:
92 for key in list(self.config.enable.keys()):
93 state = self.config.enable[key]
94 rate = self.config.rate[key]
95 cmd = Const.cmdCode['start'] if state else Const.cmdCode['stop']
96 sns = Const.snsCode[key]
97 frame[0] = chr(Const.HEADER)
98 frame[1] = chr(cmd<<(Const.SNS_BITS)|(0x1F&sns))
99 frame[2:6] = pack('f',float(rate))
100 frame[6] = Const.TAIL
101 self.serial.write(frame)
102 print('[SEND1] ',frame)
103 frame[1] = Const.cmdCode['set']<<(Const.SNS_BITS)|(0x1F&sns)
104 self.serial.write(frame)
105 print('[SEND2] ',frame)
113 if not self.running():
115 if self.serial.readable():
117 while (self.serial.inWaiting() or count<(self.frame_len(self.inbuf)-1)): ######
118 char = self.serial.read()
120 if char == chr(Const.TAIL) and buildingFrame and (len(self.inbuf))== self.frame_len(self.inbuf)-1:
121 self.inbuf.append(char)
123 line = "".join(self.inbuf)
127 item = self._parse_ascii(line) # analyze the received data
129 buildingFrame = 0 # finished building one frame
130 #print ("BF set to 0")
133 elif char == chr(Const.HEADER) and buildingFrame==0:
136 #print ("BF set to 1")
137 self.inbuf.append(char)
141 self.inbuf.append(char)
145 #print("[ERROR] Byte Going Nowhere")
147 buildingFrame = 0 # reset the construction
148 #print ("BF set to 0!")
152 print("[ERROR] Exceeded Read Limit")
158 def frame_len(self, frame):
161 dataType_snsType = ord(frame[1])
162 dataNum = ord(frame[2])
163 dataType = (0xE0&dataType_snsType)>>Const.SNS_BITS
164 snsType = 0x1F&dataType_snsType
165 #print(dataType_snsType)
169 dataSize = Const.sizeMap[Const.typeMap[dataType]]
170 return (dataSize*dataNum+4)
173 def printHex(self, frame):
175 frameLen = self.frame_len(frame)
178 print(hex(ord(frame[i])))
183 def _write_ascii(self, line): # to be changed
186 print('write: [' + line + ']')
187 self.serial.write(line + '\n')
191 def _parse_ascii(self, line):##############
193 dataType_snsType = ord(line[1]);
194 dataNum = ord(line[2]);
195 dataType = (0xE0&dataType_snsType)>>Const.SNS_BITS
196 snsType = 0x1F&dataType_snsType
198 if snsType == Const.ACC_SNS:
200 state.acc = unpack('3f',line)
201 elif snsType == Const.MAG_SNS:
203 state.mag = unpack('3h',line)
204 elif snsType == Const.LGT_SNS:
205 state.light[0] = ord(line[3])
206 elif snsType == Const.TCH_SNS:
208 state.touch = sunpack('2f',line)
209 elif snsType == Const.ADC_SNS_1:
211 state.a2d = unpack('6h', line)
213 print('[ERROR] Nothing Happened!')