Connect a supported CAN adapter to a Linux computer and you may see a new network interface called can0. That name tells you something important about how Linux handles CAN. Your application opens a socket and binds it to a CAN interface. The kernel and the device driver take care of moving frames between the application and the hardware.
That is SocketCAN. If you are building a Raspberry Pi data logger, writing a test application, or simply trying to see what is on a CAN bus, it gives you a common way to work with different hardware. The same candump command can monitor a supported USB adapter or an onboard CAN controller. You only need to tell it which interface to use.
Where SocketCAN fits
Think of the setup in three parts. At the top is your application, which could be a command-line utility, a Python script, or a C program. In the middle is the Linux CAN networking subsystem. At the bottom is a CAN interface backed by a driver and hardware, or by a virtual interface for software testing.
Figure 1. The same SocketCAN API can exchange frames through a physical interface such as can0 or a virtual interface such as vcan0.
On a physical interface, the CAN controller handles frame transmission and reception. The transceiver provides the electrical connection to CAN_H and CAN_L. On a virtual interface, Linux delivers frames to local applications without any hardware in the path.
The socket model also lets several applications use the same interface at once. You can leave candump running while your own application receives frames from the same bus. Each application can set its own receive filters, which lets a logger capture all traffic while another program receives only the identifiers it cares about.
Try SocketCAN without an adapter
The easiest way to see how this works is to create a virtual interface called vcan0. The example below uses a Debian-based system such as Raspberry Pi OS or Ubuntu and assumes the kernel includes virtual CAN support.
sudo apt install can-utils
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set dev vcan0 upIn one terminal, start listening:
candump vcan0Leave it running. In a second terminal, send a Classical CAN frame:
cansend vcan0 123#11223344The first terminal should display a line like this:
vcan0 123 [4] 11 22 33 44Read that line from left to right. vcan0 is the interface, 123 is the hexadecimal CAN identifier, and [4] is the payload length in bytes. The last four values are the payload. The identifier 0x123 in this example uses the standard 11-bit format.
You now have a repeatable test for a receive routine, a filter, or a data logger. You can feed it the same frame every time and check what the program does with it. Tests involving bit timing, termination, arbitration between physical nodes, or bus errors require a real CAN network.
Move from vcan0 to a physical CAN bus
Once Linux recognizes your hardware as a SocketCAN interface, check its name and configure it for the network you are connecting to. For an interface named can0 on a 250 kbit/s Classical CAN bench network, the commands are:
sudo ip link set dev can0 down
sudo ip link set dev can0 type can bitrate 250000
sudo ip link set dev can0 up
ip -details -statistics link show dev can0Then monitor the traffic:
candump can0The bitrate must match the network. On a bench setup, check the wiring and termination and make sure another node is transmitting. A CAN adapter can be correctly configured and still display nothing if the bus is idle.
There is an important distinction when monitoring. candump receives frames through a socket, but the controller may still acknowledge valid frames in normal operating mode. For passive monitoring, configure the interface in listen-only mode before bringing it up, provided the hardware and driver support it. An adapter in listen-only mode cannot supply the acknowledgment a lone transmitting node needs on a two-node bench setup.
What about CAN FD, J1939, and NMEA 2000?
SocketCAN also supports CAN FD. On a physical bus, the interface and driver must support it, and the interface must be configured for FD operation with timing that matches the network. An application using a raw CAN socket also enables the CAN_RAW_FD_FRAMES option to send and receive CAN FD frames through that socket.
Higher-layer protocols give the frames their meaning. A raw CAN capture can show the 29-bit identifiers and data bytes on an SAE J1939 or NMEA 2000 network. Turning those bytes into engine speed, vessel heading, or another reading requires the relevant message definitions and decoding logic. Linux also provides a dedicated J1939 socket API for applications that want kernel support for J1939 addressing and transport.
For a first project, I would start with the two-terminal experiment. Once you can send a known frame and see exactly what arrives, replace cansend or candump with your own code. When you move to physical hardware, keep candumpopen alongside your application. It gives you a direct way to check whether the frames your program expects are actually arriving.
For more detail on CAN interfaces, socket options, and programming examples, see the official SocketCAN documentation in the Linux kernel manual.
Mastering Embedded Linux Development: Craft fast and reliable embedded solutions with Linux 6.6 and The Yocto Project 5.0 (Scarthgap)
Mastering Embedded Linux Development is both a practical guide to building embedded Linux systems and a reference you can return to as your projects evolve.
The fourth edition explains the four components at the heart of an embedded Linux system: the toolchain, bootloader, kernel, and root filesystem. You’ll start by installing a prebuilt cross-compilation toolchain. Then you’ll use it to build the bootloader and kernel and assemble a root filesystem from scratch. Once you understand how the pieces fit together, you’ll learn to automate the build process with Buildroot and the Yocto Project.
From there, you’ll explore how to deliver secure over-the-air updates and prototype new peripherals with add-on boards. Two new chapters cover packaging Python software and deploying containerized applications. Later chapters explain how to write multithreaded code and manage memory efficiently. The book closes with practical techniques for debugging user-space and kernel code, using the GNU Debugger (GDB) for remote debugging and Linux tracing and profiling tools to pinpoint performance bottlenecks.
*Email sign-up and proof of purchase are required.
What you will learn
Build embedded Linux images for your target hardware with Buildroot and the Yocto Project
Enable Wi-Fi and Bluetooth using a Yocto board support package
Securely update deployed IoT devices with Mender or balena
Prototype new peripherals by connecting add-on boards, reading schematics, and writing test programs
Deploy containerized applications on edge devices with Docker
Debug devices remotely with GDB and investigate performance with tools such as
perfandply
Who this book is for
This book is for systems software engineers and system administrators who want to build Linux-based embedded devices. It also helps embedded software engineers who are familiar with low-power microcontrollers make the transition to Linux-capable systems-on-chip. Hardware developers will find practical guidance on bringing up boards and testing peripherals under Linux.
You should have a solid grounding in C programming and POSIX interfaces before you begin. More information…





