Skip to content

Commit b2e43b3

Browse files
author
Brendan Whitfield
committed
got query_DTC returning cleanly with obdsim
1 parent 4b9cf75 commit b2e43b3

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

obd/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160

161161
__mode3__ = [
162162
# sensor name description mode cmd bytes decoder
163-
OBDCommand("GET_DTC" , "Get DTCs" , "03", "" , 6, noop , True),
163+
OBDCommand("GET_DTC" , "Get DTCs" , "03", "" , 6, dtc_frame , True),
164164
]
165165

166166
__mode4__ = [

obd/decoders.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -343,24 +343,36 @@ def describeCode(code):
343343
'''
344344

345345
# converts 2 bytes of hex into a DTC code
346-
def dtc(_hex):
346+
def single_dtc(_hex):
347+
348+
if _hex == "0000":
349+
return None
350+
347351
dtc = ""
348-
bits = bitstring(_hex[0])
352+
bits = bitstring(_hex[0], 4)
349353

350354
dtc += ['P', 'C', 'B', 'U'][unbin(bits[0:2])]
351355
dtc += str(unbin(bits[2:4]))
352356
dtc += _hex[1:4]
353357

354-
return (dtc, Unit.NONE)
358+
return dtc
355359

356360
# converts a frame of 2-byte DTCs into a list of DTCs
361+
# example input = 01 04 80 03 41 23
362+
# [DTC] [DTC] [DTC]
357363
def dtc_frame(_hex):
358364
codes = []
359-
for n in range(3):
365+
for n in range(0, 12, 4):
366+
dtc = single_dtc(_hex[n:n+4])
367+
368+
if dtc is not None:
369+
370+
# pull a description if we have one
371+
if dtc in DTC:
372+
dtc += ": %s" % DTC[dtc]
373+
else:
374+
dtc += ": unknown error code"
360375

361-
start = 4 * n
362-
end = start + 4
363-
364-
codes.append(dtc(_hex[start:end]))
376+
codes.append(dtc)
365377

366378
return (codes, Unit.NONE)

obd/elm327.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ def __send(self, cmd, delay=None):
287287
self.__write(cmd)
288288

289289
if delay is not None:
290+
debug("wait: %d seconds" % delay)
290291
time.sleep(delay)
291292

292293
return self.__read()

obd/obd.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,25 @@ def query(self, c, force=False):
184184
def query_DTC(self):
185185
""" read all DTCs """
186186

187-
n = self.query(commands.STATUS).value.DTC_count;
187+
status = self.query(commands.STATUS);
188+
189+
if status.is_null():
190+
debug("Failed to retrieve number of DTCs", True)
191+
return []
192+
193+
n = status.value.DTC_count
188194
n = n if (n < 128) else 0 # if this number is over 128, it's invalid
189195

190196
codes = [];
191197

192198
while n > 0:
193-
current_codes = self.query(commands.GET_DTC).value
194-
codes += current_codes
195-
n -= len(current_codes)
199+
get_dtc = self.query(commands.GET_DTC)
200+
201+
if get_dtc.is_null():
202+
debug("Failed to retrieve DTCs", True)
203+
break
204+
205+
codes += get_dtc.value
206+
n -= len(get_dtc.value)
196207

197208
return codes

0 commit comments

Comments
 (0)