PDA

View Full Version : Checksum / CRC8 / CRC16



treitmey
- 17th November 2003, 15:42
Has anyone written PICbasic Pro code for checksum?

I would love reuse this code in a project I'm working on.

or

Does anyone know how to do this?

Thanks

picnaut
- 2nd December 2003, 07:22
Hello Treitmey,

One way to perform crude (but effective) error checking is to XOR every character you send out the serial port with the character that was sent before it and store each calculation in a checkbyte (this is not technically a checksum).

Immediately after you send out your last character you send out the checkbyte.

At the receiving end the process is repeated. As each character comes in it is XOR'd with the character that came before it. Each calculation is stored in another checkbyte variable.

The last character that is received must be the transmitted checkbyte.

If the transmitted checkbyte agrees with the calculated checkbyte at the reciever then the message must be clean. If they don't agree then something must have went wrong during transmission.

If something went wrong then the receiver must tell the transmitter that it has to resend it's data.

It's not a bad idea to frame your packets with start and end characters. Make sure that there is no way that these characters could appear in the actual data.

An example of this would be to send the data with a "(" at the start and a ")" at the end.

Also, make your packets as short as is practical. Using long packets in a noisy environment makes for much slower communication, even at higher baud rates. This is because data corruption isn't detected until the packet has been completely sent. If it's corrupt then all that time has been wasted.

Hope this helps you out.

picnaut

treitmey
- 2nd December 2003, 15:13
Thanks, I didn't know it was that easy,... When I looked checksum up on the net they went into the complex type (Which I am familure with) such as crc16 crc32

But this sounds great

Thanks

picnaut
- 2nd December 2003, 15:40
Hi Treitmey,

Glad I could help.

The XOR method works great for a master-slave system (don't say that in L.A. though).

However, if you're working with micros in various locations that send data randomly without a master to control them, you will need to use a more complex scheme.

Also, with the method I showed you, there is the small chance that you could miss an error. If electrical noise knocked a bit high on one character and then somewhere down the line knocked that same bit high on a different character, it could slip past this method. Fortunately, the chances of a coincidence like that are remote.

I'm currently working on a slight variation of the XOR method that can be used with the XOR method. It will not allow what I described above to occurr.

I'll keep you posted.

It would be nice to get some input from the list on the XOR method. I know that losts of people use it. I think it's also called a "rolling checksum". I may be wrong though.

picnaut