From Linklayer
Revision as of 22:08, 6 December 2016 by EricEvenchick (talk | contribs) (Created page with "SocketCAN is a collection of CAN drivers and networking tools for Linux. It allows interfacing with CAN bus devices in a similar fashion as other network devices. This allows...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

SocketCAN is a collection of CAN drivers and networking tools for Linux. It allows interfacing with CAN bus devices in a similar fashion as other network devices. This allows for developers to write code that can support a variety of CAN bus interfaces, including CANtact. Unfortunately, SocketCAN only works on Linux.

Getting SocketCAN

The specifics of installing SocketCAN depend on which Linux distribution you are using.


On Ubuntu, the required kernel modules are included, however you will need to install can-utils.

 sudo apt-get install can-utils

You may need to enable the relevant kernel modules:

 sudo modprobe can
 sudo modprobe vcan

SocketCAN with CANtact

To use CANtact with SocketCAN, you will need to run slcand. For example:

 sudo slcand -o -c -s6 /dev/ttyACM0 can0

This command creates a new device called can0 that is connected to the CANtact at /dev/ttyACM0. It will open the device when starting (-o), close the device when finished (-c), and set the speed mode to 6 (-s6).

After running this command, you will need to enable the interface:

 sudo ifconfig can0 up

Virtual CAN Devices

Virtual CAN devices, or vcan devices, can be used to simulate a CAN bus without any hardware. This is useful for simulation, testing, and bridging. It also lets you try out can-utils without having an actual CAN device.

To create a vcan device run:

 sudo ip link add name vcan0 type vcan

NOTE: If you get the error RTNETLINK answers: Operation not supported, load the module with sudo modprobe vcan and try again.

Once the device is created, you can use it like any other CAN device. Just replace can0 in the examples with vcan0.

SocketCAN Utilities

Once a device is enabled, there are a number of utilities that can be used.


candump displays messages on the bus in realtime. To show all traffic in real time on device can0, run:

 candump can0

The displayed messages can be filtered using a mask and identifier. Two filter types are available:

  • [can_id]:[can_mask] matches when [received_can_id] & [can_mask] == [can_id] & [mask]
  • [can_id]~[can_mask] matches when [received_can_id] & [can_mask] != [can_id] & [mask]


Only show messages with ID 0x123 on vcan0:

 candump vcan0,0x123:0x7FF

Only show messages with ID 0x123 or ID 0x456 on vcan0:

 candump vcan0,0x123:0x7FF,0x456:0x7FF 


cansend sends a single CAN frame onto the bus. You will have to specify a device, an identifier and data bytes to send. For example:

 cansend can0 123#1122334455667788

will send a message on interface can0 with identifier 0x123 and data bytes [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]. Note that this tool always assumes values are given in hexadecimal.


cangen can generate random CAN data, which can be useful for testing. For more information, run cangen for detailed usage information.


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. For more information, run cansniffer for detailed usage information.


Wireshark includes support for SocketCAN. If you have a SocketCAN device active (real or virtual), you can select it in Wireshark and view the frames being received in real time.

Wireshark has the following filters for CAN messages:

  • (frame arbitration identifier)
  • can.len (frame data length)
  • can.flags.err (frame error flag)
  • can.flags.rtr (frame remote flag)
  • can.flags.xtd (frame extended identifier flag)

Wireshark also has filters for the canopen protocol.

Developing for SocketCAN

SocketCAN makes it simple to interface with a variety of CAN bus hardware. It leverages standard network sockets, which means it can be accessed from many different languages.


SocketCAN is supported in Python versions 3.3+. The pyvit library provides access to SocketCAN. You can use pyvit for this purpose, or reuse the SocketCAN implementation implementation.


A C source code example is available on the SocketCAN Wikipedia page.