Modbus的CRC校验实验

Posted cau_par

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Modbus的CRC校验实验相关的知识,希望对你有一定的参考价值。

Modbus的CRC校验实验

表述

   Name   : "CRC-16/MODBUS"
   Width  : 16
   Poly   : 8005
   Init   : FFFF
   RefIn  : True
   RefOut : True
   XorOut : 0000
   Check  : ?

先参考一些厂家给的MODBUS校验程序

const unsigned char auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
} ;

const unsigned char auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;

//crc low byte in high 8 bit
unsigned short Modbus_CRC16(unsigned char *Buff_addr,unsigned short len)
{
	unsigned char uchCRCHi = 0xFF;             // CRC高字节的初始化
	unsigned char uchCRCLo = 0xFF;             // CRC低字节的初始化
	unsigned short uIndex;                           // CRC查找表的指针
	while (len--)								
	{
		uIndex = uchCRCHi ^ *Buff_addr++;      // 计算CRC
		uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
		uchCRCLo = auchCRCLo[uIndex];
	}
	return(uchCRCHi <<8 | uchCRCLo);
}
//crc high byte in high 8 bit
unsigned short Modbus_CRC16_2(unsigned char *Buff_addr,unsigned short len)
{
	unsigned char uchCRCHi = 0xFF ; 
    unsigned char uchCRCLo = 0xFF ; 
    unsigned uIndex ; 
    while (len--) 
    {
        uIndex = uchCRCLo ^ *Buff_addr++ ; /* calculate the CRC*/
        uchCRCLo = uchCRCHi ^ auchCRCHi[uIndex] ;
        uchCRCHi = auchCRCLo[uIndex] ;
    }
    return (uchCRCHi << 8 | uchCRCLo) ;
}

由上以程序,可看到使用的方法是直驱表法,而这个生成的表却与正向poly直接计算生成的表是不一致的。
原因在于modbus使用的输入字节倒转的选项,因此生成的表是不一样的
那么这个表是怎么生成的呢?

poly        18005
            1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 //0x8005
reserved    1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 //0xa001 || 0x4003 两种表述方法

因为MODBUS中CRC选择了REFIN这个选项,那我们看这个表格是如何生成

unsigned short crcTable[256] = {0};
unsigned short poly = 0xa001;
void crcTableCreate(void)
{
    int i = 0;
    for(i = 0; i < 256; i++)
    {
        unsigned short crc = i;
        for(int j = 0; j < 8; j++)
        {
            if(crc & 0x0001)
            {
                crc = (crc >> 1) ^ poly;
            }
            else
            {
                crc = crc >> 1;
            }
        }
        crcTable[i] = crc;
    }
}

0000,c0c1,c181,0140,c301,03c0,0280,c241,c601,06c0,0780,c741,0500,c5c1,c481,0440,
cc01,0cc0,0d80,cd41,0f00,cfc1,ce81,0e40,0a00,cac1,cb81,0b40,c901,09c0,0880,c841,
d801,18c0,1980,d941,1b00,dbc1,da81,1a40,1e00,dec1,df81,1f40,dd01,1dc0,1c80,dc41,
1400,d4c1,d581,1540,d701,17c0,1680,d641,d201,12c0,1380,d341,1100,d1c1,d081,1040,
f001,30c0,3180,f141,3300,f3c1,f281,3240,3600,f6c1,f781,3740,f501,35c0,3480,f441,
3c00,fcc1,fd81,3d40,ff01,3fc0,3e80,fe41,fa01,3ac0,3b80,fb41,3900,f9c1,f881,3840,
2800,e8c1,e981,2940,eb01,2bc0,2a80,ea41,ee01,2ec0,2f80,ef41,2d00,edc1,ec81,2c40,
e401,24c0,2580,e541,2700,e7c1,e681,2640,2200,e2c1,e381,2340,e101,21c0,2080,e041,
a001,60c0,6180,a141,6300,a3c1,a281,6240,6600,a6c1,a781,6740,a501,65c0,6480,a441,
6c00,acc1,ad81,6d40,af01,6fc0,6e80,ae41,aa01,6ac0,6b80,ab41,6900,a9c1,a881,6840,
7800,b8c1,b981,7940,bb01,7bc0,7a80,ba41,be01,7ec0,7f80,bf41,7d00,bdc1,bc81,7c40,
b401,74c0,7580,b541,7700,b7c1,b681,7640,7200,b2c1,b381,7340,b101,71c0,7080,b041,
5000,90c1,9181,5140,9301,53c0,5280,9241,9601,56c0,5780,9741,5500,95c1,9481,5440,
9c01,5cc0,5d80,9d41,5f00,9fc1,9e81,5e40,5a00,9ac1,9b81,5b40,9901,59c0,5880,9841,
8801,48c0,4980,8941,4b00,8bc1,8a81,4a40,4e00,8ec1,8f81,4f40,8d01,4dc0,4c80,8c41,
4400,84c1,8581,4540,8701,47c0,4680,8641,8201,42c0,4380,8341,4100,81c1,8081,4040,

