UDS test example in ANDi scripts

UDS test script

  • Running the following script will send and parse the response of the following diagnostic jobs:

    -> Delete all active DTC -> Read all DTC -> Parse DTC status

  • “channel_obd” is the OBD channel

from globals import message_builder
from andi.uds import HsfzConnection, UdsClient
from andi.uds.exceptions import NegativeResponseException, InvalidResponseException, UnexpectedResponseException
from andi.uds.services import ECUReset
from time import sleep

ECU = 0x10
TESTER = 0xF4

message = message_builder.create_hsfz_message('channel_obd', 'channel_obd')
message.ip_header.ip_address_source = '192.168.0.1'
message.ip_header.ip_address_destination = '192.168.0.150'
message.tcp_header.auto_port = True
message.diag.source_address = TESTER
message.diag.target_address = ECU
conn = HsfzConnection(message)
with UdsClient(conn, request_timeout=60) as client:
    try:
        # Send a TesterPresent request to the target ECU
        client.tester_present()
        # Get DTC info by DTC number for target ECU
        print(client.get_dtc_extended_data_by_dtc_number(0x021000, 255, 200))
        # Read for target ECU all the Diagnostic Trouble Codes (DTCs) that have a status matching the given mask '0x01' (DTC failed at the time of DTC request)
        response = client.get_dtc_by_status_mask(0x01)
        for dtc in response.service_data.dtcs:
            # Print the DTC number
            print("DTC : %06X" % dtc.id)
        # Delete all active DTCs for target ECU
        response = client.clear_dtc()
        # ECU Soft Reset for target ECU
        client.ecu_reset(ECUReset.ResetType.softReset)
        sleep(2)
    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:
        print('Server sent an invalid payload : %s' % e.response.original_payload)
  • Note that ANDi lib provides three UDS connections, using HSFZ protocol, DoIP and ISO-TP.

  • The following script will do the same thing as the previous one but with DoIP instead of HSFZ.

from globals import message_builder
from andi.uds import DoIpConnection, UdsClient
from andi.uds.exceptions import NegativeResponseException, InvalidResponseException, UnexpectedResponseException
from andi.uds.services import ECUReset
from time import sleep

ECU = 0x10
TESTER = 0xF4

message = message_builder.create_doip_message('channel_obd', 'channel_obd')
message.ip_header.ip_address_source = '192.168.0.1'
message.ip_header.ip_address_destination = '192.168.0.150'
message.tcp_header.auto_port = True
message.source_address = TESTER
message.target_address = ECU
conn = DoIpConnection(message)
with UdsClient(conn, request_timeout=60) as client:
    try:
        # Send a TesterPresent request to the target ECU
        client.tester_present()
        # Get DTC info by DTC number for target ECU
        print(client.get_dtc_extended_data_by_dtc_number(0x021000, 255, 200))
        # Read for target ECU all the Diagnostic Trouble Codes (DTCs) that have a status matching the given mask '0x01' (DTC failed at the time of DTC request)
        response = client.get_dtc_by_status_mask(0x01)
        for dtc in response.service_data.dtcs:
            # Print the DTC number
            print("DTC : %06X" % dtc.id)
        # Delete all active DTCs for target ECU
        response = client.clear_dtc()
        # ECU Soft Reset for target ECU
        client.ecu_reset(ECUReset.ResetType.softReset)
        sleep(2)
    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:
        print('Server sent an invalid payload : %s' % e.response.original_payload)

Note

UdsClient class can be found under <https://udsoncan.readthedocs.io/en/latest/udsoncan/client.html>

  • The following script creates an ISO-TP connection, sends a TesterPresent UDS message, and prints the available DTCs:

from andi.isotp import IsoTpSocket
from andi.uds import IsoTpConnection, UdsClient
 
sock = IsoTpSocket(name='isotp', channel='ch', tx_id=0x123, rx_id=0x456, tx_address=0x78, rx_address=0x90)
 
conn = IsoTpConnection(sock)
 
with UdsClient(conn) as client:
    client.tester_present()
    print(client.get_dtc_by_status_mask(0xff).service_data.dtcs)
DTC

Diagnostic Trouble Codes.

OBD

On-board diagnostics (OBD) is an automotive term referring to a vehicle's self-diagnostic and reporting capability.