I'm making communicate 2 arduinos nano through the UART.
To be sure the frame is been received correctly I'm using 2 bytes of CRC calculated using a library.
To verify that the frame is been received correctly I could simply recalculate the crc value of the message without the last 2 bytes and compare it with the bytes that I've received, exactly like a cheksum.
But that sounds a little bit weird considering that the CRC is been designed so that the validation could be done using a simple modulo operation on the message.
The problem is that I can't do a modulo operation on a array treating it as it was an integer value and even if I write down the code to do it, I'm pretty sure that it would spend much more time then the re-calculation of the CRC since the last one is made just with a bunch of xor and shift.
So I'm here to ask if someone knows which is the best practice in this case.
There are many different CRCC methods. If you don't like this one, use a different one. But first, have you examined WHY you may need a CRCC? Do you have a protocol to recover a message that has a CRC error?
Yes, It's a very simple ack system, If the receiver gets the message correctly it sends a ack message back. If the sender does not receive the ack (or received a corrupted ack) It sends the message again.
For the application it is not important if a message is received twice so I don't think there is any problem in case the ack message got corrupted.
// uint8_t update_crc_8( unsigned char crc, unsigned char val );
// Given a databyte and the previous value of the CRC value, the function
// calculates and returns the new actual CRC value of the data comming in.
uint8_t update_crc_8( unsigned char crc, unsigned char val ) {
return sht75_crc_table[val ^ crc];
} /* update_crc_8 */
from librc - it enables me when creating a frame to generate the crc as the data is being writen to an array or even transmitted and then added on the end of the frame before the frame terminator
maybe I'm not getting it, but it seems to me that this code is useful for calculating the crc of a new data frame.
I don't need to calculate it: I have a data frame composed of n bytes where the last 2 bytes are the crc of the frame.
Now I would like to verify if the frame is been corrupted, so I have to choose the right way to do that: Calculating again the crc and comparing it to the last two bytes is an option but I'm not sure if it's the right way to go.
if you receive a frame you have to recalculate the CRC and compare it with the CRC on the end of the frame to check for errors
if it comes in over a serial line I can calculate the CRC as each byte arrives using the example of post #4
if I gets a complete frame, e.g. from UDP, use the alternative from librc
/*
* uint8_t crc_8( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_8() calculates the 8 bit wide CRC of an input string of a
* given length.
*/
uint8_t crc_8( const unsigned char *input_str, size_t num_bytes ) {
as I tend to transmit arrays of bytes I use a crc_8