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.
Pre-requisites
Section titled “Pre-requisites”Check that your hardware supports SocketCAN. Common CAN hardware with drivers already built into the Linux kernel include devices from Peak System and Kvaser.
Setting up a CAN interface
Section titled “Setting up a CAN interface”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.
sudo ip link set can0 type can bitrate 500000sudo ip link set up can0Checking 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.
ip link showExample:
$ ip link show type vcan4: vcan0: <NOARP> mtu 72 qdisc noop state DOWN mode DEFAULT group default qlen 1000 link/canSetting up a virtual CAN interface
Section titled “Setting up a virtual CAN interface”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):
sudo modprobe vcanFrom there you can add a device and bring it up like a physical one.
sudo ip link add dev vcan0 type vcansudo ip link set up vcan0Note the type is
vcanand no bitrate is required
Inspecting CAN bus state and errors
Section titled “Inspecting CAN bus state and errors”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.
ip -details -statistics link show can0Example:
$ ip -details -statistics link show vcan04: 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 0The 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.
Recovering from errors
Section titled “Recovering from errors”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.
sudo ip link set down can0sudo ip link set up can0Detailed info
Section titled “Detailed info”If you need more detailed information about working with SocketCAN, refer to the Kernel documentation