Skip to content

Setting up a SocketCAN interface

Seerwatch uses SocketCAN on Linux in order to connect to a CAN network. SocketCAN is an open source CAN network stack built into the Linux kernel. This guide will walk you through setting up physical CAN hardware with SocketCAN, and provide tips on virtual interface setup and diagnosing issues.

Check out the SocketCAN cheat sheet for a quick reference of all the commands.

Check that your hardware supports SocketCAN. Common CAN hardware with drivers already built into the Linux kernel include devices from Peak System and Kvaser.

To set up a CAN interface you need to specify which interface to setup, and what bitrate. CAN devices typically enumerate incrementally as can0,1,2,3 etc.

You first set the bitrate, then set the link up. Here we set the baudrate to 500kbps.

Terminal window
sudo ip link set can0 type can bitrate 500000
sudo ip link set up can0

Checking available interfaces and their state

Section titled “Checking available interfaces and their state”

You can check what CAN interfaces are available by running ip link show with an optional type [can,vcan] to filter for a specific type.

Terminal window
ip link show

Example:

$ ip link show type vcan
4: vcan0: <NOARP> mtu 72 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/can

If you don’t have a physical CAN device on hand, you can use a virtual CAN interface for testing.

You first need to load the virtual can interface module into the kernel (only required once):

Installing virtual can module
sudo modprobe vcan

From there you can add a device and bring it up like a physical one.

Terminal window
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0

Note the type is vcan and no bitrate is required

Sometimes the CAN bus may not be sending or receiving as you expect. In this case, you will want to check the state for any errors. You can view details about the CAN bus by passing the -details -statistics flags to the show command.

Terminal window
ip -details -statistics link show can0

Example:

Terminal window
$ ip -details -statistics link show vcan0
4: vcan0: <NOARP> mtu 72 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0
vcan numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536
RX: bytes packets errors dropped missed mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0

The state value will show a few different values

  • DOWN: The link is down. You can fix it by running sudo ip link set up can0
  • ERROR-ACTIVE: The normal operational state of the bus.
  • ERROR-WARNING: An early warning state. This triggers when either the Transmit Error Counter (TEC) or Receive Error Counter (REC) exceeds 96. The node operates normally but signals increasing bus instability.
  • ERROR-PASSIVE: A restricted operational state. This triggers when TEC or REC exceeds 127. The node can still send and receive data, but when it detects an error, it transmits passive error flags (recessive bits), preventing it from destroying valid network traffic.
  • BUS-OFF: The critical failure state. When the TEC exceeds 25 the CAN controller completely disconnects from the physical bus. It can no longer transmit or receive any messages.

If the CAN device detects errors on the bus it will go into bus-off mode. You can recover from bus off by bringing the device down, then up.

Terminal window
sudo ip link set down can0
sudo ip link set up can0

If you need more detailed information about working with SocketCAN, refer to the Kernel documentation