Skip to content

Commit

Permalink
uds drain before send and use has_obd()
Browse files Browse the repository at this point in the history
  • Loading branch information
gregjhogan committed Nov 6, 2019
1 parent f2cbec1 commit 501db8d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion examples/eps_read_software_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
if __name__ == "__main__":
address = 0x18da30f1 # Honda EPS
panda = Panda()
uds_client = UdsClient(panda, address, debug=False)
panda.set_safety_mode(Panda.SAFETY_ELM327)
uds_client = UdsClient(panda, address, bus=1 if panda.has_obd() else 0, debug=False)

print("tester present ...")
uds_client.tester_present()
Expand Down
24 changes: 13 additions & 11 deletions python/uds.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class MessageTimeoutError(Exception):

class NegativeResponseError(Exception):
def __init__(self, message, service_id, error_code):
super().__init__()
self.message = message
self.service_id = service_id
self.error_code = error_code
Expand Down Expand Up @@ -386,7 +387,7 @@ def __init__(self, panda, tx_addr: int, rx_addr: int=None, bus: int=0, timeout:
self.panda = panda
self.bus = bus
self.tx_addr = tx_addr
if rx_addr == None:
if rx_addr is None:
if tx_addr < 0xFFF8:
# standard 11 bit response addr (add 8)
self.rx_addr = tx_addr+8
Expand All @@ -407,13 +408,6 @@ def __init__(self, panda, tx_addr: int, rx_addr: int=None, bus: int=0, timeout:

def _can_thread(self, debug: bool=False):
try:
# allow all output
self.panda.set_safety_mode(0x1337)
# clear tx buffer
self.panda.can_clear(self.bus)
# clear rx buffer
self.panda.can_clear(0xFFFF)

while True:
# send
while not self.can_tx_queue.empty():
Expand All @@ -428,13 +422,21 @@ def _can_thread(self, debug: bool=False):
continue
if debug: print("CAN-RX: {} - {}".format(hex(self.rx_addr), hexlify(rx_data)))
self.can_rx_queue.put(rx_data)
else:

if len(msgs) == 0:
time.sleep(0.01)
finally:
self.panda.close()

# generic uds request
def _uds_request(self, service_type: SERVICE_TYPE, subfunction: int=None, data: bytes=None) -> bytes:
# throw away any stale data
while not self.can_rx_queue.empty():
try:
self.can_rx_queue.get(block=False)
except BaseException:
pass

req = bytes([service_type])
if subfunction is not None:
req += bytes([subfunction])
Expand All @@ -453,12 +455,12 @@ def _uds_request(self, service_type: SERVICE_TYPE, subfunction: int=None, data:
service_id = resp[1] if len(resp) > 1 else -1
try:
service_desc = SERVICE_TYPE(service_id).name
except Exception:
except BaseException:
service_desc = 'NON_STANDARD_SERVICE'
error_code = resp[2] if len(resp) > 2 else -1
try:
error_desc = _negative_response_codes[error_code]
except Exception:
except BaseException:
error_desc = resp[3:]
# wait for another message if response pending
if error_code == 0x78:
Expand Down

0 comments on commit 501db8d

Please sign in to comment.