Skip to content

Commit be0b818

Browse files
committed
Wait 1 second before attempting to receive a response from a command. Initial testing indicates it can take a moment for a response to be ready after a command is sent
1 parent 332fd66 commit be0b818

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

shadetree/obd/scanner.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def current_engine_coolant_temperature(self):
6868
"""
6969
self.send(commands.CURRENT_ENGINE_COOLANT_TEMP_COMMAND)
7070
response = self.receive()
71-
response_data = response.split(' ')[-1]
71+
response_data = response.strip().split(' ')[-1]
7272
#The data returned in the OBD response is in hexadecimal with a zero offset to account for negative temperatures
7373
#To return the current temperature in degrees Celsius, we must first convert to decimal and then subtract 40
7474
#to account for the zero offset.
@@ -81,7 +81,7 @@ def current_engine_oil_temperature(self):
8181
"""
8282
self.send(commands.CURRENT_ENGINE_OIL_TEMP_COMMAND)
8383
response = self.receive()
84-
response_data = response.split(' ')[-1]
84+
response_data = response.strip().split(' ')[-1]
8585
#The data returned in the OBD response is in hexadecimal with a zero offset to account for negative temperatures
8686
#To return the current temperature in degrees Celsius, we must first convert to decimal and then subtract 40
8787
#to account for the zero offset.
@@ -94,9 +94,9 @@ def current_engine_rpm(self):
9494
"""
9595
self.send(commands.CURRENT_ENGINE_RPM)
9696
response = self.receive()
97-
response_data = response.split(' ')
98-
if len(response_data) == 4:
99-
rpm = (int(response.split(' ')[-2], 16) * 256 + int(response.split(' ')[-1], 16)) / 4
97+
response_data = response.strip().split(' ')
98+
if len(response_data) >= 2:
99+
rpm = (int(response_data[-2], 16) * 256 + int(response_data[-1], 16)) / 4
100100
return rpm
101101
else:
102102
return None
@@ -116,7 +116,7 @@ def fuel_type(self):
116116
"""
117117
self.send(commands.FUEL_TYPE_COMMAND)
118118
response = self.receive()
119-
response_data = response.split(' ')[-1]
119+
response_data = response.strip().split(' ')[-1]
120120
return FUEL_TYPE_DESCRIPTION.get(int(response_data, 16))
121121

122122
def echo_off(self):
@@ -143,6 +143,7 @@ def initialize(self):
143143
:return:
144144
"""
145145
self.reset()
146+
self.echo_off()
146147
self.send(elm327.SELECT_PROTOCOL_COMMAND)
147148
self.receive()
148149

@@ -152,6 +153,8 @@ def receive(self):
152153
:return: the data returned by the OBD-II Scanner
153154
"""
154155
if self.connected:
156+
#Wait a second for data to become available
157+
time.sleep(1)
155158
retry_number = 0
156159
value = ""
157160
while True:
@@ -183,7 +186,6 @@ def reset(self):
183186
"""
184187
if self.connected:
185188
self.send(elm327.RESET_COMMAND)
186-
time.sleep(1)
187189
self.elm_version = self.receive()
188190

189191
def send(self, data):

0 commit comments

Comments
 (0)