SonTek/YSI
96
ADVField Operation Manual (September 1, 2001)
A1.2.2 Checksum Calculation
The ADV uses a checksum when sending binary data over a serial port. The checksum is com-
puted with the C function
ComputeCheckSum
shown below. To understand how the checksum is
computed, assume that a sample has been placed in a segment of memory (i.e.; a character array)
in the format shown in §A1.2. The checksum is computed by the following statement in C.
CheckSum = ComputeCheckSum( buf, Sample_Length-2);
In this example,
buf
is a pointer to the memory location where the profile begins (i.e.; the name
of the character array where the profile was placed).
ComputeCheckSum
simply adds the first
Sample_Length-2
bytes starting at location
buf
, and then adds the value 0xA596. The result of
this summation is then truncated to a two-byte integer and returned. The addition of the value
0xA596 is to ensure that an all-zeros profile does not produce a valid checksum.
#define CHECK_SUM_OFFSET 0xA596
unsigned int ComputeCheckSum( unsigned char *buf, int n )
{
int i;
unsigned int ChkSum = CHECK_SUM_OFFSET;
for(i=0;i<n;i++) ChkSum += buf[i];
return(ChkSum);
}