Skip to content
Open in AI

SocketCAN Basic Configuration ​

Use the SocketCAN Command Generator to configure, bring up or down, restart and inspect interfaces.

Core Concepts ​

Linux registers SocketCAN-capable devices as network interfaces such as can0 and can1. Replace can0 below with your actual interface name. Match the bit rates of the other bus nodes. CAN FD has a separately configured data bit rate.

Environment Preparation ​

On Debian / Ubuntu:

bash
sudo apt install iproute2 can-utils

Kernel Support Check ​

Install the Linux driver appropriate to the device and current firmware. For the kcan driver supplied in the KH-UCANFD Linux SDK, follow the SDK installation instructions before loading kcan. Other firmware modes require their corresponding drivers.

bash
lsmod | grep can
sudo modprobe can
sudo modprobe can_raw

lsmod lists loadable modules; built-in drivers may not appear. Check interface availability and driver logs as well.

Device Identification ​

bash
# USB devices and drivers
lsusb -t

# Physical CAN interfaces
ip -details link show type can

# A specific interface (also usable with vcan0)
ip -details link show dev can0

Device Configuration ​

Bring the interface down before changing bit timing; this interrupts traffic on that channel. Choose one configuration below. Changing interfaces requires administrator privileges or CAP_NET_ADMIN. Supported rates and modes depend on the controller and driver.

Classic CAN ​

bash
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can bitrate 500000 fd off listen-only off restart-ms 0 &&
sudo ip link set dev can0 up

CAN FD ​

bash
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can bitrate 500000 dbitrate 2000000 fd on listen-only off restart-ms 0 &&
sudo ip link set dev can0 up

&& runs each step only if the previous step succeeds. fd on enables FD capability on the interface; the transmit command separately controls BRS. The data bit rate must not be lower than the nominal bit rate.

To specify sample points, configure the following while the interface is down. Choose values appropriate to the bus and controller:

bash
sudo ip link set dev can0 type can bitrate 1000000 sample-point 0.75 dbitrate 5000000 dsample-point 0.8 fd on

Use the CAN Baud Rate Calculator for detailed bit timing.

Controller mode and interface state ​

Listen-only mode requires driver support. It neither transmits frames nor acknowledges received frames. Bring the interface down before changing the mode:

bash
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can listen-only on &&
sudo ip link set dev can0 up

Change listen-only on to listen-only off to restore normal operation. To bring an interface up or down separately:

bash
sudo ip link set dev can0 up
sudo ip link set dev can0 down

Interface Status ​

bash
# Bit timing, controller state, traffic and error counters
ip -details -statistics link show dev can0

# All physical CAN interfaces
ip -details -statistics link show type can
FieldMeaning
UP flagAdministratively enabled; it does not mean the bus is error-free
CAN state ERROR-ACTIVEController is error active
CAN state ERROR-PASSIVETransmit or receive error count reached the error-passive threshold
CAN state BUS-OFFController has left the bus and cannot communicate normally
CAN state STOPPEDController is stopped
berr-counter tx/rxController transmit/receive error counters; availability depends on the driver
RX/TX errors/droppedNetwork-interface statistics, distinct from controller error counters
mtu 16 / mtu 72Classic CAN / CAN FD frame structure size, not payload byte count

Growing error counts can result from rate, wiring, termination, ACK or interference problems. The count alone does not identify the cause. Controller error counting has multiple rules and exceptions; it is not simply a count of lost frames.

Error Handling ​

Bus-Off recovery ​

First check bit rates, wiring, termination and other nodes. After resolving the bus fault, manually restart an UP interface in Bus-Off with automatic restart disabled (restart-ms 0):

bash
sudo ip link set dev can0 type can restart

Alternatively, configure automatic restart, for example with a 100 ms delay:

bash
sudo ip link set dev can0 down &&
sudo ip link set dev can0 type can restart-ms 100 &&
sudo ip link set dev can0 up

restart-ms 0 disables automatic restart. Inspect the state again after recovery. Repeated Bus-Off requires resolving the underlying bus fault.

Next: Send and receive frames with can-utils. Reference: Linux kernel SocketCAN documentation.

Driving Intelligent Connections, Empowering the Future
蜀ICP备2023000218号-2