Mercurial > ~darius > hgwebdir.cgi > epro
comparison epro.py @ 4:787d9c8fdec6
Add working example when run as an exe
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sun, 19 Nov 2017 18:08:13 +1030 |
parents | 6d0fe22566ab |
children | 982eeffe9d95 |
comparison
equal
deleted
inserted
replaced
3:6d0fe22566ab | 4:787d9c8fdec6 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | |
3 import serial | |
4 import sys | |
2 | 5 |
3 # View facing ePro from the back | 6 # View facing ePro from the back |
4 # +---+ | 7 # +---+ |
5 # +-| |-| | 8 # +-| |-| |
6 # | | | 9 # | | |
214 def __init__(self): | 217 def __init__(self): |
215 self.state = 0 | 218 self.state = 0 |
216 self.packets = [] | 219 self.packets = [] |
217 | 220 |
218 def process(self, dat): | 221 def process(self, dat): |
222 added = False | |
219 for d in dat: | 223 for d in dat: |
220 d = ord(d) | 224 d = ord(d) |
221 if d == 0xff and self.state != 4: | 225 if d == 0xff and self.state != 4: |
222 print "Packet corruption" | 226 print "Packet corruption" |
223 continue | 227 continue |
256 | 260 |
257 p = self.PKT_TYPES[self.msgtype](self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) | 261 p = self.PKT_TYPES[self.msgtype](self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) |
258 else: | 262 else: |
259 p = Packet(self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) | 263 p = Packet(self.dstadr, self.srcadr, self.devid, self.msgtype, self.data) |
260 | 264 |
261 print p | |
262 self.packets.append(p) | 265 self.packets.append(p) |
266 added = True | |
267 return added | |
268 | |
269 def main(): | |
270 if len(sys.argv) != 2: | |
271 print 'Bad usage' | |
272 exit(1) | |
273 | |
274 s = serial.Serial(sys.argv[1], 2400, parity='E') | |
275 s.timeout = 0.2 | |
276 | |
277 p = Processor() | |
278 while True: | |
279 res = p.process(s.read(1024)) | |
280 while len(p.packets) > 0: | |
281 print(p.packets.pop(0)) | |
263 | 282 |
264 if __name__ == '__main__': | 283 if __name__ == '__main__': |
265 main() | 284 main() |