Skip to content

Commit fac9ee3

Browse files
author
Brendan Whitfield
committed
wrote more consise DTC decoders
1 parent cb5ccc1 commit fac9ee3

4 files changed

Lines changed: 42 additions & 80 deletions

File tree

‎obd/codes.py‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2323
###########################################################################
2424

25-
pcodes = {
25+
DTC_CODES = {
2626
"P0001": "Fuel Volume Regulator Control Circuit/Open",
2727
"P0002": "Fuel Volume Regulator Control Circuit Range/Performance",
2828
"P0003": "Fuel Volume Regulator Control Circuit Low",
@@ -2092,30 +2092,6 @@
20922092

20932093
}
20942094

2095-
pcode_classes = {
2096-
"P00XX": "Fuel and Air Metering and Auxiliary Emission Controls",
2097-
"P01XX": "Fuel and Air Metering",
2098-
"P02XX": "Fuel and Air Metering",
2099-
"P03XX": "Ignition System or Misfire",
2100-
"P04XX": "Auxiliary Emission Controls",
2101-
"P05XX": "Vehicle Speed, Idle Control, and Auxiliary Inputs",
2102-
"P06XX": "Computer and Auxiliary Outputs",
2103-
"P07XX": "Transmission",
2104-
"P08XX": "Transmission",
2105-
"P09XX": "Transmission",
2106-
"P0AXX": "Hybrid Propulsion",
2107-
"P10XX": "Manufacturer Controlled Fuel and Air Metering and Auxiliary Emission Controls",
2108-
"P11XX": "Manufacturer Controlled Fuel and Air Merering",
2109-
"P12XX": "Fuel and Air Merering",
2110-
"P13XX": "Ignition System or Misfire",
2111-
"P14XX": "Auxiliary Emission Controls",
2112-
"P15XX": "Vehicle Speed, Idle Control, and Auxiliary Inputs",
2113-
"P16XX": "Computer and Auxiliary Outputs",
2114-
"P17XX": "Transmission",
2115-
"P18XX": "Transmission",
2116-
"P19XX": "Transmission",
2117-
2118-
}
21192095

21202096
ptest= [
21212097
"DTCs",

‎obd/commands.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def compute(result):
9797
OBDCommand("OBD_STANDARDS" , "OBD Standards Compliance" , "01", "1C" , 1, noop ),
9898
OBDCommand("O2_SENSORS_ALT" , "O2 Sensors Present (alternate)" , "01", "1D" , 1, noop ),
9999
OBDCommand("AUX_INPUT_STATUS" , "Auxiliary input status" , "01", "1E" , 1, noop ),
100-
OBDCommand("RUN_TIME" , "Engine Run Time" , "01", "1F" , 2, sec_to_min ),
100+
OBDCommand("RUN_TIME" , "Engine Run Time" , "01", "1F" , 2, seconds ),
101101
#OBDCommand("RUN_TIME_MIL" , "Engine Run Time MIL" , "01", "4D" , sec_to_min ),
102102
]
103103

‎obd/decoders.py‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11

22

3-
from utils import Value, Unit
4-
5-
6-
def unhex(_hex):
7-
return int(_hex, 16)
8-
9-
def unbin(_bin):
10-
return int(_bin, 2)
11-
12-
def bitstring(_hex):
13-
return bin(unhex(_hex))[2:]
14-
3+
from utils import Value, Unit, unhex, unbin, bitstring
154

165

176

@@ -77,6 +66,11 @@ def maf(_hex):
7766
v = v / 100.0
7867
return Value(v, Unit.GRAM_P_SEC)
7968

69+
# 0 to 655.35 seconds
70+
def seconds(_hex):
71+
v = unhex(_hex)
72+
return Value(v, Unit.SECONDS)
73+
8074

8175
# these functions draw data from the same PID
8276
def mil(_hex):
@@ -87,9 +81,26 @@ def dtc_count(_hex):
8781
v = bitstring(_hex)
8882
return unbin(v[1:8])
8983

90-
91-
92-
93-
def special_PID_01(_hex):
94-
95-
84+
# converts 2 bytes of hex into a DTC code
85+
def dtc(_hex):
86+
dtc = ""
87+
bits = bitstring(_hex[0])
88+
89+
dtc += ['P', 'C', 'B', 'U'][unbin(bits[0:2]))]
90+
dtc += str(unbin(bits[2:4]))
91+
dtc += _hex[1:4]
92+
return dtc
93+
94+
# converts a frame of 2-byte DTCs into a list of DTCs
95+
def dtc_frame(_hex):
96+
code_length = 4 # number of hex chars consumed by one code
97+
size = len(_hex / 4) # number of codes defined in THIS FRAME (not total)
98+
codes = []
99+
for n in range(size):
100+
101+
start = code_length * n
102+
end = start + code_length
103+
104+
codes.append(dtc(_hex[start:end]))
105+
106+
return codes

‎obd/utils.py‎

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Unit:
77
NONE = None
88
BITSTRING = "Bit String"
9+
DTC = "Diagnostic Trouble Code"
910
PERCENT = "Percent"
1011
VOLT = "Volt"
1112
F = "F"
@@ -29,6 +30,16 @@ def __str__(self):
2930
return "%s %s" % (str(self.value), str(self.unit))
3031

3132

33+
def unhex(_hex):
34+
return int(_hex, 16)
35+
36+
def unbin(_bin):
37+
return int(_bin, 2)
38+
39+
def bitstring(_hex):
40+
return bin(unhex(_hex))[2:]
41+
42+
3243
def tryPort(portStr):
3344
"""returns boolean for port availability"""
3445
try:
@@ -71,39 +82,3 @@ def scanSerial():
7182
'''
7283

7384
return available
74-
75-
76-
77-
def hex_to_int(str):
78-
return int(str, 16)
79-
80-
81-
82-
def decrypt_dtc_code(code):
83-
"""Returns the 5-digit DTC code from hex encoding"""
84-
dtc = []
85-
current = code
86-
for i in range(0,3):
87-
if len(current)<4:
88-
raise "Tried to decode bad DTC: %s" % code
89-
90-
tc = hex_to_int(current[0]) #typecode
91-
tc = tc >> 2
92-
if tc == 0:
93-
type = "P"
94-
elif tc == 1:
95-
type = "C"
96-
elif tc == 2:
97-
type = "B"
98-
elif tc == 3:
99-
type = "U"
100-
else:
101-
raise tc
102-
103-
dig1 = str(hex_to_int(current[0]) & 3)
104-
dig2 = str(hex_to_int(current[1]))
105-
dig3 = str(hex_to_int(current[2]))
106-
dig4 = str(hex_to_int(current[3]))
107-
dtc.append(type+dig1+dig2+dig3+dig4)
108-
current = current[4:]
109-
return dtc

0 commit comments

Comments
 (0)