Detailed explanation of Modbus TCP data packet structure

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

Detailed explanation of Modbus TCP data packet structure缩略图

The message formats of Modbus TCP and Modbus UDP are the same, and the difference between them is actually the difference between TCP and UDP. Therefore, the following analysis focuses on the protocol of Modbus TCP. The difference between Modbus TCP and Modbus Rtu (Modbus ASCII) is shown in the following figure:

Detailed explanation of Modbus TCP data packet structure插图

从上图可以看出,ModbusTCP在Modbus串行通信的基础上,去除了校验(由于TCP本身就带有校验和)和设备地址(ModbusTCP弱化了设备地址,用IP地址来取代),再加上MBAP报文头(占7 bytes),下面针对MBAP进行分析说明:

长度说明客户机服务器
事务处理标识符2字节Modbus请求/响应事务处理的标识客户机启动复制响应
协议标识符2字节0=Modbus协议客户机启动复制响应
长度2字节长度之后的字节总数客户机启动服务器启动
单元标识符1字节串行链路或其它总线的从站识别客户端启动复制响应

下面针对具体报文进行分析,Modbus协议在以太网链路上的报文格式如下所示:

事务处理标识符协议标识符长度单元标识符功能码数据
2 bytes2 bytes2 bytes1 byte1 byteN bytes

有了以上理论基础之后,下面针对各个功能码进行详细分析:

读取输出线圈

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图1

发送报文含义:读取服务器1号从站输出线圈,起始地址为0x13=19,对应地址为00020,线圈数量为0x1B=27,即读取1号从站输出线圈,地址从00020-00046,共27个线圈的状态值。

这里值得注意一下,协议中的起始地址指的是索引,后面的地址指的是具体地址,对于任意一个存储区,索引都是从0开始的,但是对应的具体地址,与存储区是相关的,比如输出线圈,0对应00001;输入线圈,0对应10001;输入寄存器,0对应30001;保持寄存器,0对应40001。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图2

返回报文含义:返回服务器1号从站输出线圈00020-00046,共27个线圈的状态值,返回字节数为4个,分别为CD 6B B2 05。

CD=1100 1101 对应 00020-00027

6B=0110 1011 对应 00028-00035

B2=1011 0010 对应 00036-00043

05=0000 0101 对应 00044-00046

读取输入线圈

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图3

发送报文含义:读取服务器1号从站输入线圈,起始地址为0xC4=196,对应地址为10197,线圈数量为0x1D=29,即读取1号从站输入线圈,地址从10197-10225,共29个线圈的状态值。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图4

返回报文含义:返回服务器1号从站输入线圈10197-10225,共29个线圈的状态值,返回字节数为4个,分别为CD 6B B2 05。

CD=1100 1101 对应 10197-10204

6B=0110 1011 对应 10205-10212

B2=1011 0010 对应 10213-10220

05=0000 0101 对应 10221-10225

读取保持寄存器

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图5

发送报文含义:读取服务器1号从站保持寄存器,起始地址为0x6B=107,对应地址为40108,寄存器数量为0x02=2,即读取1号从站保持寄存器,地址从40108-40109,共2个寄存器的数值。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图6

返回报文含义:返回服务器1号从站保持寄存器40108-40109,共2个寄存器的数值,返回字节数为4个,分别为02 2B 01 06,40108对应数值为0x022B,40109对应数值为0x0106。

读取输入寄存器

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图7

发送报文含义:读取服务器1号从站输入寄存器,起始地址为0x6B=107,对应地址为30108,寄存器数量为0x02=2,即读取1号从站保持寄存器,地址从30108-30109,共2个寄存器的数值。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图8

返回报文含义:返回服务器1号从站输入寄存器30108-30109,共2个寄存器的数值,返回字节数为4个,分别为02 2B 01 06,30108对应数值为0x022B,30109对应数值为0x0106。

预置单线圈

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图9

发送报文含义:预置服务器1号从站单个线圈的值,线圈地址为0x00AC=172,对应地址为00173,断通标志0xFF00表示置位,0x000表示复位,即置位1号从站输出线圈00173。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图10

返回报文含义:预置单输出线圈原报文返回。

预置单寄存器

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图11

发送报文含义:预置服务器1号从站单个保持寄存器的值,寄存器地址为0x0087=135,对应地址为40136,写入值为0x039E,即预置1号从站保持寄存器40136值为0x039E。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图12

返回报文含义:预置单保持寄存器原报文返回。

预置多线圈

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图13

发送报文含义:预置服务器1号从站多个线圈的值,线圈地址为0x0013=19,对应地址为00020,线圈数为0x0A=10,写入值为0xCD00,即预置1号从站线圈00020-00027=0xCD=1100 1101,00028-00029=0x00=0000 0000。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图14

返回报文含义:预置多输出线圈返回报文是在原报文基础上除去字节数及具体字节后返回。

预置多寄存器

发送报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图15

发送报文含义:预置服务器1号从站多个寄存器的值,寄存器地址为0x0087=135,起始地址为40136,寄存器数量为0x02=2,结束地址为40137,写入值为0xCD00和0x0A10,即预置1号从站寄存器40136=0x0105,40137=0x0A10。

返回报文格式如下:

Detailed explanation of Modbus TCP data packet structure插图16

返回报文含义:预置多保持寄存器返回报文是在原报文基础上除去字节数及具体字节后返回。

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.