How can I set up and use a SocketCAN interface on the CAN bus-supported "netFIELD Compact X8MC Next" device?

Q

How can I set up and use a SocketCAN interface on the CAN bus-supported "netFIELD Compact X8MC Next" device?

A

General information

Hilscher's Edge device "netFIELD Compact X8MC Next" physically supports a CAN bus interface for CAN 2.0A/B and CAN FD communications. The devices come preloaded with netFIELD OS, which includes a driver for the device integrated MCP2518FD CAN contoller. The driver enables the operation of a SocketCAN interface, allowing application software to access the controller.

SocketCAN

The SocketCAN package is a Linux implementation of CAN protocols and its API is designed to closely resemble TCP/IP protocols, making it easy for programmers familiar with network programming to quickly program and use CAN over a well-known socket interface.

Test environment

The SocketCAN interface can only be created and used within a software container, as this is the only method for adding applications to Hilscher's Edge devices.

To facilitate understanding of how to use the SocketCAN interface, we recommend installing a test environment, such as a Debian container. Ensure the container has privileged rights and runs in host mode to access the CAN chip driver embedded in the OS.

Socket activation

There are several methods to initialize and start the CAN socket interface, using various ready-to-use applications, tools, or source code in established programming languages ready for compilation. The simplest method is using command line instructions (as described here). The following command line examples initialize the CAN interface can0, running at a baud rate of 1 MBaud in either normal or at 500kBaud in CAN FD mode:


    sudo ip link set can0 type can bitrate 1000000 
sudo ip link set can0 type can bitrate 500000 dbitrate 2000000 fd on

After the initialization it is necessary to start the can0 interface using the following command


    sudo ip link set can0 up

You can list the available socket interfaces which outputs next to the usual Ethernet sockets now the can0 socket also


    sudo ip addr show

Simple test application

For a simple test we recommend to download and use the github project https://github.com/CANopenNode/CANopenSocket