30
MAS7.1 Suplemental Guide, Rev 1.1, 9/28/07
K.I.S.S.™
Keep It Simple Serial
Source Code Example of Calculating a Checksum
The following is a simple “C” program that calculates the checksum of the string “TestString” and then prints the
initial string with the calculated checksum appended to it.
You can download this source code at:
http://www.zektor.com/downloads/ckstst.c
#include "stdio.h"
int main( void)
{
char TestString[] = "LI 3,2,80";
unsigned char cksum;
int index;
char token = ';';
cksum = 0; // initialize checksum
// Checksum all of 'TestString[]'
index = 0;
while (TestString[index] != '\0')
cksum += TestString[index++];
// Add the checksum token character ';' to checksum
cksum += token;
// Print the results
printf( "%s%c%u", TestString, token, (unsigned char)cksum);
return (0);
}