CAN FD on Linux (SocketCAN)
This page sets up a CAN FD interface on a Linux host with SocketCAN, checks it, and shows how to send and receive frames from the shell and from Python. Everything here is standard Linux. The only drive-specific values are the bit rates, which are TBD; the examples use 1 Mbit/s arbitration and 5 Mbit/s data.
You need
- A CAN FD adapter with a SocketCAN driver.
- iproute2 (the
ipcommand), installed on most distributions. - can-utils:
sudo apt install can-utils. - For the Python example: python-can (
pip install python-can).
1. Find the interface
List the CAN interfaces:
ip link show type can
The first adapter is normally can0. If the list is empty, check that the kernel loaded a driver for the adapter:
sudo dmesg | grep -i can
2. Set the bit rates
The interface must be down while you change bit timing.
sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000 dbitrate 5000000 fd on restart-ms 100
sudo ip link set can0 txqueuelen 1000
sudo ip link set can0 up
| Option | Meaning |
|---|---|
bitrate 1000000 |
Arbitration bit rate, 1 Mbit/s. |
dbitrate 5000000 |
CAN FD data bit rate. Drive value: TBD. |
fd on |
Allow CAN FD frames. |
restart-ms 100 |
Restart the controller 100 ms after bus-off. Without it, the interface stays bus-off until you restart it. |
txqueuelen 1000 |
Longer transmit queue. The default of 10 frames can overflow during bursts such as firmware downloads. |
sample-point 0.8, dsample-point 0.75 |
Optional. Set the sample points explicitly. Drive values: TBD. |
berr-reporting on |
Optional. Report each bus error as an error frame (see step 4). |
For classic CAN only, leave out dbitrate and fd on:
sudo ip link set can0 type can bitrate 1000000
These settings are lost when you reboot or unplug the adapter. Step 5 makes them permanent.
3. Check the interface
ip -details -statistics link show can0
Example output, shortened. Timing values depend on the adapter.
4: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/can promiscuity 0 ...
can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 100
bitrate 1000000 sample-point 0.800
...
dbitrate 5000000 dsample-point 0.750
...
re-started bus-errors arbit-lost error-warn error-pass bus-off
0 0 0 0 0 0
...
| Field | Expect |
|---|---|
mtu 72 |
CAN FD frames allowed. mtu 16 means classic CAN only. |
state ERROR-ACTIVE |
The controller is taking part in bus traffic normally. |
berr-counter tx 0 rx 0 |
No errors counted. |
bitrate, dbitrate |
The values you set. |
bus-off counter |
0. |
The controller state follows its error counters:
| State | Meaning |
|---|---|
ERROR-ACTIVE |
Normal. |
ERROR-WARNING |
An error counter has reached 96. |
ERROR-PASSIVE |
An error counter has passed 127. The controller still transmits but no longer signals errors actively. |
BUS-OFF |
The transmit error counter has passed 255. The controller has stopped transmitting. |
Troubleshooting lists the usual causes of each state.
4. Watch and send frames
Show all traffic with time between frames:
candump -td can0
Show only some CAN IDs with a filter <id>:<mask>. A frame passes when frame_id & mask == id & mask. This filter passes 0x580 to 0x5FF, the CANopen SDO responses of all nodes:
candump can0,580:780
Show error frames (needs berr-reporting on for bus errors):
candump -e can0,0:0,#FFFFFFFF
Log traffic to a file, and play it back later:
candump -l can0 # writes candump-<date>_<time>.log
canplayer -I candump-<date>_<time>.log
Send frames with cansend. # marks a classic frame; ## marks a CAN FD frame, followed by one flags digit (1 = bit rate switch).
cansend can0 123#DEADBEEF # classic CAN, ID 0x123, 4 bytes
cansend can0 123##1DEADBEEF # CAN FD with bit rate switch
cansend can0 123##0DEADBEEF # CAN FD without bit rate switch
Note Every frame needs an acknowledgement from another node. If the adapter is alone on the bus, it repeats the frame, its transmit error counter rises and the state goes to
ERROR-PASSIVE. Connect a powered drive before you test sending.
5. Bring the interface up at boot
With systemd-networkd, create /etc/systemd/network/80-can0.network:
[Match]
Name=can0
[CAN]
BitRate=1M
DataBitRate=5M
FDMode=yes
RestartSec=100ms
Then enable systemd-networkd:
sudo systemctl enable --now systemd-networkd
Unplug and replug the adapter, then repeat step 3 to check the settings.
6. Use the bus from Python
python-can opens the same interface. Pass fd=True to send and receive CAN FD frames.
import can
with can.Bus(interface="socketcan", channel="can0", fd=True) as bus:
bus.set_filters([{"can_id": 0x580, "can_mask": 0x780, "extended": False}])
bus.send(can.Message(arbitration_id=0x123, data=[0xDE, 0xAD, 0xBE, 0xEF],
is_extended_id=False, is_fd=True, bitrate_switch=True))
msg = bus.recv(timeout=1.0) # None if no frame passes the filter within 1 s
print(msg)
The CANopen and UDS pages build on this with python-canopen, python-can-isotp and udsoncan.
7. Test without hardware
A virtual CAN interface lets you test host software with no adapter connected:
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set vcan0 mtu 72 # allow CAN FD frames
sudo ip link set vcan0 up
Use vcan0 in place of can0. A virtual interface has no bit timing, needs no acknowledgement and never reports bus errors.