CAN – Controller Area Network
SocketCAN
The SocketCAN is an implementation of CAN protocols (Controller Area Network) for Linux.
CAN is a network transport protocol widely used in, automotive, and industrial embedded devices.
SocketCAN uses the Berkeley socket API to provide CAN device drivers and expose CAN devices as network interfaces.
The SocketCAN API has been designed very closer as for socket API used for TCP/IP protocols.
SocketCAN drivers expose the CAN interface as a standard networking device, visible in /sys/class/net, and usable via the Berkeley Sockets Layer via the PF_CAN protocol family. SocketCAN is documented in Documentation/networking/can.txt
SocketCAN provides the following types of CAN interfaces:
-
Native interfaces. These are CAN interfaces associated with real hardware (such as a USB-to-CAN adapter). They are named as can0, can1, …
-
Virtual interfaces. These are CAN interfaces that are not associated with any real hardware. They are named as vcan0, vcan1, …
-
SLCAN based interfaces. SLCAN interfaces provide a serial interface. They are named as slcan0, slcan1, … Socket wrapper for serial-to-CAN interfaces, supporting the slcan protocol. A /dev/tty device is turned into a CAN socket device.
-
Virtual CAN tunnel interface: tunnel interface to forward traffic to a container. It is named as vxcan1, vxcan2, …
Get statistics: ip -details -statistics link show canX or in file /proc/net/can/stats
Virtual CAN Interface
Create a virtual CAN network interface with the name vcan0
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
To check the virtual can interface is created
ip link show vcan0
# result command:
4: vcan0: <NOARP,UP,LOWER_UP> mtu 72 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/can
CAN Tools
The can-utils package provides a set of commands that can be used either on real or virtual CAN bus.
Monitor received data
The candump is a command to print received data by a CAN interface. It is used in a dedicated terminal window.
# monitor window
candump vcan0
cansniffer displays frames that are currently on the bus, but filters out frames with data that is not changing. This is very useful for reverse engineering CAN bus systems. A CAN bus sniffer software is a specialized program designed to intercept and decode communication on a CAN bus.
# monitor window
cansniffer vcan0
Send Can frame
To send a CAN frame with identifier 0x1A (26 dec) and 8 bytes of data
cansend vcan0 01A#1122334455667788
The received data by can0 interface appears in the monitor terminal (candump).
# monitor window
vcan0 01A [8] 11 22 33 44 55 66 77 88
Send bulk random data
To send large amount of data in a random way
cangen vcan0 -v
Record dump file
To record all received data in a file using timestamp
# monitor window
candump -l vcan0
Disabled standard output while logging.
Enabling Logfile 'candump-2024-08-15_124705.log'
Print dumped log data
log2asc -I candump-2024-08-15_123738.log vcan
# output:
date Thu Aug 15 12:37:38 2024
base hex timestamps absolute
no internal events logged
0.000000 1 75D Rx d 0
0.200313 1 70 Rx d 2 AB 1C
0.400709 1 A8 Rx d 1 B6
0.601096 1 7BF Rx d 1 39
0.801489 1 5CB Rx d 0
1.001887 1 CE Rx d 8 51 80 7E 5E 49 A7 4E 72
1.202286 1 1C1 Rx d 8 30 72 6D 04 D5 EF 56 6E
1.402672 1 19F Rx d 1 F5
1.603068 1 791 Rx d 5 08 C7 13 63 01
1.803453 1 127 Rx d 4 CA 60 DD 75
2.003735 1 555 Rx d 0
2.204012 1 6A1 Rx d 6 27 73 B8 69 6D 44
2.404304 1 436 Rx d 8 F1 F6 A6 77 05 FD B7 3E
2.604603 1 48C Rx d 3 4E A4 06
2.804910 1 8E Rx d 8 23 1E 7D 5E 2D EA 81 2C
3.005228 1 6CF Rx d 8 29 33 DD 68 60 3E 68 75
3.205552 1 36E Rx d 2 62 3E
Replay scenario
The can data log file can be sent again to reproduce the same scenario.
canplayer -I candump-2024-08-15_123738.log
To send the data scenario to another CAN interface than the origin CAN interface.
canplayer vcan0=can1 -v -I candump-2024-08-15_123738.log