Skip to content

UDS Link

The UDS Link provides a communication interface for Unified Diagnostic Services (UDS) over CAN. It handles the low-level communication details required for UDS messaging.

Quick Start

Basic Setup

from zeloscloud.links.can_link import CanLink
from zeloscloud.links.uds_link import UdsLink
from zeloscloud.task import Task

# Create task for async operations
with Task(name="task") as task:
    # Configure and create the CAN link
    can_config = {
        "channel": "vcan0",
        "interface": "socketcan",
        "can_filters": [
            # Optional: Make isotp frame parsing more efficient
            # {"can_id": 0x000, "can_mask": 0x7F0, "extended": False},
        ],
    }

    # Create CAN link with task loop
    with CanLink(config=can_config, loop=task.loop) as can_link:
        # Configure ISO-TP addressing
        isotp_addr = {
            "txid": 0x7D0,  # Request ID
            "rxid": 0x7D8,  # Response ID
        }

        # Create ISO-TP connection
        connection = UdsLink.can_connection(can_link, isotp_addr)

        # Create and use UDS link
        with UdsLink(name="uds-link", connection=connection) as link:
            # Link is now ready for use
            link.send(b"\x11\x01")  # Example: Send ECU reset command

Pytest Setup

import pytest
from zeloscloud.links import CanLink, UdsLink
from zeloscloud.task import Task

@pytest.fixture(scope="function")
def task():
    """Create task for async operations"""
    with Task(name="task") as task:
        yield task

@pytest.fixture(scope="function")
def can_link(task):
    """Create CAN link with virtual interface"""
    config = {
        "channel": "vcan0",
        "interface": "socketcan",
        "can_filters": []
    }
    with CanLink(config=config, loop=task.loop) as link:
        yield link

@pytest.fixture(scope="function")
def connection(can_link):
    """Create ISO-TP connection"""
    isotp_addr = {
        "txid": 0x123,  # Request ID
        "rxid": 0x456,  # Response ID
    }
    return UdsLink.can_connection(can_link, isotp_addr)

@pytest.fixture(scope="function")
def uds_link(connection):
    """Create UDS link"""
    with UdsLink(name="uds-link", connection=connection) as link:
        yield link

Configuration Options

Parameter Description Default
channel CAN interface name Required
interface Interface type (e.g., 'socketcan', 'virtual') Required
can_filters List of CAN filters []

ISO-TP Addressing Configuration

Parameter Description
txid Transmit CAN ID
rxid Receive CAN ID

Advanced CAN Configuration

can_config = {
    "channel": "can0",
    "interface": "socketcan",  # Use 'socketcan' for real hardware
    "can_filters": [
        {
            "can_id": 0x000,
            "can_mask": 0x7F0,
            "extended": False
        }
    ],
    "bitrate": 500000
}

Troubleshooting

Common Issues

  1. Connection Errors

    # Check if virtual CAN interface exists
    ip link show vcan0
    
    # Create virtual CAN interface if needed
    sudo modprobe vcan
    sudo ip link add dev vcan0 type vcan
    sudo ip link set up vcan0
    

  2. Real Hardware Setup

    # Set up physical CAN interface
    sudo ip link set can0 type can bitrate 500000
    sudo ip link set up can0
    

See also: - UDS Codec Documentation - CAN Link Documentation

API Reference

See zeloscloud.links.uds_link.UdsLink in the API Reference.