In-depth discussion of the working principle and application scenarios of the vehicle CAN protocol

CAN overview

CAN (Controller Area Network) bus protocol is a data communication protocol originally developed by Bosch for internal communication in the automotive field.

The CAN bus protocol is a serial communication protocol that supports communication between multiple hosts and multiple slaves, and can transmit information between different control units.

The basic principle of the CAN bus protocol is to realize data transmission by sending and receiving digital signals on the transmission line. Each node on the CAN bus has a unique identifier, which is used to determine the data sent and received by each node.

The CAN bus protocol has the following characteristics:

  1. High reliability and real-time performance: The CAN bus protocol uses a differential signal transmission method, which can effectively resist noise interference and ensure the reliability of data transmission. At the same time, the CAN bus protocol has high real-time performance, which can meet the real-time requirements of the vehicle system for data transmission.
  2. High bandwidth: The CAN bus protocol supports high-speed data transmission and can transmit a large amount of data.
  3. Flexibility: CAN bus protocol can support communication between multi-master and multi-slave, and can transmit information between different control units.
  4. Easy to integrate: CAN bus protocol can be integrated into various control units to realize information exchange between systems.

Data reading process of CAN protocol

The data reading process of the CAN protocol usually includes the following steps:

  1. Initialize the CAN controller. In the initialization process, parameters such as the clock source and baud rate of the CAN controller need to be configured.
  2. Set filters. Since the CAN bus is a broadcast communication, all nodes will receive the data. In order to avoid processing useless data, you can set a filter to only receive data with a specific identifier.
  3. Start the CAN controller. During the startup process, the CAN controller will start to monitor the data on the CAN bus and store the received data in the receive buffer.
  4. read data. When the CAN controller receives the data, it will trigger an interrupt event, and the application program can read the data in the receive buffer through the interrupt service routine.
  5. Data processing. After reading the data, the application needs to parse and process the data, such as extracting data fields, calculating sensor values, etc.

CAN vehicle status detection

CAN bus is a common communication bus in automotive electronics, which can realize communication and data exchange between different controllers in the car. Through the CAN bus, various status information of the vehicle can be read, such as vehicle speed, rotation speed, water temperature, etc., so as to realize the monitoring and diagnosis of the vehicle status.

The reading of CAN bus data requires the use of OBD (On-Board Diagnostics) scanner and corresponding reading software. The OBD scanner can be connected to the CAN bus of the vehicle through the OBD interface, and then obtain the vehicle status information by reading the data uploaded by the CAN bus.

Before reading CAN bus data, you need to understand the CAN bus protocol of the vehicle. The CAN bus adopts the distributed control method, which can realize the communication between multiple controllers, and its communication protocol is divided into two types: standard frame and extended frame. Standard frames occupy 11-bit identifiers, and extended frames occupy 29-bit identifiers. The data frame can carry 0~8 bytes of data, and the remote frame is used to request data from other nodes without carrying data. The CAN bus adopts differential transmission, which has the advantages of strong anti-interference ability and high transmission rate.

In practical applications, the OBD scanner and corresponding reading software can be used to read the data uploaded by the CAN bus, and then analyze and process the data to obtain the status information of the vehicle. OBD scanners and reading software can be connected to computers or mobile devices through Bluetooth or USB, so as to realize real-time reading and display of data, which is convenient for users to monitor vehicle status and diagnose faults.

OBD combat reading

OBD (On-Board Diagnostics, on-board self-diagnosis) is a system used by automotive electronic control systems to monitor the working status of vehicles. It can detect the working conditions of the engine, transmission, chassis, instrument panel and other components, and output the diagnostic results through the vehicle diagnostic interface (OBD interface).

In the OBD system, the vehicle has a built-in standard diagnostic interface, which can communicate with the vehicle electronic control system through the interface to obtain vehicle status and other information. Common standards for OBD systems include OBD-I, OBD-II, EOBD, etc. Among them, OBD-II is the standard stipulated by the American Automobile Chamber of Commerce and is widely used in Europe and the Americas.

In order to read the information output by the OBD interface, you need to use an OBD diagnostic instrument or an OBD scanner. These devices can communicate with the automotive electronic control system through the OBD interface to obtain information such as vehicle status and engine fault codes.

In practical applications, OBD scanners are usually used in combination with mobile devices (such as mobile phones, tablet computers, etc.), and transmit data to mobile devices through wireless communication methods such as Bluetooth or Wi-Fi to facilitate users to view and analyze vehicle status. At the same time, there are also some specialized OBD software that can help users realize the reading and analysis of OBD data, such as Torque, DashCommand, etc.

Vehicle CAN protocol, use case code interpretation

