From c4c472bbfed04a57c8aa1f66cf9a7d34b20bc973 Mon Sep 17 00:00:00 2001 From: Pier-Yves Lessard Date: Tue, 15 Apr 2025 22:00:38 -0400 Subject: [PATCH 1/2] doc update --- doc/source/index.rst | 2 +- doc/source/udsoncan/examples.rst | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/doc/source/index.rst b/doc/source/index.rst index 5c8d7b8..c1f0b84 100755 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -50,6 +50,6 @@ Here is an example of code to give an insight of the grammar. client.ecu_reset(ECUReset.ResetType.hardReset) # HardReset = 0x01 except NegativeResponseException as e: print('Server refused our request for service %s with code "%s" (0x%02x)' % (e.response.service.get_name(), e.response.code_name, e.response.code)) - except InvalidResponseException, UnexpectedResponseException as e: + except (InvalidResponseException, UnexpectedResponseException) as e: print('Server sent an invalid payload : %s' % e.response.original_payload) diff --git a/doc/source/udsoncan/examples.rst b/doc/source/udsoncan/examples.rst index cc391c1..fb23804 100755 --- a/doc/source/udsoncan/examples.rst +++ b/doc/source/udsoncan/examples.rst @@ -6,45 +6,48 @@ Examples Different layers of intelligence (1 to 4) ----------------------------------------- -In the following examples, we will request an ECU reset in 4 different ways. We will start by crafting a binary payload manually, then we will add a layer of interpretation making the code more comprehensive each time. +In the following examples, we will request aa Routine Start with the RoutineControl service in 4 different ways. +We will start by crafting a binary payload manually, then we will add a layer of interpretation making the code more comprehensive each time. 1. Raw Connection ################# .. code-block:: python - my_connection.send(b'\x11\x01\x77\x88\x99') # Sends ECU Reset, with subfunction = 1 + my_connection.send(b'\x31\x01\x12\x34') # Sends RoutineControl, with ControlType=1, Routine ID=0x1234 payload = my_connection.wait_frame(timeout=1) - if payload == b'\x51\x01': + if payload == b'\x71\x01\x12\x34': print('Success!') else: - print('Reset failed') + print('Start Routine 0x1234 failed') 2. Request and Responses ######################## .. code-block:: python - req = Request(services.ECUReset, subfunction=1, data=b'\x77\x88\x99') + req = Request(services.RoutineControl, control_type=1, routine_id=0x1234) # control_type=1 --> StartRoutine my_connection.send(req.get_payload()) payload = my_connection.wait_frame(timeout=1) response = Response.from_payload(payload) - if response.service == service.ECUReset and response.code == Response.Code.PositiveResponse and response.data == b'\x01': + if response.service == service.RoutineControl and response.code == Response.Code.PositiveResponse and response.data == b'\x01\x12\x34': print('Success!') else: - print('Reset failed') + print('Start Routine 0x1234 failed') 3. Services ########### .. code-block:: python - req = services.ECUReset.make_request(reset_type=1, data=b'\x77\x88\x99') + req = services.RoutineControl.make_request(control_type=services.RoutineControl.ControlType.startRoutine, routine_id=0x1234) my_connection.send(req.get_payload()) payload = my_connection.wait_frame(timeout=1) response = Response.from_payload(payload) services.ECUReset.interpret_response(response) - if response.service == service.ECUReset and response.code == Response.Code.PositiveResponse and response.service_data.reset_type_echo == 1: + if ( response.service == service.RoutineControl and response.code == Response.Code.PositiveResponse + and response.service_data.control_type_echo == 1 + and response.service_data.routine_id_echo == 0x1234): print('Success!') else: print('Reset failed') @@ -55,9 +58,9 @@ In the following examples, we will request an ECU reset in 4 different ways. We .. code-block:: python try: - client.ecu_reset(reset_type=1, data=b'\x77\x88\x99') + response = client.start_routine(routine_id=0x1234) # control_type_echo and routine_id_echo are validated by the client. print('Success!') - except: + except Exception: print('Reset failed') ----- @@ -73,7 +76,7 @@ Note that, in order to run this code, both ``python-can`` and ``can-isotp`` must .. code-block:: python import can - import can.interfaces.vector import VectorBus + from can.interfaces.vector import VectorBus from udsoncan.connections import PythonIsoTpConnection from udsoncan.client import Client import udsoncan.configs @@ -219,7 +222,7 @@ This example shows how to configure the client with a DID configuration and requ 'default' : '>H', # Default codec is a struct.pack/unpack string. 16bits little endian 0x1234 : MyCustomCodecThatShiftBy4, # Uses own custom defined codec. Giving the class is ok 0x1235 : MyCustomCodecThatShiftBy4(), # Same as 0x1234, giving an instance is good also - 0xF190 : udsoncan.AsciiCodec(15) # Codec that read ASCII string. We must tell the length of the string + 0xF190 : udsoncan.AsciiCodec(17) # Codec that read ASCII string. We must tell the length of the string } # IsoTPSocketconnection only works with SocketCAN under Linux. Use another connection if needed. @@ -230,7 +233,7 @@ This example shows how to configure the client with a DID configuration and requ # Or, if a single DID is expected, a shortcut to read the value of the first DID vin = client.read_data_by_identifier_first(0xF190) - print(vin) # 'ABCDE0123456789' (15 chars) + print(vin) # 'ABCDEFG0123456789' (17 chars) ----- From df5e589153ddea31f773620285a66c6eb816b149 Mon Sep 17 00:00:00 2001 From: Pier-Yves Lessard Date: Tue, 15 Apr 2025 22:03:08 -0400 Subject: [PATCH 2/2] forgot a print --- doc/source/udsoncan/examples.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/source/udsoncan/examples.rst b/doc/source/udsoncan/examples.rst index fb23804..fefacf6 100755 --- a/doc/source/udsoncan/examples.rst +++ b/doc/source/udsoncan/examples.rst @@ -19,7 +19,7 @@ We will start by crafting a binary payload manually, then we will add a layer of if payload == b'\x71\x01\x12\x34': print('Success!') else: - print('Start Routine 0x1234 failed') + print('Start of routine 0x1234 failed') 2. Request and Responses ######################## @@ -33,7 +33,7 @@ We will start by crafting a binary payload manually, then we will add a layer of if response.service == service.RoutineControl and response.code == Response.Code.PositiveResponse and response.data == b'\x01\x12\x34': print('Success!') else: - print('Start Routine 0x1234 failed') + print('Start of routine 0x1234 failed') 3. Services ########### @@ -50,7 +50,7 @@ We will start by crafting a binary payload manually, then we will add a layer of and response.service_data.routine_id_echo == 0x1234): print('Success!') else: - print('Reset failed') + print('Start of routine 0x1234 failed') 4. Client ######### @@ -61,7 +61,7 @@ We will start by crafting a binary payload manually, then we will add a layer of response = client.start_routine(routine_id=0x1234) # control_type_echo and routine_id_echo are validated by the client. print('Success!') except Exception: - print('Reset failed') + print('Start of routine 0x1234 failed') -----