Skip to content

Commit 2e20002

Browse files
author
Brendan Whitfield
committed
handle DTC count byte in CAN GET_DTC responses
1 parent 2f611a6 commit 2e20002

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

obd/protocols/protocol_can.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ def create_message(self, frames, tx_id):
188188
# chop off the Mode/PID bytes based on the mode number
189189
mode = message.data_bytes[0]
190190
if mode == 0x43:
191-
# GET_DTC requests (mode 03) do not have a PID byte
192-
message.data_bytes = message.data_bytes[1:]
191+
192+
# fetch the DTC count, and use it as a length code
193+
num_dtc_bytes = message.data_bytes[1] * 2
194+
195+
# skip the PID byte and the DTC count,
196+
message.data_bytes = message.data_bytes[2:][:num_dtc_bytes]
197+
193198
else:
194199
# handles cases when there is both a Mode and PID byte
195200
message.data_bytes = message.data_bytes[2:]

tests/test_protocol_can.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ def test_multi_line():
133133
# MODE 03 COMMANDS (GET_DTC) RETURN NO PID BYTE
134134

135135
test_case = [
136-
"7E8 10 20 43 00 01 02 03 04",
137-
"7E8 21 05 06 07 08 09 0A 0B",
136+
"7E8 10 20 43 04 00 01 02 03",
137+
"7E8 21 04 05 06 07 08 09 0A",
138138
]
139139

140-
correct_data = list(range(12))
140+
correct_data = list(range(8))
141141

142142
r = p(test_case)
143143
assert len(r) == 1

0 commit comments

Comments
 (0)