The vehicle CAN protocol is a communication protocol used between automotive electronic control systems, and is often used in application scenarios such as vehicle status detection and data reading. The following is an interpretation of a use case code:

javaCopy codepublic class CanManager {
    private static final String TAG = "CanManager";
    private static final int SOCKET_TIMEOUT = 10000;
    private static final int READ_BUFFER_SIZE = 128;
    private static final String DEFAULT_CAN_DEVICE = "/dev/ttyS4";
    private static final String DEFAULT_CAN_BAUDRATE = "500000";
    private static final String DEFAULT_CAN_FILTER = "18DAF100";
    private static final String CMD_GET_VER = "ATI";
    private static final String CMD_GET_STATUS = "ATZ";
    private static final String CMD_OPEN_CAN = "ATSPB";
    private static final String CMD_CLOSE_CAN = "ATPC";
    private static final String CMD_SET_BAUDRATE = "ATBRD";
    private static final String CMD_SEND_CAN_FRAME = "ATSH";
    private static final String CMD_RECV_CAN_FRAME = "ATMA";
    private static final String CMD_SET_FILTER = "ATCF";
    private static final String CMD_CLEAR_FILTER = "ATCFA";
    private static final String CMD_SET_MASK = "ATCM";
    private static final String CMD_CLEAR_MASK = "ATCMA";
    private static final String CMD_SAVE_PARAMS = "AT&W";
    private static final String CMD_EXIT = "ATZ";
    private static final String CMD_RESET = "ATRS";
    private String mCanDevice;
    private int mCanBaudrate;
    private int mFilterId;
    private int mFilterMask;
​
    public CanManager() {
        mCanDevice = DEFAULT_CAN_DEVICE;
        mCanBaudrate = Integer.parseInt(DEFAULT_CAN_BAUDRATE);
        mFilterId = Integer.parseInt(DEFAULT_CAN_FILTER, 16);
        mFilterMask = 0x7FF;
    }
​
    public boolean open() {
        // 打开串口
        SerialPort serialPort = new SerialPort(new File(mCanDevice), mCanBaudrate, 0);
        if (serialPort == null) {
            Log.e(TAG, "open: serialPort is null");
            return false;
        }
​
        // 初始化CAN控制器
        if (!initCanController(serialPort)) {
            Log.e(TAG, "open: initCanController failed");
            serialPort.close();
            return false;
        }
​
        return true;
    }
​
    public boolean close() {
        // 关闭CAN控制器
        if (!closeCanController()) {
            Log.e(TAG, "close: closeCanController failed");
            return false;
        }
​
        return true;
    }
​
    public boolean sendCanFrame(int canId, byte[] data) {
        // 发送CAN帧
        if (!sendCanFrame(canId, data, data.length)) {
            Log.e(TAG, "sendCanFrame: sendCanFrame failed");
            return false;
        }
​
        return true;
    }
​
    public CanFrame recvCanFrame() {
        // 接收CAN帧
        byte[] buffer = new byte[READ_BUFFER_SIZE];
        int size = recvCanFrame(buffer);
        if (size < 0) {
            Log.e(TAG, "recvCanFrame: recvCanFrame failed");
            return null;
        }
​
        return new CanFrame(buffer, size);
    }
​
    private boolean initCanController(SerialPort serialPort) {
        // 发送初始化命令
        if (!sendCmd(CMD_GET_VER)) {
            Log.e(TAG, "

The article mainly explains some aspects of the vehicle-mounted CAN bus protocol; there is still a lot to learn about the vehicle-mounted CAN bus technology. For more vehicle-related information, please refer to the "Vehicle Development Technical Manual" and click to view the detailed categories .

in conclusion

Among these topics related to in-vehicle systems and CAN bus protocol, it can be summarized as follows:

  1. The vehicle system refers to the system used in the car, including entertainment, navigation, vehicle monitoring and other functions, and is composed of software and hardware systems.
  2. Dilink is a vehicle-mounted system, which is mainly used to realize functions such as in-car entertainment, navigation, and communication. It needs to implement technologies such as SOA architecture and CAN bus protocol.
  3. CAN bus protocol is a commonly used communication protocol in automotive electronic control systems (ECUs), used to realize data exchange between various systems, and is often used in vehicle status monitoring, fault diagnosis, etc.
  4. OBD (On-board Diagnostics) is a self-diagnostic system on the car, which can detect the state of the car and read the fault code.
  5. In the actual development, it is necessary to master relevant hardware and software knowledge, understand the principle of CAN bus protocol and data reading, master the use of OBD, etc., in order to realize various functions of the vehicle system. At the same time, it is necessary to pay attention to issues such as security and stability.

Guess you like

Origin blog.csdn.net/m0_71524094/article/details/130325089