Skip to content

Commit

Permalink
Support isotp v2 (#196)
Browse files Browse the repository at this point in the history
- Added support for isotp v2.x
- Also added some missing static typing
  • Loading branch information
pylessard authored Jan 14, 2024
1 parent d089b40 commit 1aaf2e5
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 119 deletions.
17 changes: 11 additions & 6 deletions doc/source/udsoncan/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ In this example, we show how to use :class:`PythonIsoTpConnection<udsoncan.conne
Note that, in order to run this code, both ``python-can`` and ``can-isotp`` must be installed.

.. code-block:: python
from can.interfaces.vector import VectorBus
import can
import can.interfaces.vector import VectorBus
from udsoncan.connections import PythonIsoTpConnection
from udsoncan.client import Client
import udsoncan.configs
import isotp
# Refer to isotp documentation for full details about parameters
Expand All @@ -95,15 +97,18 @@ Note that, in order to run this code, both ``python-can`` and ``can-isotp`` must
'rate_limit_enable': False, # Disable the rate limiter
'rate_limit_max_bitrate': 1000000, # Ignored when rate_limit_enable=False. Sets the max bitrate when rate_limit_enable=True
'rate_limit_window_size': 0.2, # Ignored when rate_limit_enable=False. Sets the averaging window size for bitrate calculation when rate_limit_enable=True
'listen_mode': False # Does not use the listen_mode which prevent transmission.
'listen_mode': False, # Does not use the listen_mode which prevent transmission.
}
uds_config = udsoncan.configs.default_client_config.copy()
bus = VectorBus(channel=0, bitrate=500000) # Link Layer (CAN protocol)
notifier = can.Notifier(bus, [can.Printer()]) # Add a debug listener that print all messages
tp_addr = isotp.Address(isotp.AddressingMode.Normal_11bits, txid=0x123, rxid=0x456) # Network layer addressing scheme
stack = isotp.CanStack(bus=bus, address=tp_addr, params=isotp_params) # Network/Transport layer (IsoTP protocol)
stack.set_sleep_timing(0, 0) # Speed First (do not sleep)
#stack = isotp.CanStack(bus=bus, address=tp_addr, params=isotp_params) # isotp v1.x has no notifier support
stack = isotp.NotifierBasedCanStack(bus=bus, notifier=notifier, address=tp_addr, params=isotp_params) # Network/Transport layer (IsoTP protocol). Register a new listenenr
conn = PythonIsoTpConnection(stack) # interface between Application and Transport layer
with Client(conn, request_timeout=1) as client: # Application layer (UDS protocol)
with Client(conn, config=uds_config) as client: # Application layer (UDS protocol)
client.change_session(1)
# ...
Expand Down
8 changes: 4 additions & 4 deletions udsoncan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response
timeout_type_used = 'overall'
timeout_value = max(overall_timeout_time - time.time(), 0)

payload = self.conn.wait_frame(timeout=timeout_value, exception=True)
recv_payload = self.conn.wait_frame(timeout=timeout_value, exception=True)
except TimeoutException:
if timeout_type_used == 'single_request':
timeout_name_to_report = 'P2* timeout' if using_p2_star else 'P2 timeout'
Expand All @@ -2178,13 +2178,13 @@ def send_request(self, request: Request, timeout: int = -1) -> Optional[Response
except Exception as e:
raise e

if timed_out:
if timed_out or recv_payload is None:
if spr_used:
return None
raise TimeoutException('Did not receive response in time. %s time has expired (timeout=%.3f sec)' %
(timeout_name_to_report, timeout_value))

response = Response.from_payload(payload)
response = Response.from_payload(recv_payload)
self.last_response = response
self.logger.debug("Received response from server")

Expand Down
Loading

0 comments on commit 1aaf2e5

Please sign in to comment.