beautypg.com

Wavetronix Click 500 (programmable controller) (CLK-500) - Developer Guide User Manual

Page 87

background image

86

CHAPTER 8 • SERIAL MESSAGE SUPPORT

2 An integer specifying the size of the data array.

3 A pointer to any received checksum value.

4 The index into the checksum array used by this checksum algorithm function.

The third parameter is set to “0” for a transmitted message and is set to a non-zero number

when a message is received with a checksum.

When the CSUM designator is encountered within a message, the referenced callback func-

tion is called by the system. The callback function will either generate the checksum value of

the message to be transmitted or will compute and verify the checksum bytes in a received

message.

The ClickMsgStoreChecksum function allows you to store the calculated value of a check-

sum used in a transmitting message. A pointer to the calculated checksum and an index

into the checksum array are its parameters.

This stored value is placed in the transmitted serial stream where it is specified by the mes-

sage definition. The maximum number of bytes allowed for a checksum value is eight.

The following code block presents an example custom checksum. This novel checksum adds

decimal value of all the ASCII characters (0 through 9). The resulting sum is then returned

as a one-byte checksum. This can be used with a simple protocol message like “SETTIME

07/21/2009 16:01:02.999|:\r.” In this message, the ASCII colon “:” before the \r is actually

the checksum obtained by adding up the individual digits of the timestamp. In other words,

the sum is the result of adding 0+7+2+1+2+0+0+9+1+6+0+1+2+9+9+9 = 58, which in the

ASCII code is equivalent to the colon character.

int DateChecksum(char *dataPtr, unsigned int dataSize,
char *expectedCRC, int myCSumIdx)
{
int i;
long sum;

sum = 0;
for (i=0; i {
if ((dataPtr[i] >= ‘0’) && (dataPtr[i] <= ‘9’))
{
sum += (dataPtr[i]-0x30);
}
}

if (expectedCRC != NULL)