parsedump: Support running on both python2 and python3

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-12-07 14:31:03 -05:00
parent a42f615881
commit 336cc92a0a
2 changed files with 12 additions and 14 deletions

View File

@ -21,7 +21,7 @@ MESSAGE_TRAILER_SYNC = 1
MESSAGE_PAYLOAD_MAX = MESSAGE_MAX - MESSAGE_MIN MESSAGE_PAYLOAD_MAX = MESSAGE_MAX - MESSAGE_MIN
MESSAGE_SEQ_MASK = 0x0f MESSAGE_SEQ_MASK = 0x0f
MESSAGE_DEST = 0x10 MESSAGE_DEST = 0x10
MESSAGE_SYNC = '\x7E' MESSAGE_SYNC = 0x7e
class error(Exception): class error(Exception):
pass pass
@ -29,12 +29,10 @@ class error(Exception):
def crc16_ccitt(buf): def crc16_ccitt(buf):
crc = 0xffff crc = 0xffff
for data in buf: for data in buf:
data = ord(data)
data ^= crc & 0xff data ^= crc & 0xff
data ^= (data & 0x0f) << 4 data ^= (data & 0x0f) << 4
crc = ((data << 8) | (crc >> 8)) ^ (data >> 4) ^ (data << 3) crc = ((data << 8) | (crc >> 8)) ^ (data >> 4) ^ (data << 3)
crc = chr(crc >> 8) + chr(crc & 0xff) return [crc >> 8, crc & 0xff]
return crc
class PT_uint32: class PT_uint32:
is_int = True is_int = True
@ -245,10 +243,10 @@ class MessageParser:
def check_packet(self, s): def check_packet(self, s):
if len(s) < MESSAGE_MIN: if len(s) < MESSAGE_MIN:
return 0 return 0
msglen = ord(s[MESSAGE_POS_LEN]) msglen = s[MESSAGE_POS_LEN]
if msglen < MESSAGE_MIN or msglen > MESSAGE_MAX: if msglen < MESSAGE_MIN or msglen > MESSAGE_MAX:
return -1 return -1
msgseq = ord(s[MESSAGE_POS_SEQ]) msgseq = s[MESSAGE_POS_SEQ]
if (msgseq & ~MESSAGE_SEQ_MASK) != MESSAGE_DEST: if (msgseq & ~MESSAGE_SEQ_MASK) != MESSAGE_DEST:
return -1 return -1
if len(s) < msglen: if len(s) < msglen:
@ -258,7 +256,7 @@ class MessageParser:
return -1 return -1
msgcrc = s[msglen-MESSAGE_TRAILER_CRC:msglen-MESSAGE_TRAILER_CRC+2] msgcrc = s[msglen-MESSAGE_TRAILER_CRC:msglen-MESSAGE_TRAILER_CRC+2]
crc = crc16_ccitt(s[:msglen-MESSAGE_TRAILER_SIZE]) 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)) #logging.debug("got crc %s vs %s", repr(crc), repr(msgcrc))
return -1 return -1
return msglen return msglen
@ -294,10 +292,10 @@ class MessageParser:
def encode(self, seq, cmd): def encode(self, seq, cmd):
msglen = MESSAGE_MIN + len(cmd) msglen = MESSAGE_MIN + len(cmd)
seq = (seq & MESSAGE_SEQ_MASK) | MESSAGE_DEST seq = (seq & MESSAGE_SEQ_MASK) | MESSAGE_DEST
out = [chr(msglen), chr(seq), cmd] out = [msglen, seq] + cmd
out.append(crc16_ccitt(''.join(out))) out.append(crc16_ccitt(out))
out.append(MESSAGE_SYNC) out.append(MESSAGE_SYNC)
return ''.join(out) return out
def _parse_buffer(self, value): def _parse_buffer(self, value):
if not value: if not value:
return [] return []

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python
# Script to parse a serial port data dump # Script to parse a serial port data dump
# #
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
@ -23,12 +23,12 @@ def main():
f = open(data_filename, 'rb') f = open(data_filename, 'rb')
fd = f.fileno() fd = f.fileno()
data = "" data = bytearray()
while 1: while 1:
newdata = os.read(fd, 4096) newdata = os.read(fd, 4096)
if not newdata: if not newdata:
break break
data += newdata data += bytearray(newdata)
while 1: while 1:
l = mp.check_packet(data) l = mp.check_packet(data)
if l == 0: if l == 0:
@ -37,7 +37,7 @@ def main():
logging.error("Invalid data") logging.error("Invalid data")
data = data[-l:] data = data[-l:]
continue continue
msgs = mp.dump(bytearray(data[:l])) msgs = mp.dump(data[:l])
sys.stdout.write('\n'.join(msgs[1:]) + '\n') sys.stdout.write('\n'.join(msgs[1:]) + '\n')
data = data[l:] data = data[l:]