Output frame type standard frame or extended frame from CAN message || remote frame or data frame

Table of contents

What is the CAN bus for?

The entire link layer in CAN handles the data flow

Bus president in CAN bus

ISO 11898(High speed)

Data frame in CAN bus

Remote control frame of CAN bus

Error frame on CAN bus

CAN bus overload frame

Bit timing in CAN

Hard synchronization in CAN

Resynchronization in CAN

Realization of outputting frame type code from CAN message


CAN bus is the abbreviation of Controller Area Network , which is a standard for controller area network.

The following are some important knowledge points of CAN bus:

  1. CAN bus is a serial communication protocol, which can realize distributed real-time control.
  2. The data transmission speed of the CAN bus can reach up to 1Mbps, and the communication distance can reach up to 10km.
  3. The CAN bus uses non-destructive arbitration technology to ensure that when multiple nodes send data at the same time, the node with low priority will actively stop sending to avoid data conflicts.
  4. The CAN bus uses CRC check technology to ensure the reliability of data transmission.
  5. The CAN bus supports standard format and extended format. The identifier bits of the standard format are 11 bits, and the identifier bits of the extended format are 29 bits.
  6. The CAN bus uses a synchronous jump mode, which can realize high-speed data transmission and low power consumption.
  7. The CAN bus can use a multi-master structure, multiple nodes can send data at the same time, and the order of data transmission can also be controlled by node priority.
  8. CAN bus can be integrated with other networks to realize distributed control and monitoring.

In a word, CAN bus is a powerful serial communication protocol with advantages of high speed, long range, reliability, energy saving, etc. It is widely used in industrial automation, automotive electronics, medical equipment, smart home and other fields.

What is the CAN bus for?


The CAN bus is a powerful vehicle bus design that allows standard microcontrollers and devices to communicate without each other's applications. It is a message-based protocol originally designed for multi-way electrical wiring in automobiles to save copper wires, but it can be used in many other environments as well. CAN bus is widely used in industrial automation, ships, medical equipment, industrial equipment and other fields to achieve short-distance communication with large data volume or long-distance small data volume, and has relatively high real-time requirements, multi-master and multi-slave or each node is equal used in the field.

Features of the CAN bus include:

  1. There is no distinction between master and slave in data communication. Any node can initiate data communication to any other (one or more) nodes, and the order of communication is determined by the priority of each node's information.
  2. When multiple nodes initiate communication at the same time, the ones with lower priority avoid the ones with higher priority, which will not cause congestion on the communication line.
  3. The communication distance can reach up to 10KM (the rate is less than 5Kbps), and the rate can reach 1Mbps (the communication distance is less than 40M).
  4. CAN bus transmission medium can be twisted pair, coaxial cable.

In short, CAN bus is a communication protocol widely used in various fields, with powerful functions and efficient data transmission capabilities.

The entire link layer in CAN handles the data flow


The CAN ( Controller Area Network ) bus is a standard for controller area networks. The entire CAN link layer processing data flow includes the following steps:

  1. Data segment encoding: Encode application data into data segments (Data Segment). The data segment consists of data bytes and Remote Transmission Request (RTR) bits. The data byte can be 8 or 16 bits, and the RTR bit indicates whether it is a data frame or a remote control frame.
  2. Frame type encoding: Encode the data segment and frame type (Remote Transmission Request, Data, Error, Overload) together into a frame format (Frame Format).
  3. Priority encoding: Encode the frame type and priority (0-15) together into a Priority Segment.
  4. Arbitration Field Encoding: Encode the priority field and the identifier (Identifier) ​​into an arbitration field (Arbitration Field).
  5. Data field encoding: Encode the data segment and the reserved bit (Reserved Bit) together into a data field (Data Field).
  6. Check field encoding: encode the check field (CRC Field) and the response field (ACK Field) together into a check field (Checksum Field).
  7. Frame transmission: Send the entire CAN frame (including frame start, arbitration field, data field, check field and frame end) to the CAN bus.
  8. Error detection and processing: After the receiving node receives the CAN frame, it will perform error detection and processing. If an error is detected, the receiving node will send an error flag (Error Flag) to the bus to notify the sending node to resend the data.
  9. Data reception and processing: After receiving the correct CAN frame, the receiving node will decode it into application data and process it accordingly.

