C language example_sum check algorithm

1. Algorithm introduction

Checksum is a simple error correction algorithm used to detect or verify errors in data transmission or storage. It calculates and generates a checksum on the data, then appends the checksum to the data, and calculates the checksum again at the receiving end and compares it to determine whether the data is complete and correct.

Checksum algorithms typically use bitwise operations to compute the checksum.

Common sum check algorithms are as follows:

(1) Simple Sum Checksum: Add all the bytes in the data and compare the result with a predefined checksum. If the two are equal, the data is not in error.

(2) CRC (Cyclic Redundancy Check): It uses division to calculate the checksum, which has higher error detection ability. The CRC algorithm uses a fixed generator polynomial to divide the data and generate a remainder as a checksum.

The sum check algorithm can be used in a variety of different application scenarios:

(1) Data transmission: When data is transmitted through network transmission, serial port communication or other communication channels, the sum check can detect bit errors or transmission errors that occur during the transmission process to ensure the integrity and accuracy of the data.

(2) Storage verification: When writing data on or reading data from a data storage medium, sum verification can help detect media failure or data corruption.

(3) File verification: In scenarios such as downloading files, backing up files, or transferring files, sum verification can be used to verify the integrity of the file to ensure that the file has not been tampered with or damaged.

(4) Database verification: In a database system, sum verification can be used to detect data integrity and prevent data from being wrong or damaged during storage or transmission.

The sum check algorithm is a simple but practical error correction algorithm for detecting errors in data transmission or storage, and has been widely used in many applications to ensure data integrity and accuracy.

image-20230626213659368

2. Code implementation

Scenario: In single-chip communication, the single-chip needs to send a piece of data to the host computer. For example, stored in char buff[1024]; this array. Two functions need to be encapsulated. The MCU calls the function to perform a sum check on this piece of data, encapsulates the check value, and then the host computer verifies the checksum after receiving the data, and checks whether the data is transmitted correctly.

2.1 MCU-side encapsulation function (send data and calculate and verify)

// 计算校验和
unsigned char calculateChecksum(const char* data, int length) {
    
    
    unsigned char checksum = 0;
    for (int i = 0; i < length; i++) {
    
    
        checksum += data[i];
    }
    return checksum;
}

// 发送数据并附加校验和
void sendDataWithChecksum(const char* data, int length) {
    
    
    // 发送数据...
    
    unsigned char checksum = calculateChecksum(data, length);
    
    // 发送校验和
    // 若使用UART通信,可以使用以下代码发送校验和,并确保上位机端能够解析它
    // sendByte(checksum); // 发送校验和
}

In the above code, calculateChecksumthe function is used to calculate the checksum of the data, adding each data byte and returning the checksum value. sendDataWithChecksumThe function is used to calculate the checksum before sending data, and send the checksum to the host computer.

2.2 Encapsulation function of host computer (receive data and verify checksum)

e// 验证校验和
bool verifyChecksum(const char* data, int length, unsigned char receivedChecksum) {
    
    
    unsigned char calculatedChecksum = calculateChecksum(data, length);
    return calculatedChecksum == receivedChecksum;
}

// 接收数据并验证校验和
void receiveDataWithChecksum(const char* data, int length) {
    
    
    // 接收数据...
    
    unsigned char receivedChecksum = receiveChecksum(); // 假设从上位机接收到校验和的值
    
    if (verifyChecksum(data, length, receivedChecksum)) {
    
    
        // 校验通过,数据传输正确
        // 处理数据...
    } else {
    
    
        // 校验失败,数据传输错误
        // 进行相应的处理...
    }
}

On the host side, verifyChecksumthe function is used to verify that the checksum matches the received checksum. receiveDataWithChecksumThe function is used to receive data and checksum, and call verifyChecksumthe function to verify. If the verification is passed, the data transmission is correct; otherwise, the data transmission is wrong.

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/132269319
Recommended