JBUS protocol (continued)
*****************************************************************************************************/ | */ | |
/* | FUNCTION CALL: crc = crc16 (message, length); | */ |
/* | with char *message; message = address of message | */ |
/* |
| */ |
/* | int length; length of received message (including CRC) | */ |
/* | expressed in number of bytes | */ |
/* | int crc; = CRC16 calculated from the "address", "code" and | */ |
/* | "information" fields. | */ |
/* |
| */ |
/* | RETURN VALUE: calculated crc 16 (int crc) | */ |
/***************************************************************************************************** */
int crc16 (message, length) |
|
|
| |
unsigned char message [ ]; | /* | buffer containing message | */ | |
|
| /* | for which the crc16 is to be calculated. | */ |
int length; | /* | length of message to be checked | */ | |
|
| /* | (including crc16) | */ |
{ |
| /* | beginning of the function | */ |
int | i ; | /* | loop variable | */ |
int | j ; | /* | calculation and displacement variable | */ |
union { | /* | calculated crc16: | */ | |
int | ival ; | /* | - whole | */ |
unsigned char cval [ 2 ]; | /* | - table of 2 characters | */ | |
|
| /* | unsigned is important since otherwise the sign | */ |
|
| /* | extension causes negative displacements with | */ |
|
| /* | respect to the beginning of the table | */ |
| } crcal; |
|
|
|
|
| /*$ | initialization of calculated crc | */ |
crcal.ival = 0XFFFF; |
|
|
| |
i = 0; |
|
|
| |
|
| /*$ | correction of the length to be checked: remove the | */ |
|
| /*$ | received crc16 from the length to be checked | */ |
length = length - 2; |
|
|
| |
|
| /*$ WHILE there are bytes to be checked DO | */ | |
while ( i < length ) |
|
|
| |
| { |
|
|
|
|
| /*$ | calculate the table index | */ |
| j = (int) ( message [ i ] ^ crcal.cval [ CPH ] ); |
| ||
|
| /*$ | most significant byte | */ |
| crcal.cval [ CPH ] = tbcrch [ j ] ^ crcal.cval [ CPL ]; |
| ||
|
| /*$ | least significant byte | */ |
| crcal.cval [ CPL ] = tbcrcl [ j ]; |
| ||
|
| /*$ | next byte | */ |
| i++; |
|
|
|
| }; | /*$ | END WHILE there are bytes to be checked | */ |
|
| /*$ | return the calculated crc | */ |
return ( crcal.ival ); |
|
|
| |
} |
| /* | end of function | */ |
Note: if the CRC16 is computed using table lookup, the most significant byte is transmitted first.
6739389EN/FB - Page 17