automotive is a Rust crate
and Python library for CAN communication, ISO-TP and DoIP
transports, and UDS, XCP, and CCP clients. The Rust API is async; Python offers
both synchronous and asyncio APIs.
Rust:
cargo add automotive --features default-adaptersPython:
uv add automotiveautomotive::can provides shared CAN types and an async adapter interface for
sending and receiving Classical CAN and CAN FD frames.
Rust adapter support is opt-in; no hardware adapters are enabled by default.
| Adapter | Platforms | Feature |
|---|---|---|
| SocketCAN | Linux | socketcan |
| comma.ai panda | Linux, macOS, Windows (USB) | panda |
| PEAK PCAN-USB FD family | Linux, macOS, Windows (raw USB) | peak |
| J2534 PassThru | Windows | j2534 |
| Vector XL | Windows x64 | vector-xl |
default-adapters enables panda and SocketCAN; all-adapters enables every
backend supported on the target platform. Adapter implementations and discovery
live in automotive::adapters.
adapters::get_adapter() opens the first available adapter. Send a frame and
receive frames as a stream:
use std::time::Duration;
use automotive::{
adapters::get_adapter,
can::{Frame, Identifier},
StreamExt,
};
#[tokio::main]
async fn main() -> automotive::Result<()> {
let adapter = get_adapter()?;
let mut frames = adapter.recv_for(Duration::from_millis(100));
let frame = Frame::builder(Identifier::standard(0x541)?)
.data(&[0xff; 8])
.build()?;
adapter.send(&frame).await?;
while let Some(frame) = frames.next().await {
println!("{frame:?}");
}
Ok(())
}send().await waits for transmit confirmation. Confirmation accuracy depends
on the adapter and driver; backends without hardware confirmation emulate it.
Receive streams include transmit echoes, marked with frame.loopback.
recv_for(duration) subscribes immediately and returns a stream. Its timer and
waiting start when the stream is first consumed, so create it before sending.
Frame::builder supports standard and extended identifiers, CAN FD, and bitrate
switching. See the CAN API
for frame types and validation, or run the
CAN sender example:
cargo run --example can_sender --features default-adaptersAlternatively, enable the peak feature and select a PEAK adapter with explicit
nominal and CAN FD data bitrates:
#[cfg(feature = "peak")]
fn open_peak() -> automotive::Result<automotive::can::AsyncCanAdapter> {
use automotive::adapters::peak::{PeakAdapter, PeakConfig};
let config = PeakConfig::builder()
.bitrate(|bitrate| bitrate.bitrate(500_000).data_bitrate(2_000_000))
.build()?;
PeakAdapter::new_async(config)
}See the adapter API and bitrate example for more configuration options.
automotive::transports provides message transports implementing the shared
Transport interface used by UDS.
IsoTP exchanges messages over Classical CAN or CAN FD, handling segmentation,
reassembly, and flow control. Wrap a CAN adapter with IsoTP::new and an
IsoTPConfig, or use IsoTP::from_id for a standard configuration.
use automotive::{adapters::get_adapter, transports::isotp::IsoTP, Error, StreamExt};
#[tokio::main]
async fn main() -> automotive::Result<()> {
let adapter = get_adapter()?;
let transport = IsoTP::from_id(&adapter, 0x7a1);
let mut responses = transport.recv();
transport.send(&[0x3e, 0x00]).await?;
let response = responses.next().await.ok_or(Error::Disconnected)??;
println!("Response: {response:02x?}");
Ok(())
}The configuration builder controls transmit and receive identifiers, bus, addressing, CAN FD, and timing. See the ISO-TP API and example.
On Windows, J2534NativeIsoTP can offload ISO-TP framing and timing to a
compatible PassThru device.
DoIP carries diagnostic messages over Ethernet using ordinary IPv4 or IPv6
sockets. It works on Linux, macOS, and Windows without a hardware-adapter
feature. Connect to a known ECU with
DoIP::connect("192.168.0.10:13400", 0x1000).await?, or discover vehicles:
use automotive::transports::doip::discover;
#[tokio::main]
async fn main() -> automotive::Result<()> {
for vehicle in discover().await? {
let transport = vehicle.connect().await?;
println!("Connected to {}", transport.peer_address());
}
Ok(())
}Active and passive discovery, source binding, and IPv6 multicast are configurable. The transport handles routing activation and alive checks; TLS and OEM authentication are not implemented. See the DoIP API and example. For a directly connected ECU on Linux, scripts/set_up_doip.sh configures the network interface.
Protocol clients live in automotive::protocols.
UDSClient works over ISO-TP or DoIP through the shared Transport interface.
For example, read a VIN over CAN:
use automotive::{
adapters::get_adapter,
protocols::uds::{DataIdentifier, UDSClient},
transports::isotp::IsoTP,
};
#[tokio::main]
async fn main() -> automotive::Result<()> {
let adapter = get_adapter()?;
let transport = IsoTP::from_id(&adapter, 0x7a1);
let uds = UDSClient::new(&transport);
let vin = uds.read_data_by_identifier(DataIdentifier::Vin).await?;
println!("VIN: {}", String::from_utf8_lossy(&vin));
Ok(())
}To use DoIP, pass a connected DoIP transport to UDSClient::new instead.
The client provides typed service helpers, response validation, configurable
timing, and positive-response suppression. Keep one request outstanding per ECU.
See the UDS API
and example for supported services and configuration.
XCPClient supports XCP over Classical CAN, CAN FD, and Ethernet (TCP/UDP),
using XCPOnCAN, XCPOnTCP, or XCPOnUDP. It provides session, resource access,
and memory transfer commands.
use automotive::{
adapters::get_adapter,
can::Identifier,
protocols::xcp::{XCPCanConfig, XCPClient, XCPOnCAN},
};
use std::sync::Arc;
#[tokio::main]
async fn main() -> automotive::Result<()> {
let adapter = Arc::new(get_adapter()?);
let config = XCPCanConfig::new(Identifier::standard(0x6c3)?, Identifier::standard(0x6c4)?);
let xcp = XCPClient::new(XCPOnCAN::new(adapter, config)?)?;
xcp.connect(0).await?;
let data = xcp.short_upload(0x8000_0000, 4, 0).await?;
println!("Memory: {data:02x?}");
xcp.disconnect().await?;
Ok(())
}See the XCP API and the CAN and Ethernet examples.
CCPClient supports CCP 2.1 session, resource access, and memory commands over
Classical CAN. Construct it with a CAN adapter and CCPConfig specifying the
CRO/DTO identifiers and ECU memory byte order.
use automotive::{
adapters::get_adapter,
can::Identifier,
protocols::ccp::{CCPClient, CCPConfig},
};
use std::sync::Arc;
#[tokio::main]
async fn main() -> automotive::Result<()> {
let adapter = Arc::new(get_adapter()?);
let config = CCPConfig::new(Identifier::standard(0x6ec)?, Identifier::standard(0x6ed)?);
let ccp = CCPClient::new(adapter, config)?;
ccp.connect(0).await?;
let data = ccp.short_upload(0x8000_0000, 4, 0).await?;
println!("Memory: {data:02x?}");
ccp.disconnect(false).await?;
Ok(())
}See the CCP API and the memory read example.
The Python package exposes the same CAN adapters, transports, and protocol
clients through synchronous and asyncio APIs, with the import name automotive.
For example, read a VIN over DoIP.
Synchronous:
from automotive.sync.protocols.uds import DataIdentifier, UDSClient
from automotive.sync.transports.doip import DoIP
with DoIP.connect("192.168.0.10", 0x1000) as transport:
uds = UDSClient(transport)
vin = uds.read_data_by_identifier(DataIdentifier.VIN)
print(vin.decode())Asyncio:
import asyncio
from automotive.protocols.uds import DataIdentifier, UDSClient
from automotive.transports.doip import DoIP
async def main():
async with await DoIP.connect("192.168.0.10", 0x1000) as transport:
uds = UDSClient(transport)
vin = await uds.read_data_by_identifier(DataIdentifier.VIN)
print(vin.decode())
asyncio.run(main())See the Python README for more examples and the Python documentation for the user guide and API reference. Matching examples live in python/examples and python/examples/sync.
The optional Nix development shell includes Rust, Python, Scapy, and the build and network tools used by the project:
nix develop
cargo test
cargo test --features allFor the virtual ECU tests on Linux, set up vcan0 and enable the transport test
features. The setup script requires sudo; Scapy must be available to Python.
scripts/set_up_vcan.sh
cargo test --features test-vcan,test-doip,serdetest-vcan runs CAN tests; test-doip runs the DoIP tests over IPv4 and IPv6.
See the Python README for Python development and test commands.