Skip to content

Installing SocketCAN

Guy Corbaz edited this page Sep 22, 2019 · 6 revisions

Introduction

SocketCAN is a set of open source CAN device drivers and networking stack that extend the Berkeley socket API in Linux. Installing SocketCAN is rather easy. It introduces a new protocol family called PF_CAN. SocketCAN coexists with other protocol families like PF_INET for the Internet Protocol. The communication with the CAN bus is therefore done analogously to the use of the Internet Protocol, via sockets.

Installing SocketCAN

Installing SocketCAN is rather easy. In fact, it just needs to install Debian/Ubuntu packages. To proceed, uses the following command:

sudo apt-get install can-utils

You can find the detailed installation and configuration commands in the following web site. However, I copied some parts below to avoid the reader jumping from a site to another ans ease its life.

Enabling SocketCAN

By default, the SocketCAN device drivers are not automatically loaded by Linux at boot time on all systems: you may need to enable the relevant linux kernel modules. You can manually load these modules with the following command:

sudo modprobe can
sudo modprobe vcan
sudo modprobe slcan

Loading kernel modules at boot time

Instead of manually loading the modules after the system has booted, you can configure the system to do it automatically for you. For that, just edit the file /etc/modules and configure it as below:

To do

Using SocketCAN with CanUSB

To use CanUSB with SocketCAN, you have to establish a “link” between the drivers and the hardware. This is done with the following command:

sudo slcand -o -c -s4 /dev/ttyACM0 can0
sudo ifconfig can0 up

The -s parameter allow to configure different interface speed as indicated below.

ASCII Command CAN Bitrate
s0 10 Kbit/s
s1 20 Kbit/s
s2 50 Kbit/s
s3 100 Kbit/s
s4 125 Kbit/s
s5 250 Kbit/s
s6 500 Kbit/s
s7 800 Kbit/s
s8 1000 Kbit/s

If everything worked fine, you should now see the can0 network device. To very it, just type the following command:

ifconfig