Checksum

<< Click to Display Table of Contents >>

Navigation:  Devices > UDP data exchange > Protocol >

Checksum

Previous pageReturn to chapter overviewNext page

If the message is shorter than 100 byte, the sum s0 will be calculated from the whole package's single bytes. If the message contains 100 bytes or more, s0 will be calculated from the message's first and the last 50 bytes.

 

In both cases the checksum byte must be initialized with 0. The checksum is calculated to

 

checksum = 0xff - s0

 

unsigned char checksum( const unsigned char* payload, unsigned int payloadLength ) const

{

 unsigned char s0 = 0;

 

 if( payloadLength < 100 )

 {

         for( int i = 0; i < payloadLength; ++i )

         {

                 s0 += payload[i];

         }

 }

 else

 {

         for( int i = 0; i < 50; ++i )

         {

                 s0 += payload[i];

         }

         for( int i = payloadLength-1; i >= payloadLength - 50; --i )

         {

                 s0 += payload[i];

         }

 }

 

 return ( 0xFF - s0 );

}

 

To check if the package has been transmitted correctly, the whole message's single bytes will be accumulated to the byte sum s1 if the message is shorter than 100 byte. If it contains 100 bytes or more, s1 is calculated from the message's first and last 50 bytes.

 

The package is correct if

 

s1 = 0xFF