JBUS protocol
16
(cont.)
/**********************************************************************************************/
/*
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
int length;
{
int i ;
int j ;
union {
int ival ;
unsigned char cval [ 2 ];
} crcal
crcal.ival = 0XFFFF;
i = 0;
length = length - 2;
while ( i < length )
{
j = (int) ( message [ i ] ^ crcal.cval [ CPH ] );
crcal.cval [ CPH ] = tbcrch [ j ] ^ crcal.cval [ CPL ];
crcal.cval [ CPL ] = tbcrcl [ j ];
i++;
};
return ( crcal.ival );
}
note: if the CRC is computed using table lookup, the most significant byte is
transmitted first.
SineWave™ JBUS communication system installation: E-51027160XT/AC
/* for which the crc16 is to be calculated
/* length of message to be checked
/* (including crc16)
/* beginning of the function
/* loop variable
/* calculation and displacement variable
/* calculated crc16:*/
/* - whole
/* - table of 2 characters
/* unsigned is important since otherwise the sign
/* extension causes negative displacements with
/* respect to the beginning of the table
/*$ initialization of calculated crc
/*$ correction of the length to be checked: remove the */
/*$ received crc16 from the length to be checked
/*$ WHILE there are bytes to be checked DO
/*$ calculate the table index
/*$ most significant byte
/*$ least significant byte
/*$ next byte
/*$ END WHILE there are bytes to be checked
/*$ return the calculated crc
/* end of function
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
MGE UPS SYSTEMS