Linux SocketCAN

SocketCAN is the combination of a network stack and a set of CAN hardware drivers included in the Linux Kernel since version 2.6.25.

More information about SocketCAN can be found in the file can.txt which is part of the Linux kernel.

SocketCAN behaves much like other Linux networking protocols like TCP/IP, so the same tools are used to configure the network. Additional command line tools exists to listen to incoming CAN frames or send own frames to the CAN bus.

Virtual CAN lines

For tests without CAN hardware, a "virtual" CAN network can be created:

sudo modprobe vcan

sudo ip link add dev vcan0 type vcan

sudo ip link set up vcan0

CAN hardware drivers

The CAN hardware from the following vendors are used for isegHAL development. Other CAN hardware may work too, but are not tested at iseg.

PEAK

Drivers for PEAK USB and PCI adapters are included in Kernel 3.13.0 (Ubuntu 14.04) and higher.

SYS TEC electronic

Drivers for SYS TEC electronic USB adapters are not included in the Linux Kernel yet, but can be compiled from source code available at the vendors web site.

Useful commands

# Load the basic SocketCAN Kernel drivers
# (if not already done automatically):
sudo modprobe can
sudo modprobe can-raw
sudo modprobe can-bcm

# Set up the CAN interface can0 bitrate:
sudo ip link set can0 type can bitrate 250000

# Automatically reset the CAN controller
# after 100 milliseconds bus-off:
sudo ip link set can0 type can restart-ms 100

# Show information about CAN interface can0
ip -det -stat link show can0

Command line tools

Examples:

candump can0

cansend can0 123#11223344

cangen can0

libsocketcan

isegHAL uses libsocketcan to get bitrate and error information from the CAN bus. Therefore this library needs to be installed to use isegHAL.

isegHAL SocketCAN support

The script driver.sh loads the necessary SocketCAN Kernel drivers and configures the CAN network to the given bitrate.

For CAN hardware other than PEAK-USB, loading their device drivers may be necessary too.

Example to set up the first CAN interface (can0) to a bitrate of 125 kBit/s:

./driver.sh 125 0