Skip to content

Commit

Permalink
Safe handling of dictionary lookup when decoding DTC
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperkarlsson committed Jul 3, 2015
1 parent 2b1448e commit d9b356f
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tool/modules/dcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@
0x7F: 'serviceNotSupportedInActiveSession'
}


def dcm_dtc(args):
"""
Fetches the Diagnostic Trouble Codes from a supported service (Mode $03)
Fetches and prints the Diagnostic Trouble Codes from a supported service (Mode $03)
:param args: A namespace containing src, dst and clear
"""
Expand All @@ -86,25 +87,25 @@ def decode_dtc(msg):
return

def dtc_type(x):
return {
0: 'P',
1: 'B',
2: 'C',
3: 'U',
}[x]
return {
0: 'P',
1: 'B',
2: 'C',
3: 'U',
}.get(x, "?")

dtc_msg = dtc_type(msg.data[3] & 0xF0 >> 4) + format(msg.data[3] & 0x0F, '01x') + format(msg.data[4], '02x')
print("DTC: {0}\n".format(dtc_msg))
return decode_dtc
dtc_type_char = dtc_type(msg.data[3] & 0xF0 >> 4)
print("DTC: {0}{1:01x}{2:02x}\n".format(dtc_type_char, msg.data[3] & 0x0F, msg.data[4]))

with CanActions(arb_id=send_arb_id) as can_wrap:
if clear:
can_wrap.send([0x01, 0x04])
print("Cleared DTCs and reset MIL")
can_wrap.send([0x01, 0x04])
print("Cleared DTCs and reset MIL")
else:
print("Fetching Diagnostic Trouble Codes")
can_wrap.send_single_message_with_callback([0x01, 0x03], decode_dtc)
time.sleep(1)
print("Fetching Diagnostic Trouble Codes")
can_wrap.send_single_message_with_callback([0x01, 0x03], decode_dtc)
time.sleep(1)


def dcm_discovery(args):
"""
Expand Down

0 comments on commit d9b356f

Please sign in to comment.