In short, the CAN link layer processing data flow includes data segment encoding, frame type encoding, priority encoding, arbitration field encoding, data field encoding, check field encoding, frame transmission, error detection and processing, and data reception and processing. These steps ensure that the data transmission on the CAN bus is stable and reliable.

Bus president in CAN bus


The bus president in CAN bus refers to the device or component that controls the physical layer and data link layer of CAN bus communication . The bus president is responsible for managing the data transmission on the bus to ensure the correctness and reliability of data transmission.

In the CAN bus, the bus president is usually held by the CAN controller or CAN transceiver. The CAN controller is the device responsible for managing the data link layer, which receives data from the application and sends it on the CAN bus. The CAN transceiver is a device responsible for managing the physical layer and the data link layer. It receives data from the CAN controller and sends it to the CAN bus, and at the same time receives data from the CAN bus and sends it to the CAN controller. .

The bus president plays an important role in the CAN bus, and its performance and reliability directly affect the performance and reliability of the CAN bus. Therefore, choosing a suitable bus controller is the key to ensure the reliability of CAN bus communication.

ISO 11898(High speed)


ISO 11898 is a communication protocol standard for road traffic information systems. The standard specifies two communication modes, namely high speed ( High speed ) mode and low speed ( Low speed ) mode.

The high-speed mode communication protocol is mainly used in short-distance communication, such as communication between vehicles and roadside equipment. In this mode, the communication rate can reach up to 1Mbps , and the communication distance can reach up to 100m . The high-speed mode communication protocol adopts the CAN bus protocol, so it has all the advantages of the CAN bus protocol, such as non-destructive arbitration, CRC check, synchronous jump mode, etc.

The low-speed mode communication protocol is mainly used in long-distance communication, such as vehicle-to-vehicle communication. In this mode, the communication rate is low, up to 125Kbps , and the communication distance is up to 10km . The low-speed mode communication protocol also adopts the CAN bus protocol, but in order to reduce power consumption and cost, some functions of the CAN bus protocol are simplified or omitted.

The application of the ISO 11898 standard can realize information sharing and interaction between vehicles and roadside equipment, and improve road traffic safety and efficiency.

Data frame in CAN bus


The data frame in the CAN bus is the basic unit for transmitting data. A data frame consists of seven segments, including:

  1. Frame start: Indicates the start of a data frame, indicated by a dominant level of 1 bit .
  2. Arbitration section: Indicates the priority of the frame, including identifier bits and remote transmission request bits. The identifier bit is an 11-bit functional address, and the CAN receiver filters data frames through the identifier. The remote transmit request bit (RTR) indicates whether the frame is a remote frame or a data frame.
  3. Control segment: A segment indicating the number of bytes of data and reserved bits, consisting of 6 bits, indicating the number of bytes of the data segment.
  4. Data segment: Contains the actual data to be transmitted, up to 8 bytes can be sent.
  5. CRC section: check the transmission error of the frame, and use 16-bit CRC for verification.
  6. ACK segment: Indicates confirmation of normal reception, consisting of 2 bits.
  7. End of frame: Indicates the end of the data frame, indicated by a recessive level of 7 bits .

There are two formats of data frames in the CAN bus: standard format and extended format. The standard format has 11 identifier bits and the extended format has 29 identifier bits. In the standard format, the highest weight bit ( MSB ) of the identifier bit is located in ID10 , the lowest weight bit ( LSB ) is located in ID0 , and is transmitted in the order of ID10~ID0 . In the extended format, the highest weight bit ( MSB ) of the identifier bit is located in ID28 , and the lowest weight bit ( LSB ) is located in ID0 , and is transmitted in the order of ID28~ID0 .

In short, the data frame in the CAN bus is a structure for transmitting data, which has a complete structure and strict rules to ensure the reliability and stability of data transmission.

Remote control frame of CAN bus


The remote control frame of the CAN bus is a frame used by the receiving unit to request data from the sending unit. The remote control frame is composed of 6 segments, including frame start, arbitration segment, control segment, CRC segment, ACK segment and frame end.

