Modbus-RTU和Modbus-TCP协议模板(C语言)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第1页Modbus-RTU和Modbus-TCP协议模板(C语言)目录简介:...........................................................................................................................................................2Modbus-RTU简介:........................................................................................................................................2Modbus-TCP简介:........................................................................................................................................3System.h:....................................................................................................................................................4CRC_Check.h..................................................................................................................................................5CRC_Check.c..................................................................................................................................................5Modbus_RTU.h................................................................................................................................................8Modbus_RTU.c..............................................................................................................................................10Modbus_TCP.h..............................................................................................................................................27Modbus_TCP.c..............................................................................................................................................27第2页简介:在单片机内部实现modbus协议,可以简单地将变量的值映射到modbus寄存器地址,这种方法是高效的,但是有以下缺点:1、通常不支持一条指令操作多个变量(寄存器);2、可移植性较差。本模板使用8位数组模拟modbus寄存器。06,10指令均对数组进行写操作,同时记录寄存器的写入操作,在其他地方定时判断“写入标志”,将“写入标志”置位的“模拟寄存器”的值转移到相关变量,或进行其他操作。03指令读取最新数据,要求定时将变量的值写入“模拟寄存器”。本模板缺点是占用资源较多、效率较低,优点是完整支持了modbus的03,06,10指令。Modbus-RTU简介:1、Modbus读寄存器指令(0x03)地址(1B)+功能码(1B)+起始地址(2B)+寄存器数量(2B)+CRC(2B)返回:地址(1B)+功能码(1B)+字节数(1B)+数据值(2*“寄存器数量”Bytes或者“字节数”)+CRC(2B)2、Modbus写单个寄存器指令(0x06)地址(1B)+功能码(1B)+寄存器地址(2B)+数据值(2B)+CRC(2B)返回:地址(1B)+功能码(1B)+寄存器地址(2B)+数据值(2B)+CRC(2B)(返回与发送的指令相同)3、Modbus写多个寄存器指令(0x10)地址(1B)+功能码(1B)+起始地址(2B)+写寄存器数量(2Bn)+字节计数(1Bn*2)+数据(n*2B)+CRC(2B)返回:地址(1B)+功能码(1B)+起始地址(2B)+写寄存器数量(2B)+CRC(2B)第3页Modbus-TCP简介:在MODBUS-RTU前添加6个字节,并删除MODBUS-RTU的CRC校验。示例指令:0051000000090110000100010200006个字节部分称为“MBAP报文头”示例:0051000000090051是客户端发出的校验信息,服务端原内容返回即可0000表示modbus-tcp协议0009表示后面还有9个字节后面部分:01100001000102000001设备地址10表示10指令0001起始地址0001写寄存器数量02字节计数0000数据响应指令:005100000006011000010001第4页System.h:#ifndef_SYSTEM_H_#define_SYSTEM_H_#includestm32f10x.h//=======================不常改动项==========================================/******stm32f10x.h第505、506、507行已经定义了u32,u16和u8******///typedefunsignedcharu8;//typedefunsignedshortintu16;//typedefunsignedintu32;//typedefunsignedcharINT8U;//typedefunsignedshortintINT16U;//typedefunsignedintINT32U;//typedefsignedcharint8_t;//typedefsignedshortintint16_t;//typedefsignedintint32_t;//typedefsignedlonglongintint64_ttypedefunsignedlonglongintu64;typedefunsignedcharbool_t;#defineFALSE0#defineTRUE!FALSE#defineNULL0#endif第5页CRC_Check.h#ifndef_CRC_CHECK_H_#define_CRC_CHECK_H_#includesystem.hu16CRC16_Verify(u8*puchMsg,u16usDataLen);#endifCRC_Check.c#includeCRC_Check.h/**高位表*/staticconstu8auchCRCHi[]={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,第6页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};/**低位表*/staticconstu8auchCRCLo[]={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

1 / 34
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功