Skip to content

Commit

Permalink
Wait 1 second before attempting to receive a response from a command.…
Browse files Browse the repository at this point in the history
… Initial testing indicates it can take a moment for a response to be ready after a command is sent
  • Loading branch information
corbinbs committed Jul 20, 2014
1 parent 332fd66 commit be0b818
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions shadetree/obd/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def current_engine_coolant_temperature(self):
"""
self.send(commands.CURRENT_ENGINE_COOLANT_TEMP_COMMAND)
response = self.receive()
response_data = response.split(' ')[-1]
response_data = response.strip().split(' ')[-1]
#The data returned in the OBD response is in hexadecimal with a zero offset to account for negative temperatures
#To return the current temperature in degrees Celsius, we must first convert to decimal and then subtract 40
#to account for the zero offset.
Expand All @@ -81,7 +81,7 @@ def current_engine_oil_temperature(self):
"""
self.send(commands.CURRENT_ENGINE_OIL_TEMP_COMMAND)
response = self.receive()
response_data = response.split(' ')[-1]
response_data = response.strip().split(' ')[-1]
#The data returned in the OBD response is in hexadecimal with a zero offset to account for negative temperatures
#To return the current temperature in degrees Celsius, we must first convert to decimal and then subtract 40
#to account for the zero offset.
Expand All @@ -94,9 +94,9 @@ def current_engine_rpm(self):
"""
self.send(commands.CURRENT_ENGINE_RPM)
response = self.receive()
response_data = response.split(' ')
if len(response_data) == 4:
rpm = (int(response.split(' ')[-2], 16) * 256 + int(response.split(' ')[-1], 16)) / 4
response_data = response.strip().split(' ')
if len(response_data) >= 2:
rpm = (int(response_data[-2], 16) * 256 + int(response_data[-1], 16)) / 4
return rpm
else:
return None
Expand All @@ -116,7 +116,7 @@ def fuel_type(self):
"""
self.send(commands.FUEL_TYPE_COMMAND)
response = self.receive()
response_data = response.split(' ')[-1]
response_data = response.strip().split(' ')[-1]
return FUEL_TYPE_DESCRIPTION.get(int(response_data, 16))

def echo_off(self):
Expand All @@ -143,6 +143,7 @@ def initialize(self):
:return:
"""
self.reset()
self.echo_off()
self.send(elm327.SELECT_PROTOCOL_COMMAND)
self.receive()

Expand All @@ -152,6 +153,8 @@ def receive(self):
:return: the data returned by the OBD-II Scanner
"""
if self.connected:
#Wait a second for data to become available
time.sleep(1)
retry_number = 0
value = ""
while True:
Expand Down Expand Up @@ -183,7 +186,6 @@ def reset(self):
"""
if self.connected:
self.send(elm327.RESET_COMMAND)
time.sleep(1)
self.elm_version = self.receive()

def send(self, data):
Expand Down

0 comments on commit be0b818

Please sign in to comment.