The composition of the remote control frame is as follows:

  1. Frame start: Indicates the start of the remote control frame, indicated by a dominant level of 1 bit .
  2. Arbitration section: Indicates the priority of the frame, including identifier bits and remote transmission request bits. The identifier bit is an 11-bit functional address, and the CAN receiver filters remote control frames through the identifier. The remote transmit request bit (RTR) indicates whether the frame is a remote frame or a data frame.
  3. Control segment: A segment indicating the number of bytes of data and reserved bits, consisting of 6 bits, indicating the number of bytes of the data segment.
  4. CRC section: check the transmission error of the frame, and use 16-bit CRC for verification.
  5. ACK segment: Indicates confirmation of normal reception, consisting of 2 bits.
  6. Frame end: Indicates the end of the remote control frame, indicated by a 7 -bit recessive level .

The difference between a remote control frame and a data frame is that a remote control frame has no data segment, while a data frame has a data segment. The remote control frame is used by the receiving unit to request data from the sending unit, so there is no data segment. The RTR bit of the remote control frame is always recessive 1 , and the RTR bit of the data frame is always dominant 0 .

In short, the remote control frame of the CAN bus is a frame used by the receiving unit to request to send data to the sending unit. It consists of 6 segments, including the frame start, arbitration segment, control segment, CRC segment, ACK segment and frame end.

Error frame of CAN bus


The error frame of the CAN bus is a frame for detecting and handling errors on the bus. When a node on the bus detects an error, it sends an error frame to notify other nodes that an error has occurred.

An error frame consists of 8 segments, including:

  1. Frame start: Indicates the start of an error frame, indicated by a dominant level of 1 bit.
  2. Arbitration section: Indicates the priority of the frame, including identifier bits and remote transmission request bits. The identifier bit is an 11-bit functional address, and the CAN receiver filters error frames through the identifier. The remote transmission request bit (RTR) is always recessive 1.
  3. Control segment: Indicates the type of error, including active error flags and passive error flags. The active error flag is an error flag output when an error is detected by the unit in the active error state, and it is a dominant level of 6 bits. The passive error flag is an error flag output when an error is detected by a unit in a passive error state, which is a 6-bit recessive level.
  4. Error delimiter: It is composed of 8-bit recessive level, indicating the end of the error frame.

Types of error frames include:

  1. Active error frame: When a node detects an error on the bus, it sends an active error frame to notify other nodes that an error has occurred.
  2. Passive error frame: When a node detects an error in another node, it sends a passive error frame to notify itself and other nodes that an error has occurred.

Handling of error frames includes:

  1. Resend: When a node detects an error, it resends a data frame or telecontrol frame to avoid data loss or communication failure.
  2. Shut down the bus: When a node detects a serious error, it will send an active error frame and shut down the bus to avoid the spread of errors and loss of data.

In short, the error frame of the CAN bus is a frame used to detect and process errors on the bus. It consists of 8 segments, including the start of the frame, the arbitration segment, the control segment, and the error delimiter. The types of error frames include active error frames and passive error frames, and the processing methods include resending and closing the bus.

CAN bus overload frame


The overload frame ( Overload Frame ) is a special frame in the CAN bus, which is used to notify other nodes to temporarily stop sending data when the receiving node is overloaded. The format of the overload frame is different from the standard data frame and remote control frame. It consists of an overload flag ( Overload Flag ) and an overload delimiter ( Overload Delimiter ).

The overload flag is a special flag composed of 6 dominant bits, which is used to indicate that the receiving node is overloaded. The overload delimiter is a flag consisting of 8 recessive bits used to indicate the end of the overload frame. When the receiving node detects the overload flag, it will immediately stop receiving data and send an overload frame to the bus. After receiving the overload frame, other nodes will temporarily stop sending data, waiting for the receiving node to restart sending notification.

The role of the overload frame is to prevent data congestion during data transmission, so that nodes can coordinate data transmission and ensure data stability and reliability.

Bit timing in CAN


In the CAN bus, bit timing refers to the time sequence during bit transmission. It includes the following parameters:

  1. Synchronization segment: used to synchronize the transmission time of each bit, consisting of 11 bits of dominant level.
  2. Propagation segment: It is used to represent the delay of bit transmission time, which consists of sign bit and interval bit. The sign bit indicates the end of the previous bit, and the interval bit indicates the time interval between the sign bit and the data bit.
  3. Data segment: used to transmit actual data, consisting of 5 bits of dominant level or recessive level.
  4. Acknowledgment section: used to confirm received data, consisting of a recessive level of 1 bit.
  5. Frame end: used to indicate the end of a frame of data, consisting of 7-bit recessive levels.

