cancel
Showing results for 
Search instead for 
Did you mean: 

CRC check for received data through UART

sireevenkat1
Senior
Hi,

I am sending/receiving data using UART between STM32G4 board and other board. While I am sending the data I am adding the CRC and transmitting it through UART to other board. While receiving how do I check the CRC correct or not.

For Eg: From other board to my STM32G4 board I recivedata frame is as below

header 

Length 

Command 

Data 

CRC 

EOF 

0x32

0x05 

0x17

 0x00/0x01/0x02

 

0x89 

how do I check the CRC is correct or not in the coding?

Do I need to calculate crc for all 3 possible data values and compare the possible crc with received crc?

is this correct way. Can anyone suggest how to check the CRC.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

You'd need the details of the polynomial used, align your frames as received and compute the correct value for the data received, and compare that.

Write a CRC computation that can take the data a byte at a time, the packet format looks to have flexibility, so all packets are not the same size.

Link to documentation for the protocol. Find a number of valid examples for packets so you can valid the computation based on polynomial, shift direction, initial values, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4
LCE
Principal

Transmitter and Receiver should use the same algorithm for calculating the CRC, only that the receiver compares the result to the CRC sent by the transmitter.

You'd need the details of the polynomial used, align your frames as received and compute the correct value for the data received, and compare that.

Write a CRC computation that can take the data a byte at a time, the packet format looks to have flexibility, so all packets are not the same size.

Link to documentation for the protocol. Find a number of valid examples for packets so you can valid the computation based on polynomial, shift direction, initial values, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Karl Yamashita
Lead III

Here is code that I use for a simple CRC. You'll have to pass the Command, Data and CRC when validating and just Command and Data when calculating the CRC. 

/*
 * Description: Validate checksum using modulo 256
 * Input: the data array and the length of array including the checksum
 * Output: true if valid checksum else false
 *
 */
bool ValidateChkSum(uint8_t *data, uint8_t len){
    uint32_t chksum = 0;
    uint32_t i = 0;
    for(i = 0; i < len - 1; i++){
        chksum += *data++;
    }
    if((chksum % 256) != *data){
        return false;
    }
    return true;
}

/*
 * Description: Calculate checksum using modulo 256 and adds checksum to array at last index + 1
 * Input: The data array and the length of array minus the checksum index
 * Output:
 *
 */
void CalculateChkSum(uint8_t *data, uint8_t len){
    uint32_t chksum = 0;
    uint32_t i = 0;

    for (i = 0; i < len - 1; i++)
    {
        chksum += *data++;
    }
    *data = (chksum % 256); // copy checksum to last index
}

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

>>Do I need to calculate crc for all 3 possible data values and compare the possible crc with received crc?

No you'd compute on-the-fly, otherwise the table of possible values for 3 bytes would run to 16 million..

Typically it would include the length and command fields, so you can fully validate the whole content of the messages/packet.

Provide a cite to the specific protocol you are using here.

>>Can anyone suggest how to check the CRC.

Have some documentation showing known working examples, or collect some valid / good data from the serial port to inspect and test against.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..