STATUS SCIENTIFIC CONTROLS
Installation, Commissioning & Routine Gas Testing
FGD10A-M Gas Detector
TD18/032
Issue:
10
Change Note:
1869
Page 76
10.3.2. Example 2: Method for recalculating CRC checksum:
The following code is used by the FGD10AM to check the incoming message
CRC. The incoming message was stored in an array called
aucModbusRxBuffer[RXBUFFERSIZE]
The length of the incoming message ucMsgLength is the number of bytes
received not including the 2 byte CRC.
unsigned integer RecalcModbusChecksumRx(unsigned integer
ucMsgLength)
{
unsigned integer uiRecalcChksum;
//checksum to be recalculated
unsigned integer uiMBTemp;
unsigned char ucLoop1;
unsigned char ucLoop2;
uiRecalcChksum = 0xFFFF;
//Load CRC reg with FFFF
for (ucLoop1 = 0; ucLoop1 <= ucMsgLength; +)
{
uiRecalcChksum = uiRecalcChksum^
aucModbusRxBuffer[ucLoop1];
// XOR 1st byte of msg with LSbyte of CRCreg and put result in
CRCreg
for (ucLoop2 = 0; ucLoop2 <= 7; +)
{
uiMBTemp = (uiRecalcChksum & 0x0001);
//mask off LSB
if (uiMBTemp == 0)
// test LSB
{
uiRecalcChksum = uiRecalcChksum >> 1;
// if LSB was 0 do a
shift
} else
{
uiRecalcChksum = uiRecalcChksum >> 1;
//shift CRCreg on
bit right
uiRecalcChksum = uiRecalcChksum^0xA001;
// if LSB was 1, XOR CRCreg with 0xA001
}
}/
/repeat for 8 bits
}
//repeat for all ucMsgLength bytes
return uiRecalcChksum;
//
the CRC has been regenerated. Now compare this with the received
CRC.
}