使用这种方法生成的表格与官方给的是相一致的,只是高低位是颠倒的,那这个又如何理解呢,那是因为采用低位先XOR的方式,那么字节填充的方式就是跟正向是不一致的,反向方式相当于数据是填充在数据串的左侧,于是生成CRC的后8位反而与后进来的数据高位进行XOR。
这种方式来进行直驱表法的公式与POLY是正向的也有所区别

unsigned short crcUpdate3(unsigned short crcIn, unsigned char data)
{
    unsigned short result = 0;
    //result = (crcIn << 8) ^ crcTable[(crcIn >> 8) ^ data]; // poly
    result = (crcIn >> 8) ^ crcTable[(crcIn & 0xff) ^ data]; // reversed poly
    return result;
}

unsigned short crcCheck3(unsigned char *pData, unsigned char size)
{
    unsigned short crcResult = 0xffff;//Initial Value
    for(int i = 0; i < size; i++)
    {
        crcResult = crcUpdate3(crcResult, *(pData+i));
    }
    return crcResult;
}

如果想要得到三种方法得到一样的结果那边直驱表法的寄存器初始值必须都是0才可以达到三者的输出结果一致,不然直驱表法得到与其他两种方法一样的结果,因为直驱表法本来就是直接计数方法的变种。
下面是针对MODBUS的三种结果一致的具体实现,寄存器初始值设置为0

unsigned short crcUpdate(unsigned short crcIn, unsigned char data)
{
    unsigned short result = 0;
    unsigned int poly = (0xa001 << 1) + 1;
    unsigned int tmp =  crcIn;
    for(int i = 0; i < 8; i++)
    {
        if(data & 0x01)
        {
            tmp += 0x10000;
        }
        data >>= 1;
        if(tmp & 0x0001)
        {
            tmp ^= poly;
        }
        tmp >>= 1;
    }
    result = tmp & 0xffff;
    return result;
}

unsigned short crcCheck(unsigned char *pData, unsigned char size)
{
    unsigned short crcResult = 0x0000;//Initial Value
    for(int i = 0; i < size; i++)
    {
        crcResult = crcUpdate(crcResult, *(pData+i));
    }
    for(int i = 0; i < 2; i++)
    {
        crcResult = crcUpdate(crcResult, 0x00);
    }
    return crcResult;
}

unsigned short crcUpdate2(unsigned short crcIn, unsigned char data)
{
    unsigned short result = 0;
    result = (crcIn >> 8 | data << 8) ^ crcTable[(crcIn & 0xff)];
    return result;
}

unsigned short crcCheck2(unsigned char *pData, unsigned char size)
{
    unsigned short crcResult = 0x0000;//Initial Value
    for(int i = 0; i < size; i++)
    {
        crcResult = crcUpdate2(crcResult, *(pData+i));
    }
    for(int i = 0; i < 2; i++)
    {
        crcResult = crcUpdate2(crcResult, 0x00);
    }
    return crcResult;
}

unsigned short crcUpdate3(unsigned short crcIn, unsigned char data)
{
    unsigned short result = 0;
    result = (crcIn >> 8) ^ crcTable[(crcIn & 0xff) ^ data];
    return result;
}

unsigned short crcCheck3(unsigned char *pData, unsigned char size)
{
    unsigned short crcResult = 0x0000;//Initial Value
    for(int i = 0; i < size; i++)
    {
        crcResult = crcUpdate3(crcResult, *(pData+i));
    }
    return crcResult;
}

将字节倒过来,先对小位进行计算,是因为一些硬件电路他是LSB先发送的,如果对小位开始计算可以直接对硬件进入的数据直接开始CRC计算,方便硬件电路的实现。





以上是关于Modbus的CRC校验实验的主要内容,如果未能解决你的问题,请参考以下文章

使用了modbus协议的程序,如何进行CRC校验

MODBUS通讯CRC校验码怎么算,有那位能告诉我呀

如何计算modbus-rtu的crc校验码

C51 modbus rtu crc下面这段代码校验结果总是不对,请高手指教,不胜感激!

Java Modbus CRC16校验

Modbus协议 CRC 校验码