
In the Linux system, CAN (Controller Area Network) communication can be operated through the SocketCAN interface. SocketCAN is the native interface provided by the Linux kernel for the CAN bus, allowing communication with the CAN network in a socket-like manner. It supports common CAN protocols, including standard frames and extended frames, and can be operated through command-line tools or programming interfaces.
Using command-line tools to configure the CAN interface
1. Check if the CAN interface exists
CAN is also a network device like a network card, so you can check its status using the ifconfig command.

2. Set the CAN network interface
(1) Use the ip command to set the baud rate for the CAN interface (assuming the CAN interface name is can0). For example, set the baud rate to 500kbps:
(2) Bring the CAN interface UP to enable the interface:
(3) You can check the status of the CAN interface using the ip link command:

The output indicates that the can0 interface is UP and the physical connection is normal (LOWER_UP), and it is using CAN protocol communication. MTU is 16 bytes, echo functionality is enabled, queue length is 10, and the scheduling algorithm is FIFO.
If you need to shut down the CAN interface, you can use the following command:
3. Use candump to listen to the CAN bus
candump is a commonly used CAN command-line tool in Linux for listening to and displaying all data frames on the CAN bus.
Listen to all data frames on the CAN bus:
This command will display all data frames received from the can0 interface.
The effect is as follows:

Different versions of candump have slightly different commands.
Use candump –help to see specific command prompts.
You can use the candump command to filter specific CAN IDs, for example, only listen to data frames with ID 0x123:
The filter format is id:mask, where:
-
id is the CAN frame ID you want to filter.
-
mask is the mask used to match which bits of the ID need to be compared.
When the ID of the data frame ANDed with the specified id equals the mask, the data frame will be displayed.

4. Use cansend to send CAN frames
cansend is a tool used to send CAN frames. The format for sending CAN frames is as follows:

-
can0 is the CAN interface name.
-
-
11223344556677 is the data to be sent, with a length of no more than 8 bytes.
For example, send a CAN frame with ID 0x123 and data 0x11, 0x22, 0x33:

Note: Some versions of cansend do not support this sending command, please check if the CAN analysis software accepts the data normally.
Use cansend –help to see the specific command format.

According to the help information of the cansend tool, the <can-msg> part needs to be sent in the form of a space-separated byte list. In other words, the data part is no longer connected using the # symbol, but separated by spaces for each byte.
The specific usage is as follows


At this point, the data display is normal. If the data does not match expectations, please check the format supported by the cansend command.
Using Linux library functions to complete CAN communication
In Linux system programming, sending and receiving CAN data can be achieved by using the native socket API with the PF_CAN protocol family.
Create and configure the CAN socket.
Bind to a specific CAN interface (like can0).
Send and receive CAN frames.
Code example for sending CAN data


Code example for receiving CAN frames



When performing CAN (Controller Area Network) communication in Linux, the following three important structures are used: struct sockaddr_can, struct ifreq, and struct can_frame. They are used for socket addresses, network interface configuration, and handling CAN frames, respectively. Below is a detailed analysis of these three structures.
-
struct sockaddr_can is used to bind the socket to a specific CAN interface, containing the address family and interface index.
-
struct ifreq is used to configure and manipulate network interface properties, most commonly used to obtain the index of the CAN interface.
-
struct can_frame is used to represent data frames in the CAN protocol, containing the CAN ID, data length, and actual CAN data.
This structure is used to represent the address of the CAN socket, and it is the address structure of the AF_CAN protocol family (the CAN protocol in Linux). It will use this structure when binding the socket to the CAN interface.
Defined in the header file <linux/can.h>:

-
can_family: Specifies the protocol family of the address. In CAN communication, can_family is set to AF_CAN to represent the CAN protocol.
-
can_ifindex: Represents the network interface index related to CAN. It is obtained through ioctl, and common interfaces include can0, can1, etc. Use SIOCGIFINDEX to get the index.
-
tp (optional): Used only in certain advanced transport layer protocols (such as ISO-TP). Generally, this field is not required for basic CAN communication.

struct ifreq is mainly used to configure network interface properties, such as obtaining the index of the network interface and setting device parameters. In CAN communication, it is commonly used to obtain the index of a specified interface (like can0) to associate with the CAN device.
Defined in the header file <net/if.h>:

-
ifr_name: Represents the name of the network interface, such as “can0”, “eth0”. This is a string array defining the name of the interface.
-
ifr_ifindex: Stores the index of the network interface. The interface index can be obtained by calling ioctl and using the SIOCGIFINDEX command.
-
Other fields (like ifr_flags) can be used to set and obtain interface status, but are not commonly used in basic CAN communication.

This is the structure of the CAN frame, representing the data frame transmitted over the CAN network. Each CAN frame contains an identifier (CAN ID), data length (DLC), and up to 8 bytes of actual data.
Defined in the header file <linux/can.h>:

can_id: The identifier of the CAN frame. According to the CAN protocol, this field can have an 11-bit standard ID or a 29-bit extended ID. The CAN ID may include additional flag bits, such as remote transmission request (RTR) and error flag (ERR):
-
CAN_EFF_FLAG: Indicates that the frame uses a 29-bit extended ID.
-
CAN_RTR_FLAG: Indicates a remote transmission request frame (remote transmission request).
-
CAN_ERR_FLAG: Indicates an error frame.
can_dlc: Data length code, indicating the number of bytes actually transmitted in the data field data. The value of DLC ranges from 0 to 8, and the CAN frame can carry a maximum of 8 bytes of data.
data: A byte array used to store the actual transmitted data, which can hold a maximum of 8 bytes.






Source: Trump. yang
*Disclaimer: This article is original or forwarded by the author. If we inadvertently infringe on any party’s intellectual property rights, please inform us and we will delete it immediately. The above images and texts are sourced from the internet, if there is any infringement, please contact us in time, and we will delete it within 24 hours. The content of the article is the author’s personal opinion, and the Automotive Ethernet Technology Research Laboratory reposts it only to convey a different perspective, which does not represent the Automotive Ethernet Technology Research Laboratory’s approval or support of this view. If there are any objections, please feel free to contact the Automotive Ethernet Technology Research Laboratory.
https://blog.csdn.net/weixin_46999174/article/details/142338017