资源描述:
《modbusrtu协议中的crc算法专业文章》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第:4页,共:4页一、本文重点介绍ModBusRTU协议及协议中的CRC16算法。ModBusRTU协议在仪器仪表、数据采集中(如深圳科立恒公司KM数控仪表、KT2隔离数据采集器)广范应用。二、ModBus协议说明1>ModBusRTU发送协议(读取:03功能码):地址03数据地址(H)数据地址(L)数据长度(H)数据长度(L)CRC(H)CRC(L)例:010300000001840A接收数据格式为:地址03字长数据1(H)数据1(L)...CRC(H)CRC(L)例:0103020000B8442>ModBusRTU发送协议
2、(写入:06功能码)地址06数据地址(H)数据地址(L)新数据(H)新数据(L)CRC(H)CRC(L)例:010600000002080B回复数据格式为(也就把收到的数据不变返回):地址06数据地址(H)数据地址(L)新数据(H)新数据(L)CRC(H)CRC(L)例:010600000002080B第:4页,共:4页三、CRC的生成方法://--------------------------------------------------------------// CRC16计算方法1:使用2个256长度的校验表//
3、--------------------------------------------------------------constBYTEchCRCHTalbe[]= //CRC高位字节值表{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,0x4
4、0,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,
5、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,0x
6、81,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
7、,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,第:4页,共
8、:4页0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40};constBYTEchCRCLTalbe[]=