parsedump: Support running on both python2 and python3
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
a42f615881
commit
336cc92a0a
|
@ -21,7 +21,7 @@ MESSAGE_TRAILER_SYNC = 1
|
|||
MESSAGE_PAYLOAD_MAX = MESSAGE_MAX - MESSAGE_MIN
|
||||
MESSAGE_SEQ_MASK = 0x0f
|
||||
MESSAGE_DEST = 0x10
|
||||
MESSAGE_SYNC = '\x7E'
|
||||
MESSAGE_SYNC = 0x7e
|
||||
|
||||
class error(Exception):
|
||||
pass
|
||||
|
@ -29,12 +29,10 @@ class error(Exception):
|
|||
def crc16_ccitt(buf):
|
||||
crc = 0xffff
|
||||
for data in buf:
|
||||
data = ord(data)
|
||||
data ^= crc & 0xff
|
||||
data ^= (data & 0x0f) << 4
|
||||
crc = ((data << 8) | (crc >> 8)) ^ (data >> 4) ^ (data << 3)
|
||||
crc = chr(crc >> 8) + chr(crc & 0xff)
|
||||
return crc
|
||||
return [crc >> 8, crc & 0xff]
|
||||
|
||||
class PT_uint32:
|
||||
is_int = True
|
||||
|
@ -245,10 +243,10 @@ class MessageParser:
|
|||
def check_packet(self, s):
|
||||
if len(s) < MESSAGE_MIN:
|
||||
return 0
|
||||
msglen = ord(s[MESSAGE_POS_LEN])
|
||||
msglen = s[MESSAGE_POS_LEN]
|
||||
if msglen < MESSAGE_MIN or msglen > MESSAGE_MAX:
|
||||
return -1
|
||||
msgseq = ord(s[MESSAGE_POS_SEQ])
|
||||
msgseq = s[MESSAGE_POS_SEQ]
|
||||
if (msgseq & ~MESSAGE_SEQ_MASK) != MESSAGE_DEST:
|
||||
return -1
|
||||
if len(s) < msglen:
|
||||
|
@ -258,7 +256,7 @@ class MessageParser:
|
|||
return -1
|
||||
msgcrc = s[msglen-MESSAGE_TRAILER_CRC:msglen-MESSAGE_TRAILER_CRC+2]
|
||||
crc = crc16_ccitt(s[:msglen-MESSAGE_TRAILER_SIZE])
|
||||
if crc != msgcrc:
|
||||
if crc != list(msgcrc):
|
||||
#logging.debug("got crc %s vs %s", repr(crc), repr(msgcrc))
|
||||
return -1
|
||||
return msglen
|
||||
|
@ -294,10 +292,10 @@ class MessageParser:
|
|||
def encode(self, seq, cmd):
|
||||
msglen = MESSAGE_MIN + len(cmd)
|
||||
seq = (seq & MESSAGE_SEQ_MASK) | MESSAGE_DEST
|
||||
out = [chr(msglen), chr(seq), cmd]
|
||||
out.append(crc16_ccitt(''.join(out)))
|
||||
out = [msglen, seq] + cmd
|
||||
out.append(crc16_ccitt(out))
|
||||
out.append(MESSAGE_SYNC)
|
||||
return ''.join(out)
|
||||
return out
|
||||
def _parse_buffer(self, value):
|
||||
if not value:
|
||||
return []
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2
|
||||
#!/usr/bin/env python
|
||||
# Script to parse a serial port data dump
|
||||
#
|
||||
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
|
||||
|
@ -23,12 +23,12 @@ def main():
|
|||
|
||||
f = open(data_filename, 'rb')
|
||||
fd = f.fileno()
|
||||
data = ""
|
||||
data = bytearray()
|
||||
while 1:
|
||||
newdata = os.read(fd, 4096)
|
||||
if not newdata:
|
||||
break
|
||||
data += newdata
|
||||
data += bytearray(newdata)
|
||||
while 1:
|
||||
l = mp.check_packet(data)
|
||||
if l == 0:
|
||||
|
@ -37,7 +37,7 @@ def main():
|
|||
logging.error("Invalid data")
|
||||
data = data[-l:]
|
||||
continue
|
||||
msgs = mp.dump(bytearray(data[:l]))
|
||||
msgs = mp.dump(data[:l])
|
||||
sys.stdout.write('\n'.join(msgs[1:]) + '\n')
|
||||
data = data[l:]
|
||||
|
||||
|
|
Loading…
Reference in New Issue