Skip to content

Commit 7952a5c

Browse files
author
Brendan Whitfield
committed
passing actual bytearrays, using unhexlify
1 parent 3524fa4 commit 7952a5c

8 files changed

Lines changed: 13 additions & 15 deletions

File tree

obd/OBDCommand.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def __constrain_message_data(self, message):
105105
message.data = message.data[:self.bytes]
106106
else:
107107
# pad the right with zeros
108-
message.data += ([0] * (self.bytes - len(message.data)))
108+
message.data += (b'\x00' * (self.bytes - len(message.data)))
109109

110110

111111
def __str__(self):

obd/protocols/protocol.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# #
3030
########################################################################
3131

32-
from obd.utils import ascii_to_bytes, isHex, numBitsSet
32+
from obd.utils import isHex, numBitsSet
3333
from obd.debug import debug
3434

3535

@@ -56,7 +56,7 @@ class Frame(object):
5656
""" represents a single parsed line of OBD output """
5757
def __init__(self, raw):
5858
self.raw = raw
59-
self.data = []
59+
self.data = b''
6060
self.priority = None
6161
self.addr_mode = None
6262
self.rx_id = None
@@ -71,7 +71,7 @@ class Message(object):
7171
def __init__(self, frames):
7272
self.frames = frames
7373
self.ecu = ECU.UNKNOWN
74-
self.data = []
74+
self.data = b''
7575

7676
@property
7777
def tx_id(self):

obd/protocols/protocol_can.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# #
3030
########################################################################
3131

32+
from binascii import unhexlify
3233
from obd.utils import contiguous
3334
from .protocol import *
3435

@@ -63,7 +64,7 @@ def parse_frame(self, frame):
6364
if self.id_bits == 11:
6465
raw = "00000" + raw
6566

66-
raw_bytes = ascii_to_bytes(raw)
67+
raw_bytes = unhexlify(raw)
6768

6869
# check for valid size
6970

obd/protocols/protocol_legacy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# #
3030
########################################################################
3131

32+
from binascii import unhexlify
3233
from obd.utils import contiguous
3334
from .protocol import *
3435

@@ -46,7 +47,7 @@ def parse_frame(self, frame):
4647

4748
raw = frame.raw
4849

49-
raw_bytes = ascii_to_bytes(raw)
50+
raw_bytes = unhexlify(raw)
5051

5152
if len(raw_bytes) < 6:
5253
debug("Dropped frame for being too short")

obd/utils.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ def bytes_to_hex(bs):
8686
h += ("0" * (2 - len(bh))) + bh
8787
return h
8888

89-
def ascii_to_bytes(a):
90-
""" converts a string of hex to an array of integer byte values """
91-
return [ unhex(a[i:i+2]) for i in range(0, len(a), 2) ]
92-
9389
def bitstring(_hex, bits=None):
9490
b = bin(unhex(_hex))[2:]
9591
if bits is not None:

tests/test_OBDCommand.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ def test_call():
6262
# valid response size
6363
cmd = OBDCommand("", "", "0123", 4, decode_raw, ECU.ENGINE)
6464
r = cmd(messages)
65-
assert r.value == [0xBE, 0x1F, 0xB8, 0x11]
65+
assert r.value == b'\xBE\x1F\xB8\x11'
6666

6767
# response too short (pad)
6868
cmd = OBDCommand("", "", "0123", 5, decode_raw, ECU.ENGINE)
6969
r = cmd(messages)
70-
assert r.value == [0xBE, 0x1F, 0xB8, 0x11, 0x00]
70+
assert r.value == b'\xBE\x1F\xB8\x11\x00'
7171

7272
# response too long (clip)
7373
cmd = OBDCommand("", "", "0123", 3, decode_raw, ECU.ENGINE)
7474
r = cmd(messages)
75-
assert r.value == [0xBE, 0x1F, 0xB8]
75+
assert r.value == b'\xBE\x1F\xB8'
7676

7777

7878

tests/test_protocol_can.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def check_message(m, num_frames, tx_id, data):
2323
""" generic test for correct message values """
2424
assert len(m.frames) == num_frames
2525
assert m.tx_id == tx_id
26-
assert m.data == data
26+
assert m.data == bytes(data)
2727

2828

2929

tests/test_protocol_legacy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def check_message(m, n_frames, tx_id, data):
2020
""" generic test for correct message values """
2121
assert len(m.frames) == n_frames
2222
assert m.tx_id == tx_id
23-
assert m.data == data
23+
assert m.data == bytes(data)
2424

2525

2626
def test_single_frame():

0 commit comments

Comments
 (0)