---
jupytext:
  text_representation:
    format_name: myst
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# CAN bus

{.lead}
A CAN bus connects the controllers in a vehicle or machine over one twisted pair. This page covers how its nodes share the bus, and how to turn its frames into channels of data.

{download}`Download this page as a notebook <can-bus.md>` to run its code in JupyterLab. [Running the notebooks](notebooks/running.md) explains how to set it up.

## How a CAN bus works

Every node on a {gloss}`CAN bus` can send, and every node hears every message.[^ti-can]

- **Identifiers.** A classic CAN frame has an 11-bit message identifier, or a 29-bit extended one.[^ti-can]
- **Data.** A classic frame carries 0 to 8 bytes of data.[^ti-can] CAN FD carries up to 64 bytes, and can switch to a faster bit rate for the data part of each frame.[^cia-can-fd]
- **Arbitration.** When two nodes start sending at once, the bus settles it bit by bit, by each message's identifier. The identifier with the lower binary value has the higher priority, and wins.[^ti-can]

This code orders four frames that are all ready to send at once, as arbitration does. The identifiers are made up:

```{code-cell} ipython3
import pandas as pd

waiting = [
    {"node": "display", "identifier": 0x7E0},
    {"node": "brake", "identifier": 0x0A0},
    {"node": "battery", "identifier": 0x180},
    {"node": "motor", "identifier": 0x120},
]
order = sorted(waiting, key=lambda frame: frame["identifier"])  # lowest value wins
pd.DataFrame(order).assign(
    identifier=lambda df: df["identifier"].map("0x{:03X}".format),
    sends=range(1, len(order) + 1),
)
```

Because the lowest identifier always wins, the identifiers a system assigns set its priorities.

## Decoding frames

ISO 11898 defines CAN's physical and data-link layers. What a frame's data bytes mean belongs to the application layer, where a higher-layer protocol such as CANopen, or the device's manufacturer, defines it.[^ti-can] To decode a frame, you need its layout: which bytes hold each signal, in which byte order, and how to scale the raw number into units. A DBC file can hold that layout, and the cantools library reads DBC files and decodes frames with them.[^cantools]

python-can receives the frames. Its `virtual` interface connects buses within one Python process, which supports tests that are independent of the operating system and the driver.[^python-can] This code reads frames from a bus, decodes each with a DBC file, and streams the signals to Nominal:

```python
from datetime import datetime, timezone

import can
import cantools
from nominal.core import NominalClient

db = cantools.database.load_file("vehicle.dbc")
client = NominalClient.from_profile("default")
dataset = client.get_dataset("<dataset RID>")

with can.Bus("can0", interface="socketcan") as bus:
    with dataset.get_write_stream() as stream:
        while True:
            message = bus.recv()
            signals = db.decode_message(message.arbitration_id, message.data, decode_choices=False)
            now = datetime.now(timezone.utc)  # when the computer received the frame
            for signal, value in signals.items():
                stream.enqueue(f"{message.arbitration_id:#x}.{signal}", now, float(value))
```

The code timestamps each frame when it arrives, so a delay between the bus and the computer shows up as a delay in the data. [What the link means for your data](communications.md#what-the-link-means-for-your-data) explains how to check for it. Each decoded signal becomes a channel named for its frame's identifier and the signal, and `get_write_stream` sends them in batches.[^nominal-client] Set each channel's unit with `add_channel`, as in [Engineering data and warehousing](engineering-data.md#in-nominal).

## Further reading

- [Communications](communications.md), with a worked decode of a motor controller's frames, and the other links a test system uses.
- [Network performance](network-performance.md), on how much traffic a bus carries.
- Texas Instruments, [Introduction to the Controller Area Network (CAN)](https://www.ti.com/lit/an/sloa101b/sloa101b.pdf).
- cantools, [documentation](https://cantools.readthedocs.io/en/latest/).
- python-can, [documentation](https://python-can.readthedocs.io/en/stable/).

## References

[^ti-can]: S. Corrigan, Texas Instruments, ["Introduction to the Controller Area Network (CAN)"](https://www.ti.com/lit/an/sloa101b/sloa101b.pdf), SLOA101B, revised May 2016. Retrieved 2026-11-12.
[^cia-can-fd]: CAN in Automation, ["CAN FD: The basic idea"](https://www.can-cia.org/can-knowledge/can-fd-the-basic-idea). Retrieved 2026-11-12.
[^cantools]: cantools, ["cantools documentation"](https://cantools.readthedocs.io/en/latest/), on loading DBC files with `cantools.database.load_file` and decoding with `decode_message`. Retrieved 2026-11-30.
[^python-can]: python-can, ["python-can 4.6.1 documentation"](https://python-can.readthedocs.io/en/stable/) and ["Virtual"](https://python-can.readthedocs.io/en/stable/interfaces/virtual.html), on `can.Bus`, `recv` and the virtual interface. Retrieved 2026-11-30.
[^nominal-client]: Nominal, `Dataset.get_write_stream` in the [nominal](https://pypi.org/project/nominal/) Python client, version 1.186.0. Retrieved 2026-11-30.
