aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2022-12-07 14:31:03 -0500
committerKevin O'Connor <kevin@koconnor.net>2022-12-07 14:31:03 -0500
commit336cc92a0a06a78358d1c17c7c620655a6eaa055 (patch)
tree2a32675ecd435da9ad57e44e26196ad4b060e4fd
parenta42f615881a31c487067153aa7ce385146a5807c (diff)
downloadkutter-336cc92a0a06a78358d1c17c7c620655a6eaa055.tar.gz
kutter-336cc92a0a06a78358d1c17c7c620655a6eaa055.tar.xz
kutter-336cc92a0a06a78358d1c17c7c620655a6eaa055.zip
parsedump: Support running on both python2 and python3
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--klippy/msgproto.py18
-rwxr-xr-xklippy/parsedump.py8
2 files changed, 12 insertions, 14 deletions
diff --git a/klippy/msgproto.py b/klippy/msgproto.py
index 5a4233e7..177dbf7b 100644
--- a/klippy/msgproto.py
+++ b/klippy/msgproto.py
@@ -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 []
diff --git a/klippy/parsedump.py b/klippy/parsedump.py
index 380446eb..6d419183 100755
--- a/klippy/parsedump.py
+++ b/klippy/parsedump.py
@@ -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:]