comparison epro.py @ 12:0a571da65068

Use logging rather than print.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 06 Dec 2021 11:25:41 +1030
parents 3baca74482b6
children 4450cf739263
comparison
equal deleted inserted replaced
11:b4d6c6049024 12:0a571da65068
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import logging
3 import serial 4 import serial
4 import sys 5 import sys
6
7 logger = logging.getLogger('epro')
5 8
6 # View facing ePro from the back 9 # View facing ePro from the back
7 # +---+ 10 # +---+
8 # +-| |-| 11 # +-| |-|
9 # | | 12 # | |
221 def process(self, dat): 224 def process(self, dat):
222 added = False 225 added = False
223 for d in dat: 226 for d in dat:
224 d = ord(d) 227 d = ord(d)
225 if d == 0xff and self.state != 4: 228 if d == 0xff and self.state != 4:
226 print "Packet corruption" 229 logger.warn("Packet corruption")
227 continue 230 continue
228 231
229 if self.state == 0: 232 if self.state == 0:
230 # Waiting for destination address (MSB set but not 0xff as that is EOM) 233 # Waiting for destination address (MSB set but not 0xff as that is EOM)
231 if d == 0xff or d & 0x80 == 0: 234 if d == 0xff or d & 0x80 == 0:
232 print "Skipping byte" 235 logger.info("Skipping byte")
233 continue 236 continue
234 self.dstadr = d & 0x7f 237 self.dstadr = d & 0x7f
235 self.data = [] 238 self.data = []
236 self.state += 1 239 self.state += 1
237 elif self.state == 1: 240 elif self.state == 1:
253 continue 256 continue
254 self.state = 0 257 self.state = 0
255 if self.msgtype in Processor.PKT_TYPES: 258 if self.msgtype in Processor.PKT_TYPES:
256 t = self.PKT_TYPES[self.msgtype] 259 t = self.PKT_TYPES[self.msgtype]
257 if len(self.data) != t.LEN: 260 if len(self.data) != t.LEN:
258 print "Packet length incorrect, expected %d got %d" % (t.LEN, len(self.data)) 261 logger.warn("Packet length incorrect, expected %d got %d", t.LEN, len(self.data))
259 continue 262 continue
260 263
261 p = self.PKT_TYPES[self.msgtype](self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) 264 p = self.PKT_TYPES[self.msgtype](self.dstadr, self.srcadr, self.devid, self.msgtype, self.data)
262 else: 265 else:
263 p = Packet(self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) 266 p = Packet(self.dstadr, self.srcadr, self.devid, self.msgtype, self.data)
266 added = True 269 added = True
267 return added 270 return added
268 271
269 def main(): 272 def main():
270 if len(sys.argv) != 2: 273 if len(sys.argv) != 2:
271 print 'Bad usage' 274 print('Bad usage')
272 exit(1) 275 exit(1)
273 276
274 s = serial.Serial(sys.argv[1], 2400, parity='E') 277 s = serial.Serial(sys.argv[1], 2400, parity='E')
275 s.timeout = 0.2 278 s.timeout = 0.2
276 279