In the CAN bus, the importance of bit timing is to ensure the stability and reliability of data transmission. The correct bit timing can ensure the synchronization between nodes and the correctness of data transmission. Therefore, the bit timing in the CAN bus is carefully designed to ensure its stability and reliability.

Hard synchronization in CAN


In the CAN bus, hard synchronization refers to synchronizing the received signals to avoid data loss or communication failure. Hard synchronization is achieved by using the sync segment, which consists of 11 bits of dominant level . When the receiving node receives the sync segment, it resets the clock signal and starts waiting for the next bit. If the received bit is recessive, the receiving node will continue to wait for the next bit. If the received bit is dominant, the receiving node updates the clock signal to the current bit and starts receiving the next bit. By using hard synchronization, the receiving node can ensure that the received data is synchronized with the data sent by the sending node, thus avoiding data loss or communication failure.

Resynchronization in CAN


In the CAN bus, re-synchronization refers to the process of re-synchronizing with the sending node when the receiving node detects a loss of synchronization or a phase jump. The importance of resynchronization is to ensure that the data received by the receiving node is synchronized with the data sent by the sending node to avoid data loss or communication failure.

The principle of resynchronization is that during data transmission, the sending node will add an additional bit at the end of each byte to help the receiving node to synchronize. When the receiving node detects a loss of synchronization or a phase jump, it sends a request frame asking the sending node to resend the data. After receiving the request frame, the sending node will resend the data and add an additional bit at the end of each byte to help the receiving node to synchronize.

The resynchronization process consists of the following steps:

  1. When the receiving node detects a loss of synchronization or a phase jump, it sends a request frame to request the sending node to resend the data.
  2. After receiving the request frame, the sending node will resend the data and add an additional bit at the end of each byte to help the receiving node to synchronize.
  3. When the receiving node receives the additional bits, it treats it as a sync segment and resynchronizes with the sending node.
  4. Once the receiving node resynchronizes with the sending node, it continues to receive data, error detection and handling.

In conclusion, resynchronization is an important mechanism in CAN bus to ensure stable and reliable data transmission. It helps the receiving node re-synchronize with the sending node by adding additional bits during data transmission, thus avoiding data loss or communication failure.

Realization of outputting frame type code from CAN message


Give a string of hexadecimal numbers 07 11 22 33 44 55 66 77 88 99 12 into binary numbers. The first 3 bytes are the description part, byte 1 is the frame information, and the seventh bit (FF) in byte 1 Indicates the frame format, FF=0 in standard frame, FF=1 in extended frame, the sixth bit (RTR) in byte 1 indicates the type of frame , RTR=0 in byte 1 indicates data frame, RTR=1 indicates remote frame .

DLC indicates the actual data length in the data frame.

The ID of a standard data frame has 11 bits. Sent sequentially from ID10 to ID0. Prohibit the upper 7 bits from being invisible (prohibited setting: ID=1111111XXXX)

Bytes 2~3 are message identification codes, and the upper 11 bits are valid.

Byte 4~11 is the actual data of the data frame, and it is invalid for the remote frame.

#include <stdio.h>

typedef struct {
    int frameFormat; // 帧格式:0表示标准帧,1表示扩展帧
    int frameType; // 帧类型:0表示数据帧,1表示远程帧
    int dataLength; // 数据帧长度
} FrameInfo;

FrameInfo analyzeFrame(unsigned char* hexData) {
    FrameInfo info;
    unsigned char byte1 = hexData[0];


    // 解析帧格式
    info.frameFormat = (byte1 >> 7) & 0x01;

    // 解析帧类型
    info.frameType = (byte1 >> 6) & 0x01;

    // 解析数据帧长度
    if (info.frameType == 0) {
        info.dataLength = byte1 & 0x0F;
    } else {
        info.dataLength = 0; // 远程帧时数据帧长度为0
    }

    return info;
}

int main() {
    unsigned char hexData[] = {0x07, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x12};
    FrameInfo info = analyzeFrame(hexData);

    printf("帧格式: %s\n", info.frameFormat == 0 ? "标准帧" : "扩展帧");
    printf("帧类型: %s\n", info.frameType == 0 ? "数据帧" : "远程帧");

    if (info.frameType == 0) {
        printf("数据帧长度: %d\n", info.dataLength);
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/131480167