Home > Back-end >  Questions about CRC16_MODBUS calibration
Questions about CRC16_MODBUS calibration

Time:03-12

1. Could you tell me why use CRC16_MODBUS check?
2. Or use CRC16_MODBUS check good?
3. What circumstances use CRC16_MODBUS check?
See some code about CRC16_MODBUS check but also is not very understand, help knowledge sharing


 
//CRC16_MODBUS: polynomial x16 + x15 + x 5 + 1 (0 x8005), the initial value of 0 XFFFF, low before, and in the high results with 0 x0000 exclusive or


Void InvertUint8 (unsigned char * dBuf, unsigned char * srcBuf)
{
int i;
Unsigned char TMP [4].
TMP [0]=0;
for(i=0; i<8; I++)
{
If (srcBuf [0] & amp; (1 & lt; TMP [0] |=1 & lt; <(7 - I);
}
DBuf [0]=TMP [0];

}
Void InvertUint16 (unsigned short * dBuf, unsigned short * srcBuf)
{
int i;
Unsigned short TMP [4].
TMP [0]=0;
for(i=0; i<16. I++)
{
If (srcBuf [0] & amp; (1 & lt; TMP [0] |=1 & lt; <(15 - I);
}
DBuf [0]=TMP [0];
}

Unsigned short CRC16_MODBUS (unsigned char * puchMsg, unsigned int usDataLen)
{
Unsigned short wCRCin=0 XFFFF;
Unsigned short wCPoly=0 x8005;
Unsigned char wChar=0;

While (usDataLen -)
{
WChar=* (puchMsg++);
InvertUint8 (& amp; WChar, & amp; WChar);
WCRCin ^=(wChar & lt; <8);
for(int i=0; I & lt; 8; I++)
{
If (wCRCin & amp; 0 x8000)
WCRCin=(wCRCin & lt; <1) ^ wCPoly;
The else
WCRCin=wCRCin & lt; <1;
}
}
InvertUint16 (& amp; WCRCin, & amp; WCRCin);
Return (wCRCin);
}






CodePudding user response:

Calibration is to discover data transmission error, why use CRC16, because of simple, rapid and easy hardware implementation, Ethernet, PCIe underlying all use CRC16 is used to test the correctness of the data transmission
  • Related