Skip to content
Raibisch edited this page Dec 16, 2023 · 5 revisions

Infos and Links for CAN bus communication in Linux Systems

socketCAN Basics

https://elinux.org/CAN_Bus

https://blog.mbedded.ninja/programming/operating-systems/linux/how-to-use-socketcan-with-the-command-line-in-linux/

Quick Start

Native Interfaces

 $ sudo ip link set can0 type can bitrate 125000
 $ sudo ip link set up can0

SLCAN based Interfaces

SLCAN based device provide a serial interface. At first you'll need a special daemon (slcand from can-utils), that will link this serial interface with a virtual CAN device. By default these devices get slcan name base. This is an example for a USB-to-CAN adapter working at 3Mbit/s:

 $ sudo slcand -o -s8 -t hw -S 3000000 /dev/ttyUSB0
 $ sudo ip link set up slcan0

So far there is no way to set bitrate for SLCAN based devices via ip tool, so you'll have to do this by slcand invocation: -sX parameter. -s8 in the above example will set adapter's bitrate to 1Mbit/s. See the table below for further CAN bitrates:

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

Script for adding virtual CAN interface (vcan)

#!/bin/bash
# Make sure the script runs with super user privileges.
[ "$UID" -eq 0 ] || exec sudo bash "$0" "$@"
# Load the kernel module.
modprobe vcan
# Create the virtual CAN interface.
ip link add dev vcan0 type vcan
# Bring the virtual CAN interface online.
ip link set up vcan0

Lazarus (Free-Pascal) Unit for socketCAN

https://pragmaticlinuxblog.github.io/cancomm_lazarus/