From 162f4853df6ca78146d3ba7a9c08c06e6e2b04c4 Mon Sep 17 00:00:00 2001 From: rbiasini Date: Sun, 13 Oct 2019 18:15:04 -0700 Subject: [PATCH] fix chr to bytes conversions (#298) --- examples/query_vin_and_stats.py | 2 +- python/isotp.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/query_vin_and_stats.py b/examples/query_vin_and_stats.py index 1dc181c57639f2..c1d58d0a17000d 100755 --- a/examples/query_vin_and_stats.py +++ b/examples/query_vin_and_stats.py @@ -12,7 +12,7 @@ def get_current_data_for_pid(pid): # 01 xx = Show current data - isotp_send(panda, b"\x01"+ (chr(pid)).encode("utf8"), 0x7e0) + isotp_send(panda, b"\x01"+ bytes([pid]), 0x7e0) return isotp_recv(panda, 0x7e8) def get_supported_pids(): diff --git a/python/isotp.py b/python/isotp.py index ac0f8c4a5414a1..ca518a06bcc93d 100644 --- a/python/isotp.py +++ b/python/isotp.py @@ -6,7 +6,7 @@ def msg(x): if DEBUG: print("S:", binascii.hexlify(x)) if len(x) <= 7: - ret = chr(len(x)).encode("utf8") + x + ret = bytes([len(x)]) + x else: assert False return ret.ljust(8, b"\x00") @@ -40,7 +40,7 @@ def isotp_recv_subaddr(panda, addr, bus, sendaddr, subaddr): dat = msg[3:] # 0 block size? - CONTINUE = chr(subaddr).encode("utf8") + b"\x30" + b"\x00"*6 + CONTINUE = bytes([subaddr]) + b"\x30" + b"\x00"*6 panda.can_send(sendaddr, CONTINUE, bus) idx = 1 @@ -68,22 +68,22 @@ def isotp_send(panda, x, addr, bus=0, recvaddr=None, subaddr=None): if len(x) <= 7 and subaddr is None: panda.can_send(addr, msg(x), bus) elif len(x) <= 6 and subaddr is not None: - panda.can_send(addr, chr(subaddr).encode("utf8") + msg(x)[0:7], bus) + panda.can_send(addr, bytes([subaddr]) + msg(x)[0:7], bus) else: if subaddr: - ss = (chr(subaddr) + chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF)).encode("utf8") + x[0:5] + ss = bytes([subaddr]) + bytes([0x10 + (len(x) >> 8)]) + bytes([len(x) & 0xFF]) + x[0:5] x = x[5:] else: - ss = (chr(0x10 + (len(x)>>8)) + chr(len(x)&0xFF)).encode("utf8") + x[0:6] + ss = bytes([0x10 + (len(x) >> 8)]) + bytes([len(x) & 0xFF]) + x[0:6] x = x[6:] idx = 1 sends = [] while len(x) > 0: if subaddr: - sends.append((((chr(subaddr) + chr(0x20 + (idx&0xF))).encode('utf8') + x[0:6]).ljust(8, b"\x00"))) + sends.append(((bytes([subaddr]) + bytes([0x20 + (idx & 0xF)]) + x[0:6]).ljust(8, b"\x00"))) x = x[6:] else: - sends.append(((chr(0x20 + (idx&0xF)).encode("utf8") + x[0:7]).ljust(8, b"\x00"))) + sends.append(((bytes([0x20 + (idx & 0xF)]) + x[0:7]).ljust(8, b"\x00"))) x = x[7:] idx += 1 @@ -107,7 +107,7 @@ def isotp_recv(panda, addr, bus=0, sendaddr=None, subaddr=None): else: msg = recv(panda, 1, addr, bus)[0] - if msg[0]&0xf0 == 0x10: + if msg[0] & 0xf0 == 0x10: # first tlen = ((msg[0] & 0xf) << 8) | msg[1] dat = msg[2:]