Go Down

### Topic: Checksum for RF transmission (Read 1 time)previous topic - next topic

#### BKnight760

##### Jun 07, 2011, 08:28 pmLast Edit: Jun 07, 2011, 08:33 pm by BKnight760 Reason: 1
Hi guys.  I have two RF radios I am using to send data between Arduinos.

I am sending data using a structure of 3 bytes.

Byte1 = Packet Type
Byte2 = Data
Byte3 = Checksum

My checksum is simply the Packet Type and Data bytes added together and then negated.

When all of the expected bytes are received, this simple checksum can detect single-order errors in the data.  However, if one of the bytes is lost, sometimes this still passes the checksum, because it doesn't handle cyclic redundancy.  (It is possible that double or 2^n-order errors could occur and pass this checksum too, if they occured just right)

ie:

Packet 1:

0x00
0x00
0xFF

Packet 2:
0x00
0x00
0xFF

If I happen to lose the first byte of the Packet1, then it reads the packet as:
0x00  <--Packet1
0xFF  <--Packet1
0x00  <--Packet2

This passes the checksum test still.  Is there a simple checksum algorithm I could use that would be easy to implement on the Arduino that guarantees the paket is received exactly as it was sent?  Are there any Arduino libraries that implement checksums for arbitrary data?

#### PaulS

#1
##### Jun 07, 2011, 08:53 pm
Quote
Byte3 = Checksum

My checksum is simply the Packet Type and Data bytes added together and then negated.

A negative value in a byte? I don't think so. What data types are you actually using?

#### BKnight760

#2
##### Jun 07, 2011, 08:58 pmLast Edit: Jun 07, 2011, 09:00 pm by BKnight760 Reason: 1
Bitwise negation.  http://arduino.cc/en/Reference/BitwiseXorNot

~0x00 = 0xFF

You can think of a byte as signed or not.  It doesn't really matter, it's just your interpretation of the bits.  In this case, I treat it as unsigned because it's declared as a byte type.

#3
##### Jun 08, 2011, 10:43 pm
Quote
Is there a simple checksum algorithm I could use that would be easy to implement on the Arduino that guarantees the paket is received exactly as it was sent?

CRC is a much better choice than addition.

Quote
Are there any Arduino libraries that implement checksums for arbitrary data?

Yup...
http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html

This should get you started...

Code: [Select]
`#include <util/crc16.h>uint16_t MyChecksum( uint8_t* buffer, uint8_t count ){  uint16_t result;  result = 0;  while ( count > 0 )  {    result = _crc16_update ( result, *buffer );    ++buffer;    --count;  }  return( result );}`

#### robtillaart

#4
##### Jun 08, 2011, 11:42 pm
Quote
If I happen to lose the first byte of the Packet1,

You need to add 2 more bytes: a start and stop char so one packet becomes something like
< packettype data CRC  >

// yes it looks a bit like XML
// http://www.asciitable.com/   has char 2 and 3  defines as Start of Text and End of TExt can also be used.

after receiving the > character you know you must have 5 bytes to work with, if not one is lost
if don't you receive a > char but a < char you know there is something faulty too

And yes there is a chance that the CRC code contains a > stop char.

This is the way to get in sync with the sender. just read until you got "><" a stop followed by a start.

Hopes this helpes,
Rob

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up