《Modbus协议中CRC校验和LRC校验.docx》由会员分享,可在线阅读,更多相关《Modbus协议中CRC校验和LRC校验.docx(12页珍藏版)》请在第一文库网上搜索。
1、CRC的生成循环冗余校验(CRC)域为两个字节,涉及一种二进制16位值。附加在报文背面的CRC时值由发送设备计算。接受设备在接受报文时重新计算CRC时值,并将计算成果于实际接受到时CRC值相比较。如果两个值不相等,则为错误。CRC的计算,开始对一种16位寄存器预装全1然后将报文中时持续的8位子节对其进行后续时计算。只有字符中的8个数据位参与生成CRC的运算,起始位,停止位和校验位不参与CRC计算。CRC的生成过程中,每个8-位字符与寄存器中的值异或。然后成果向最低有效位(1SB)方向移动(Shift)1位,而最高有效位(MSB)位置充零。然后提取并检查1SB:如果1SB为1,则寄存器中时值与一
2、种固定的预置值异或;如果1SB为0,则不进行异或操作。这个过程将反复直到执行完8次移位。完毕最后一次(第8次)移位及有关操作后,下一种8位字节与寄存器的目前值异或,然后又同上面描述过的同样反复8次。当所有报文中子节都运算之后得到的寄存器中的最后值,就是CRC.生成CRC的过程为:1 .将一种16位寄存器装入十六进制FFFF(全1).将之称作CRC寄存器.2 .将报文的第一种8位字节与16位CRC寄存器的低字节异或,成果置于CRC寄存器.3 .将CRC寄存器右移1位(向1SB方向),MSB充零.提取并检洌J1SB.4 .(如果1SB为0):反复环节3(另一次移位).(如果1SB为1):对CRC寄
3、存器异或多项式值OXAoO1(1010000000000001).5 .反复环节3和4,直到完毕8次移位。当做完此操作后,将完毕对8位字节的完整操作。6 .对报文中的下一种字节反复环节2到5,继续此操作直至所有报文被解决完毕。7 .CRC寄存器中的)最后内容为CRC值.8 .当放置CRC值于报文时,如下面描述的那样,高下字节必须互换。MODBUS合同的CRC校验子程序代码为以便读者使用MODBUS合同,将VC、VB、ASM51环境下MoDBUS合同的CRC校验子程序代码一并给出,供读者参照。/*CRCCa1cuIationforMODBUSProtoCo1forVC*/数组Snd为地址等传播字
4、节,num为字节数,发为6收为5usigedintmb_crc(BYTE*snd,intnum)(inti,j;usignedintczcrc=0xFFFFfor(i=0;inum;i)c=Sndi&OxOOFF;crc=c;for(j=0j=1;crc=0xAOO1;)eIsecrc=1)retrn(crc);)usignedshortintCrcCheck(constunsignedchr*buffer,constintbuff1e)(usignedshortintcrcVa1ue=0;if(!bufferIbff1en0)returcrcVaIue;intCRCHi=0x0,OxC10x
5、810x40zOx1,OxC0z0x8O,0x41zOxbOxCO,0x80,0x41,0x0,OxCb0x81z0x40,Ox1OxCOzOx8Oz0x41,OXO,OxC1z0x81,0x40zOXO,OxC10x81,0x40zOx1,OxCOzOx80z0x41z0x1,OxCOz0x8O,Ox41,OxO,OXCI,0x81,Ox40zOxzOxC1,Ox81,Ox40,OxbOxCOz0x80,Ox41zOXO,OxC1z0x81,0x40,Ox1OxCOzOx80,Ox41zOx1zOxCOz0x8OzOx41,OxOzOXC1,0x810x40zOx1,OxCO,0x80,0x
6、41zOxOzOxC1zOx81,Ox40zOxO,OxC1,Ox81,O40zOxbOxCOz0x80,Ox41,OxOzOxC10x81,Ox40,Ox1,OxCO,0x80,0x41,0x1,OXCo,0x8O,Ox41OxOzOxC1z0x81Ox40zOxOzOxCb0x81,0x40,Ox1zOxCOzOx8OzOx41,OxbOxCO,0x80,0x41,0x0,OxCbOx81,Ox4OzOx1zOxCO,0x80z0x41z0x0,OxCb0x81,0x40,Ox0zOxC1z0x81z0x40,Ox1OxCOz0x80z0x41,Ox1zOXC0,0x8Oz0x41zOxO
7、zOxC1,Ox81,0x40,0x0,OxCbOx81,Ox40,Ox1,OxCO,Ox80z0x41z0x0,OxC1,0x8bOx4OzOx1zOxCOz0x80zOx41zOx1,OxCOz0x8O,Ox41zOxOzOxC1,0x81,0x40,OXO,OxC1zOx81,0x40x41,0x0,OxCb0x810x40,O,OxbOxCOzOx80,0x41,0x1,OxCO,0x80,0x41,OxOzOxC1,0x81,0x4O,Ox1zOxCOz0x8Oz0x41,0x0,OxC1,Ox81z0x40z0x0,OxC1Ox81z0x40,Ox1,OxCOz0x80zOx41,
8、Ox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7B,0x7A,OxBAzOxBEzOx7E,Ox7FzOxBFzOX7DzOxBDzOXBCzOx7Cz0xB4zOx74zOx75zOxB5z0x77zOxB7,0xB6z0x76z0x72,OxB2zOxB3,Ox73zOxB1,Ox710x70zOxBOzOx50,
12、Ox90,0x91Ox51,0x93z0x53,0x52,0x92z0x96zOx56z0x57z0x97zOx55zOx95zOx94z0x54,Ox9C,0x5C,0x5D,0x9D,Ox5Fz0x9Fz0x9EzOx5Ez0x5A,0x9AzOx9Bz0x5Bz0x99zOx59z0x58,0x98z0x88,Ox48,Ox49,Ox89z0x4Bz0x8Bz0x8AzOx4A,OX4E,0x8EzOx8F,0x4F,Ox8D,0x4D,0x4C,0x8C,0x44,Ox84zOx85,0x45,Ox87zOx47,Ox46z0x86,0x82,0x42z0x43,0x83z0x41,0
13、x81z0x80z0x4O);itizm;intdCRCHi=OxFF;itdCRC1o=OxFF;for(i=O;ibuff1e;i+)(m=dCRC1obufferi;dCRC1o=dCRCHiCRCHim;dCRCHi=CRC1om;)creVaIue=dCRC1o+(dCRCHi8);returncreVa1ue;)|/*CRCCaIcu1ationforMODBUSProtoco1forVB*/Functiomb_crc(ByRefsd()asBYTEznmasinteger)as1ongcrc_I=crc_h=&HFFfori=1tonumcrc_1=crc_1XORsnd(i)f
14、orj=1to8ifcrc_IAND1thencrc1=(crcj-1)2ifcrc_hnd1thencrc_1=crcJ128crc_h=(crc_h-1)/2endifcrc_I=Crc_1XOR&HAOcrc_h=CrJhXOR&H01eIse:crc=crc/2ifcrc_hand1thencrcU=Crc128crc_h=(crc_h-1)/2e1se:crc_h=crc_h/2endifedifnextjnextimb_crc=crc_1crc_h*256EndFunction;CRCCaIcuIationforMODBUSProtocoIforASM51;R1为发送(接受)字节的
15、缓存首地址;R2为发送(接受)字节的字节数(不含CRC字节),;R3为CRC校验低位字节,;R4为CRC校验高位字节,CRC:MOVAzOFFHMOVR4,AMOVR3,ACRCkMOVA,R1XR1AzR3MOVR3,AMOVR2,#08HCRC8:C1RCMOVA,R4RRCAMOVR4zAMOVAzR3RRCAMOVR3,AjnccrcioMOVA,R3XR1Az#01HMOVR3zAMOVAzR4XR1Az#0A0HMOVR4zACRC1O:DJNZR2,CRC8INCR1DJNZCRC1RET1RC的生成纵向冗余校验(1RC)为一种字节,具有8位二进制值。1RC由发送设备计算,并附加1RC到报文。接受设备在接受文时计算1RC并将计算的成果与在1RC接受到的实际值相比较,如果两个值不