Cyclic Redundancy Check(CRC) code in arduino

I have two arduino uno boards. They are connected as server and client. Each board has its own SD card. I am transferring a file via ethernet from one SD card of a server board to the another SD card of client board. Now, my question is, how to use CRC to calculate the error while transferring a file from server to the client? Do I have to have CRC code running at both server and client. I will appreciate if anyone could help me. Help with written code would be great.

The normal way is to simply exclusive OR all the bytes in your message and send that number in the last byte of your message. Then at the other end you exclusive OR all the bytes as they come in and see if the result is the same as the last byte that comes in.

Hi! Grumpy_Mike,

Thank you for the reply. Well! If I were sending a string message from one arduino to another then yes it would be easier but I am grabding a file from the SD card in server, and client will read that file and write it to its SD card. So, the way I did is, I have a code that calculate the CRC of the file. I have this code in the server but I do not know what to do in the client. How to have the client compare the CRC generated by the server? I am not good at coding. I found the CRC code generator online. Do you have some ideas? Can I please have a simple format of code for CRC generator in both server and the client board? Thanks in advance.

The code in the client will be the same as that in the server. There are two ways to check the CRC. One is for the client to generate the CRC of the received data and compare the result with that generated by the server. The other is to append the CRC generated by the server to the data and then the client generates a new CRC of all the data and the server CRC. The result should be zero.


Hi! Pete,
Thanks for the explanation. So, the CRC code I wrote in the server generated hexadecimal value. And according to your post, One of the ways I could do is run the same code in the client side too and compare the value. I actually tried this before and got the same value. If the values are same then there is no error and if I get the different value then there is an error in the file transfer. Then my other question would be, how to figure out number of errors occured during transfer if I get different CRC number in server and client? All I am trying to do is calculating the Bit Error Rate (BER).


If the values are same then there is probably no error.

I don't know if you can get a BER from a single check on the entire file though.


If the CRCs differ, it only means that there was at least one bit in error. It doesn't tell you how many bits were bad. To do that you would have to compare the received message against the original, in which case the CRC is redundant.


Hi! Rob and Pete,

Thank you for your suggestion. Do you guys have ideas in calculating BER then? As I mentioned earlier I am too new for all this stuff. A complete code would be much more helpful for me. Thanks Pooja

To calculate the BER you have to transmit the file in a number of small packages, each one needs to carry some form of data correction code. This is where you can reconstruct the code in case of an error. This normally has the same byte encoded three times and the two that point to the same result are deemed to be correct. If all three are the same then there is no error. This is known as redundant encoding or error correcting encoding.

This is an example of what you can use.

This is not an easy thing to measure and is very dependent on your encoding.

As I mentioned earlier I am too new for all this stuff.

This is not beginners stuff. Why do you want to measure it and what do you expect to do with the result? It is the sort of thing that is used in digital TV.

Note this is not the question you originally asked.

Hi! Mike,

I am fully aware that we are moving away from the original question. At first I though that I could do BER using CRC code generation so I had my question about the CRC. But you guys helped me realise that with CRC code I can only compare the result to see if my transmitted file has error or not but can’t calculate BER. And my main goal is to calculate BER. So, I am moving to a different direction now. I have already gone through the websites you posted. I understood the theory part but I do not know how to implement it in my code. Reason I want to calculate BER is that I want to have attenuator connected in between my two arduinos and compare the Bit Error Rate with respect to the noise. I know I am too new for all this but I have to get the result. I am desperately in need of you guys help to figure out the code.

but I have to get the result

This sounds like some sort of assignment?

I want to have attenuator connected in between my two arduinos and compare the Bit Error Rate with respect to the noise.

No that will not work. You need to inject a known amount of noise into the system so you have a known signal to noise ratio. Then you can compare that against the bit error rate.

One way to do what you want is to send random bytes from one system to the other. If you start with the same fixed random seed in both programs then both numbers will match. So you can directly compare what you get with what you should get. If you exclusive OR what you get with what you receive you will get zero for no error. Every non zero result represents an error. You then have to count the non zero bits to find the bit error rate in that byte. Then you repeat this for a time to get the overall bit error rate.

I am desperately in need of you guys help to figure out the code.

Sorry this is not a code writing service. We will help you with code but not write it for you.

Thanks for the the way this is not an is my research work.......... pooja