-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathELMDevice.py
168 lines (124 loc) · 4.1 KB
/
ELMDevice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from ELMCommands import ELMCommands
from OBDParameters import OBDParameters
class ELMDevice:
def __init__(self, adapter):
self.adapter = adapter
self.pidflags = ''
def __configure(self):
self.__getRaw(ELMCommands.HardReset)
self.__getRaw(ELMCommands.EnableAdaptiveTiming)
self.__getRaw(ELMCommands.DisableLineFeed)
self.__getRaw(ELMCommands.DisableEcho)
self.__getRaw(ELMCommands.DisableHeaders)
self.__getRaw(ELMCommands.DisableSpaces)
def open(self):
result = self.adapter.open()
if (result is True):
self.__configure()
return True
return result
def close(self):
self.adapter.close()
# TODO: remove when not needed
def test(self):
print self.getTroubleCodes()
def getTroubleCodes(self):
result = []
output = self.__getRaw(OBDParameters.GetDiagnosticTrouleCodes)
output = output.strip()
if (output == 'NO DATA'):
return result
frames = output.split('\r')
for frame in frames:
codes = self.__extractTroubleCodes(frame)
for code in codes:
result.append(code)
return result
def __extractTroubleCodes(self, frame):
rawCodes = []
result = []
if (frame[0:2] != '43'):
raise BaseException('Expected mode 03 frame, received ' + frame)
for i in [2, 6, 10]:
code = frame[i : i + 4]
if (code == '0000'):
continue
rawCodes.append(code)
for code in rawCodes:
result.append(self.__processRawTroubleCode(code))
return result
def __processRawTroubleCode(self, code):
binaryCode = ''
for i in range(0, 4):
binaryCode += bin(int(code[i], 16))[2:].zfill(4)
byteA = binaryCode[:8]
byteB = binaryCode[8:]
charTemplate = {
'00' : 'P',
'01' : 'C',
'10' : 'B',
'11' : 'U'
}
result = charTemplate[byteA[:2]]
result += str(int(byteA[2:4], 2))
result += str(int(byteA[4:], 2))
result += str(int(byteB[:4], 2))
result += str(int(byteB[4:], 2))
return result
def __getRaw(self, command):
return self.adapter.execute(command)
def __getBytes(self, command):
if (len(command) < 4):
raise BaseException("Minimum command lenght: 4")
inMode = command[1:1]
inPID = command[2:2]
result = self.adapter.execute(command)
result = result.strip('\r')
if (result == 'NO DATA'):
raise BaseException("No data for parameter: " + command)
if (result == '?'):
raise BaseException("Unknown command: " + command)
if ((len(result) - 4) / 2 != OBDParameters.Descriptions[command]['bytes']):
raise BaseException("Incorrect result length: " + str(len(result)))
outMode = command[1:1]
outPID = command[2:2]
if (inMode != outMode):
raise BaseException("Incorrect mode returned: %s, expected %s, buffer: %s" % (inMode, outMode, result))
if (inPID != outPID):
raise BaseException("Incorrect PID returned: %s, expected %s, buffer: %s" % (inMode, outMode, result))
return result[4:]
def isSupported(self, pid):
if (not self.pidflags):
result = self.__getBytes(OBDParameters.SupportedPIDs)
for c in result:
self.pidflags += bin(int(c, 16))[2:].zfill(4)
return int(self.pidflags[OBDParameters.Descriptions[pid]['index']]) == 1
def getDeviceInformation(self):
return self.__getRaw(ELMCommands.GetDeviceInformation)
def clearTroubleCodes(self):
result = self.__getRaw(OBDParameters.ClearTroubleCodes)
if (result.upper() == 'OK'):
return True
return False
def getEngineLoad(self):
result = self.__getBytes(OBDParameters.EngineLoad)
return int(result, 16) * 100 / 255
def getRPM(self):
result = self.__getBytes(OBDParameters.RPM)
a = int(result[0:2], 16)
b = int(result[2:4], 16)
return ((a * 256) + b) / 4
def getThrottlePosition(self):
result = self.__getBytes(OBDParameters.ThrottlePosition)
return int(result, 16) * 100 / 255
def getEngineTemperature(self):
result = self.__getBytes(OBDParameters.EngineTemperature)
return int(result, 16) - 40
def getMAFAirFlow(self):
result = self.__getBytes(OBDParameters.MAFAirFlow)
a = int(result[0:2], 16)
b = int(result[2:4], 16)
return ((a * 256) + b) / 100
def getVehicleSpeed(self):
result = self.__getBytes(OBDParameters.VehicleSpeed)
return int(result, 16)