Python is widely used in automotive engineering to automate diagnostic requests, validate ECU behavior and collect repeatable test evidence. A Python UDS application can request a diagnostic session, read data identifiers, interpret negative responses and execute test sequences. However, the diagnostic service layer is only one part of the system: addressing, transport, timing and ECU-specific configuration must also be correct.
This guide explains the architecture and workflow of a Python-based UDS client, shows how a diagnostic transaction is structured, and describes the checks required before using an implementation with a real ECU. Examples are illustrative. Always follow the ECU diagnostic specification, OEM requirements and project-approved tool configuration.
1. Understand the UDS architecture
A Python diagnostic application commonly contains three distinct layers:
- UDS service layer: Builds service requests and interprets positive and negative diagnostic responses.
- Transport layer: Transfers UDS payloads over a configured transport, commonly ISO-TP over CAN or DoIP over Ethernet.
- ECU and project configuration: Defines addressing, supported services, session rules, timing, data identifiers and security policy.
Keeping these responsibilities separate makes faults easier to isolate. For example, a timeout may be caused by an incorrect CAN identifier, transport configuration, ECU response timing or application-level behavior. Treating all of these as a generic “UDS failure”.
2. End-to-end data flow
The diagram shows a logical flow, not a mandatory product architecture. The actual transport adapter depends on the interface hardware, operating system, network configuration and selected Python library.
Before implementation, document:
- Physical or functional addressing and the applicable request/response identifiers.
- CAN addressing and ISO-TP parameters, or DoIP logical addresses and routing activation requirements.
- UDS timing configuration, including P2/P2* handling and tester activity policy.
- Supported sessions, services, DIDs, routines and ECU-specific preconditions.
- The versions of Python, diagnostic libraries, interface drivers and configuration files.
3. Python libraries and their responsibilities
Commonly used Python packages include:
| Package | Typical responsibility |
|---|---|
udsoncan | UDS service requests, responses and client behavior |
python-can | Access to supported CAN interfaces |
can-isotp | ISO-TP transport over CAN |
| DoIP-capable library or project adapter | Diagnostic communication over automotive Ethernet |
These packages are not interchangeable. A UDS client does not automatically configure the CAN interface, ISO-TP addressing or DoIP routing. Check the library documentation and supported versions before selecting a combination.
A typical CAN-based stack can be represented as:
Test sequence
|
v
UDS client (service layer)
|
v
ISO-TP transport
|
v
CAN interface driver
|
v
CAN network and ECU
For Ethernet diagnostics, the transport path changes to DoIP and the relevant Ethernet interface and addressing configuration.
4. Example: organizing a UDS client
The following is a structural example using a UDS client API. connection represents a transport connection that has already been configured for the target ECU. Its construction is hardware- and project-dependent, so it is intentionally kept separate from the service calls.
from udsoncan.client import Client
from udsoncan.configs import default_client_config
# Create and configure the transport connection separately.
# It must match the target ECU's addressing and transport settings.
connection = create_project_transport_connection()
with Client(connection, config=default_client_config) as client:
# Request Extended Diagnostic Session.
client.change_session(0x03)
# Read a project-approved data identifier.
response = client.read_data_by_identifier(0xF190)
print("VIN response:", response)
This example illustrates the separation between transport setup and UDS service execution. The connection factory is a project-specific placeholder, not a function supplied by udsoncan. Confirm the exact API and configuration options against the installed library version before running or adapting the example.
The DID 0xF190 is commonly associated with VIN in UDS profiles, but support, response length and access conditions must still be confirmed for the target ECU. Do not assume every ECU supports the same identifiers.
5. Understand a diagnostic transaction
A UDS transaction should be reviewed at both the service and transport levels.
For example, a request to enter Extended Diagnostic Session is:
Tester request: 10 03
ECU response: 50 03 ...
Here, 0x10 is DiagnosticSessionControl, 0x03 is the requested session type, and 0x50 is the positive response SID. The remaining response bytes, when present, carry timing information that must be decoded according to the applicable UDS specification and ECU implementation.
A client should not treat transmission as proof that a session transition succeeded. It must evaluate the response, record the transaction and apply the configured timing rules.
A typical read-data transaction uses service 0x22:
Tester request: 22 F1 90
ECU response: 62 F1 90 ...
The positive response SID is 0x62; the response echoes the requested DID before the returned data. The meaning and encoding of the data bytes are defined by the ECU diagnostic data specification.
6. Timing, pending responses and session maintenance
A robust client must distinguish between different timing mechanisms:
- P2Server: The server's applicable initial response timing.
- P2*Server: The extended response timing used when the server indicates that processing is continuing.
- S3Server: The session inactivity timing behavior that can cause a non-default session to time out.
- ISO-TP timing: Transport-layer flow control and separation timing, which are distinct from UDS application timing.
When the ECU sends a ResponsePending negative response, the payload has the form:
7F <original SID> 78
NRC 0x78 means the request is still being processed; it is not the same as a final rejection. The tester must continue waiting according to the applicable P2* rules and its diagnostic stack configuration.
TesterPresent (0x3E) may be used to maintain diagnostic activity where required. Its scheduling, addressing and sub-function behavior must follow the ECU and project requirements. Do not use arbitrary fixed sleeps as a substitute for protocol timing.
7. Negative response handling
A UDS client should preserve and report the negative response code (NRC), original service identifier and request context. Some commonly encountered NRCs are:
| NRC | General meaning | Investigation |
|---|---|---|
0x11 | Service not supported | Check whether the ECU implements the requested SID |
0x12 | Sub-function not supported | Check the requested sub-function |
0x13 | Incorrect message length or invalid format | Verify payload length and encoding |
0x22 | Conditions not correct | Check ECU state and service preconditions |
0x31 | Request out of range | Verify the DID, routine or parameter range |
0x33 | Security access denied | Check the configured security state and authorization |
0x78 | Response pending | Continue waiting using the applicable extended timing |
The table is not exhaustive. NRC support and exact behavior depend on the UDS edition and ECU implementation. A useful test report should retain the raw response rather than recording only a decoded text message.
8. Build repeatable diagnostic test automation
A maintainable Python test should separate setup, execution, verdict and cleanup.
- Load configuration: Read the approved ECU variant, addressing, transport parameters and diagnostic data definitions.
- Establish communication: Initialize the interface and transport, then confirm that the target is reachable.
- Enter the required session: Request the session and verify the ECU response.
- Perform the diagnostic operation: Execute only services authorized for the test and current ECU state.
- Validate the result: Check response SID, echoed parameters, data length, decoded values and timing.
- Capture evidence: Store timestamps, raw request/response payloads, NRCs and software/configuration versions.
- Clean up: Close the diagnostic client and transport, and return the ECU to the required state if the test specification calls for it.
For automated execution, use explicit verdicts such as PASS, FAIL, NOT EXECUTED and INCONCLUSIVE. A missing response should not be reported as the same failure as a valid negative response.
9. Verification matrix
| Test area | Stimulus or condition | Evidence to capture |
|---|---|---|
| Nominal request | Supported service and valid ECU state | Positive response and decoded result |
| Unsupported service | Request an unimplemented SID | Correct negative response |
| Invalid format | Malformed or incorrect-length request | NRC and raw response |
| Session transition | Request supported diagnostic sessions | Session response and service availability |
| Timing | Delayed response or ResponsePending | P2/P2* behavior and timestamps |
| Session timeout | Remain inactive for the configured interval | Return-to-default behavior |
| TesterPresent | Send according to configured policy | Session maintenance behavior |
| Multi-frame transport | Request or response exceeding single-frame capacity | Segmentation, flow control and reassembly |
| Communication loss | Disconnect or interrupt the interface | Timeout classification and recovery |
| ECU reset | Reset during or after a diagnostic operation | Reconnection and post-reset state |
| Cleanup | End the test or trigger an exception | Closed resources and defined ECU state |
Link every test to a requirement or project acceptance criterion. Record the hardware, ECU software, database, Python package and test-script versions so that a result can be reproduced.
10. Failure analysis and common mistakes
Common implementation mistakes include:
- Treating a UDS library as if it also configures the physical interface and transport.
- Using CAN identifiers, logical addresses or DIDs copied from another ECU.
- Mixing ISO-TP transport timeouts with UDS P2/P2* or S3 session timing.
- Treating NRC
0x78as a final failure. - Assuming a positive response to session control grants SecurityAccess.
- Ignoring multi-frame segmentation and flow-control behavior.
- Failing to close the transport after an exception.
- Logging sensitive security material or proprietary diagnostic data in shared reports.
For fault isolation, move from the outside toward the application: check power and physical connectivity, verify the network interface, inspect transport addressing and segmentation, then examine UDS service behavior and ECU preconditions. Preserve raw traces before filtering or decoding them.
11. Security and project controls
Diagnostic automation can invoke operations that alter ECU configuration, clear fault information, execute routines or program memory. Restrict scripts to authorized environments and approved test cases.
Do not publish OEM credentials, seed/key algorithms, proprietary DIDs, memory addresses or confidential traces. SecurityAccess is ECU-specific; use only authorized project mechanisms and credentials. Apply appropriate access controls to test logs and generated reports.
12. Engineering checklist
- UDS standard edition and project diagnostic profile are identified.
- Transport type and addressing are verified for the target ECU.
- Python, package, driver and configuration versions are recorded.
- Session, timing and TesterPresent behavior are configured.
- Supported services and DIDs are taken from approved ECU documentation.
- Positive and negative responses are validated.
- Multi-frame transport, timeout and recovery paths are tested.
- Raw communication evidence and timestamps are captured.
- Sensitive credentials and proprietary data are protected.
- Test cleanup and ECU post-test state are defined.
13. Check your understanding
- Why should the UDS service layer be separated from the transport layer?
- What additional configuration is needed before a Python UDS client can communicate with a real ECU?
- What does NRC
0x78indicate? - Why should ISO-TP timing and UDS P2/P2* timing be analyzed separately?
- Why is a positive DiagnosticSessionControl response not proof that SecurityAccess has been granted?
Answers
- It separates diagnostic service behavior from network transport, making configuration and fault isolation clearer.
- The target's addressing, interface, transport parameters, timing and ECU-specific diagnostic configuration must be supplied.
- The ECU is still processing the request and the tester should continue waiting according to the applicable extended timing rules.
- They belong to different protocol layers and indicate different classes of delay or failure.
- Session control and SecurityAccess are separate mechanisms; protected services may require an additional authorized security procedure.
Related articles
- UDS Protocol Explained
- UDS Services and Service Identifiers
- UDS SecurityAccess (0x27)
- Diagnostic Session Control (0x10)
- Python-CAN: A Practical Automotive Engineering Guide
- Automated Test Report Generation
Further reading
- udsoncan documentation
- python-can documentation
- can-isotp documentation
- ASAM standards
- ISO 14229 series — Unified diagnostic services
Standards note: Confirm the ISO 14229 edition and project-specific diagnostic profile applicable to the ECU under test. OEM and program requirements may select an earlier edition or add implementation-specific constraints.
