MODBUS 通信协议概述

Modbus 通信协议 概述

图片以及英文描述摘自: MODBUS APPLICATION PROTOCOL SPECIFICATION V1.1b3 .pdf 侵删

在这里插入图片描述

用户数据单元(Application data unit)ADU

The MODBUS protocol defines a simple protocol data unit (PDU) independent of the underlying communication layers. The mapping of MODBUS protocol on specific buses or network can introduce some additional fields on the application data unit (ADU).

Modbus 协议定义一个独立于底层通信层简易的协议数据单元(PDU),MODBUS协议在特定总线或者网络上的映射,可以在应用数据单元(ADU)上引入一些附加字段。

地址域:用于主从通信认证,范围1-247 。0 地址一般作为广播地址。248~255:是留做用户自己拓展的。
功能码:

The function code field of a MODBUS data unit is coded in one byte. Valid codes are in the range of 1 … 255 decimal (the range 128 – 255 is reserved and used for exception responses). When a message is sent from a Client to a Server device the function code field tells the server what kind of action to perform. Function code “0” is not valid.

modbus 数据单元的功能码域是1字节编码的。有效的功能码的范围是十进制的 1-255(128 - 255范围被保留和用作异常响应)。当一个信息从客户端发送到服务端设备,功能码告诉服务端哪种行为应该被执行。功能码 "0"是无效的。

数据域:

The data field of messages sent from a client to server devices contains additional information that the server uses to take the action defined by the function code. This can include items like discrete and register addresses, the quantity of items to be handled, and the count of actual data bytes in the field. The data field may be nonexistent (of zero length) in certain kinds of requests, in this case the server does not require any additional information. The function code alone specifies the action.

从客户端发送到服务器设备的消息的数据字段包含服务器用来执行函数代码定义的操作的附加信息。这可以包括离散地址和寄存器地址、要处理的项的数量和字段中的实际数据字节数。在某些类型的请求中,数据字段可能不存在(长度为零),在这种情况下,服务器不需要任何额外的信息。功能码单独指定操作。

差错校验
CRC算法名称CRC-16/MODBUS
多项式公式x16 + x15 + x2 + 1
宽度(width)16
多项式(poly)0x8005
初始值(init)0xFFFF
输出结果异或值(xorout)0x0000
异常/错误 处理

If no error occurs related to the MODBUS function requested in a properly received MODBUS ADU the data field of a response from a server to a client contains the data requested. If an error related to the MODBUS function requested occurs, the field contains an exception code that the server application can use to determine the next action to be taken.

If an error related to the MODBUS function requested occurs, the field contains an exception code that the server application can use to determine the next action to be taken.

For example a client can read the ON / OFF states of a group of discrete outputs or inputs or it can read/write the data contents of a group of registers.

When the server responds to the client, it uses the function code field to indicate either a normal (error-free) response or that some kind of error occurred (called an exception response). For a normal response, the server simply echoes to the request the original function code.

For an exception response, the server returns a code that is equivalent to the original function code from the request PDU with its most significant bit set to logic 1.

如果在正常接收的MODBUS ADU中没有发生与MODBUS功能请求相关的错误,则服务器向客户端响应的数据字段中应包含所请求的数据。

如果发生一个关于MODBUS功能请求错误,这个字段应该包换一个异常码,服务器能够使用这个异常码去确定下一步应该执行的动作。

例如一个客户端能够读取一组离散输出或者输入的 ON/OFF 状态或者能够读写一组寄存器的内容。

当服务端去响应客户端时,服务端使用这个功能码字段去描述一个正常(没有错误)响应或者有错误发生(被称为异常响应),对于一个正常响应,服务端简单的回应请求时原始的功能码。

在这里插入图片描述

对于异常响应,服务器返回一个功能码,该功能码相当于请求的PDU中的原始功能码的最高有效位设置为逻辑 1。(eg: 0x03 -> 0x83, 0b0000 0011 -> 0b1000 0011)

在这里插入图片描述

各个部分数据字节长度以及格式

The size of the MODBUS PDU is limited by the size constraint inherited from the first MODBUS implementation on Serial Line network (max. RS485 ADU = 256 bytes).

Therefore:
MODBUS PDU for serial line communication = 256 - Server address (1 byte) - CRC (2 bytes) = 253 bytes.

Consequently:
RS232 / RS485 ADU = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 bytes.
TCP MODBUS ADU = 253 bytes + MBAP (7 bytes) = 260 bytes.
The MODBUS protocol defines three PDUs. They are :

  • MODBUS Request PDU, mb_req_pdu
  • MODBUS Response PDU, mb_rsp_pdu
  • MODBUS Exception Response PDU, mb_excep_rsp_pdu

The mb_req_pdu is defined as:
mb_req_pdu = {function_code, request_data}, where
function_code = [1 byte] MODBUS function code,
request_data = [n bytes] This field is function code dependent and usually contains information such as variable references, variable counts, data offsets, sub-function codes etc.

The mb_rsp_pdu is defined as:
mb_rsp_pdu = {function_code, response_data}, where
function_code = [1 byte] MODBUS function code
response_data = [n bytes] This field is function code dependent and usually contains information such as variable references, variable counts, data offsets, sub-function codes, etc.

The mb_excep_rsp_pdu is defined as:
mb_excep_rsp_pdu = {exception-function_code, request_data}, where
exception-function_code = [1 byte] MODBUS function code + 0x80
exception_code = [1 byte] MODBUS Exception Code Defined in table

Modbus PDU数据的字节限制,是继承自第一个在串行线网络上实现的Modbus(RS485 上 的最大的 ADU数据长度 =256 字节)。

因此:Modbus PDU 对于串行线通信 最大字节长度 = 256 - 服务器地址(1字节) - CRC(2字节) = 253 字节。

因此:

​ RS232 / RS485 ADU(长度) = 253 bytes + Server address (1 byte) + CRC (2 bytes) = 256 bytes.

​ TCP MODBUS ADU (长度) = 253 bytes + MBAP(没有了解过,大家可以百度查一查) (7 bytes) = 260 bytes.

Modbus 定义了三种PDU:

  • Modbus 请求的 PDU : mb_req_pdu
  • Modbus 响应 PDU : mb_rsp_pdu
  • Modbus 异常响应 PDU mb_excep_rsp_pdu

mb_req_pdu 定义:

​ mb_req_pdu ={功能码, 请求数据},

​ 这里的功能码 = [1字节]Modbus 功能码,

​ 请求数据 =[n 字节] 这个字段取决于功能码,通常包含信息,例如,变量引用(地址?寄存器地址 大概可以这样理解?),变量的数量(大概可以理解为寄存器的数量),数据的偏移,子功能码等。

mb_rsp_pdu 定义:

​ mb_rsp_pdu = {功能码, 响应数据},

​ 这里的功能码 = [1字节]Modbus 功能码,

​ 响应数据 = [n字节]这个字段取决于功能码,通常包含信息,例如,变量引用(地址?寄存器地址 大概可以这样理解?),变量的数量(大概可以理解为寄存器的数量),数据的偏移,子功能码等。

mb_excep_rsp_pdu 定义:

​ mb_excep_rsp_pdu = {异常 功能码, 响应数据},

​ 这里的功能码 = [1字节] Modbus 功能 + 0x80

​ 响应数据 = [1字节]Mdobus 异常码定义的表中。(哪个表过于庞大,比较懒…,想了解的可以自己去翻一下 英文文档的 第7章)

数据编码

MODBUS uses a ‘big-Endian’ representation for addresses and data items. This means that when a numerical quantity larger than a single byte is transmitted, the most significant byte is sent first. So for example :

Register size value 16 - bits 0x1234 the first byte sent is 0x12 then 0x34.

Modbus 使用 大段模式表示地址和数据项,这意味着当一个数值大于一个字节时候,首先发高8位字节,再发低8位字节,例如寄存器大小值是16位的0x1234,发送的第一个字节是0x12,然后是0x34。

Modbus 数据模型

(这部分 只能大概明白什么意思,英对应的中译可能 不是很到位 希望指正)

MODBUS bases its data model on a series of tables that have distinguishing characteristics. The four primary tables are:

…这句话我翻译不好,大概意思应该是有 数据有这四种类型 如下表:

Primary Table(区块)Object type(访问长度)Type of(访问权限)Comments(说明)
Discretes Input(离散输入)Single bit(一位)Read-Only(只读)This type of data can be provided by an I/O system.
这个类型 可通过IO 系统提供
Coils|(线圈)Single bitRead-Write(读写)This type of data can be alterable by an application program.
这个类型的数据通过用户编程能够变化的
Input Registers(输入寄存器)16-bit word(16 位 一字)Read-Only(只读)This type of data can be provided by an I/O system
可通过IO 系统提供
Holding Registers (保持寄存器)16-bit wordRead-Write(读写)This type of data can be alterable by an application
program.
这个类型的数据通过用户编程能够变化的

The distinctions between inputs and outputs, and between bit-addressable and word-addressable data items, do not imply any application behavior. It is perfectly acceptable, and very common, to regard all four tables as overlaying one another, if this is the most natural interpretation on the target machine in question.

For each of the primary tables, the protocol allows individual selection of 65536 data items, and the operations of read or write of those items are designed to span multiple consecutive data items up to a data size limit which is dependent on the transaction function code. It’s obvious that all the data handled via MODBUS (bits, registers) must be located in device application memory. But physical address in memory should not be confused with data reference. The only requirement is to link data reference with physical address.
MODBUS logical reference numbers, which are used in MODBUS functions, are unsigned integer indices starting at zero.

数据项的区分/特征是:输入和输出,位寻址和字寻址,不隐含任何的用户程序的行为,是完全可以接受的,且很常见。去解决目标机器的所需要的应用,选择这四种方式相互叠加。(直接以自己理解的 写了出来翻译不清楚,没写清楚,可以直接看一下英文理解下。)

对于每个类型(primary tables),协议允许独立选择0-65536个数据项,读写操作这些字段被设计为可以连续的访问,直到功能码所对应的极限字节数。

很明显,所有通过MODBUS的数据处理,必须实在设备本身的内存中,但是在内存中的物理地址和数据引用(Modbus 数据地址,可以理解为Modbus寄存器地址)不能混淆。唯一的要求是将数据引用与物理地址链接。

MODBUS 裸机引用数量,使用在MODBUS 功能中,是一个从0开始的无符号整形。

MODBUS 实现的示例(Implementation examples of MODBUS model)

The examples below show two ways of organizing the data in device. There are different organizations possible, but not all are described in this document. Each device can have itsown organization of the data according to its application.

下面展示了两种方式去组织服务端(从机)设备数据。可能有不同的组织,但本文档中并未介绍所有组织。每个设备都可以根据其应用程序自行组织数据。

示例1:设备有4个分离的块 (Example 1 : Device having 4 separate blocks)

The example below shows data organization in a device having digital and analog, inputs and outputs. Each block is separate because data from different blocks have no correlation. Each block is thus accessible with different MODBUS functions.

下图是一个有数字量和模拟量,输入和输出的设备数组组织方式,各个块是相互分离的,因为每个块是没有关系的,因此,每个模块都可以通过不同的MODBUS功能进行访问。

在这里插入图片描述

示例2:设备只有一个内存块 (Example 2: Device having only 1 block)

In this example, the device has only 1 data block. The same data can be reached via several MODBUS functions, either via a 16 bit access or via an access bit.

在这个例子中这个设备只有一个数据块,可以通过多个MODBUS功能访问相同的数据, 也能通过16位访问或者1位的去访问。

在这里插入图片描述

MODBUS 寻址模型(MODBUS Addressing model)

The MODBUS application protocol defines precisely PDU addressing rules.

In a MODBUS PDU each data is addressed from 0 to 65535.

It also defines clearly a MODBUS data model composed of 4 blocks that comprises several elements numbered from 1 to n.

In the MODBUS data Model each element within a data block is numbered from 1 to n.

Afterwards the MODBUS data model has to be bound to the device application ( IEC-61131object, or other application model).

The pre-mapping between the MODBUS data model and the device application is totally vendor device specific.

modbus 用户协议 定义了精确的PDU地址规则

在一个modbus PDU中每一个数据是 规定地址在0-65535。

也明确的定义了4个块Modbus数据规范的组成 包括每个块的从1-N的元素数量。

在 MODBUS 数据模型中,数据块中的每个元素都按 1 到 n 的编号。

每一个Modbus 数据规定和用户设备是 全部由供应商设别特定的。

在这里插入图片描述

The previous figure shows that a MODBUS data numbered X is addressed in the MODBUS PDU X-1.

上图显示编号为 X 的 MODBUS 数据在 MODBUS PDU X-1 中寻址。

定义MODBUS 传输(Define MODBUS Transaction)

The following state diagram describes the generic processing of a MODBUS transaction in server side.

下面这个状态流程图,描述了在服务端,通用的MODBUS通信过程。

在这里插入图片描述

Once the request has been processed by a server, a MODBUS response using the adequate MODBUS server transaction is built.
Depending on the result of the processing two types of response are built :
A positive MODBUS response :
the response function code = the request function code
A MODBUS Exception response ( see section 7 ):
the objective is to provide to the client relevant information concerning the
error detected during the processing ;
the exception function code = the request function code + 0x80 ;
an exception code is provided to indicate the reason of the error.

一旦请求被服务器处理,使用适当的MODBUS服务器事务构建一个MODBUS响应。

根据处理的结果,将构建两种类型的响应:

一个正确的MODBUS响应:

响应函数代码=请求函数代码

MODBUS异常响应(见英文文档中的第7节):

目的是向客户提供有关的信息

处理过程中检测到错误;

异常函数代码=请求函数代码+ 0x80;

提供了一个异常代码来指示错误的原因。

功能码实例:03功能码

读保持寄存器(03 (0x03) Read Holding Registers)

This function code is used to read the contents of a contiguous block of holding registers in a remote device. The Request PDU specifies the starting register address and the number of registers. In the PDU Registers are addressed starting at zero. Therefore registers numbered 1-16 are addressed as 0-15. The register data in the response message are packed as two bytes per register, with the binary contents right justified within each byte. For each register, the first byte contains the high order bits and the second contains the low order bits.

此功能码用于读取远程设备中保存寄存器的连续块的内容。请求的 PDU 指定了起始寄存器地址和寄存器的数量。在PDU中,寄存器从零开始寻址。因此编号为1-16的寄存器被编址为0-15。响应消息中的寄存器数据在每个寄存器中打包为两个字节,二进制内容在每个字节中右对齐。对于每个寄存器,第一个字节包含高8位,第二个字节包含低8位。

  • 请求 Request
描述字节长度内容/范围
Function code (功能码)1 Byte0x03
Starting Address(起始地址)2 Bytes0x0000 to 0xFFFF
Quantity of Registers(寄存器总量)2 Bytes2 Bytes 1 to 125 (0x7D)
  • 响应 Response
描述字节长度内容/范围
Function code (功能码)1 Byte0x03
Byte count (字节数)2 Bytes2 x N*
Register value(寄存器值)N * x 2 Bytes
  • **N = Quantity of Registers **(寄存器总数)

  • 错误 Error

描述字节长度内容/范围
Error code(错误码 回应的功能码)1 Byte0x83
Exception code (异常码)1 Byte01 or 02 or 03 or 04

Here is an example of a request to read registers 108 – 110:

这里是一个请求读取寄存器 108-110 的示例

在这里插入图片描述

The contents of register 108 are shown as the two byte values of 02 2B hex, or 555 decimal.
The contents of registers 109–110 are 00 00 and 00 64 hex, or 0 and 100 decimal,respectively.

寄存器108的内容显示为十六进制022b或十进制555的两个字节值。

寄存器109-110的内容分别为00 00和00 64位十六进制,或0和100位十进制。

这篇概述,基本都是摘自,文章开头说的哪篇文档,目的主要是希望能够通过这篇文章能够让 阅读者对MODBUS协议有个浅显的了解,接下来会写一些,对FreeMODBUS 源码的浅析,作一个铺垫。

如果文章存在任何侵权,请联系我删除。

以上的英文翻译,都是我蹩脚的英文水平和有道进行翻译的,如果存在不对的地方希望评论指正。
如果有存在表达不正确的地方也同样希望大家评论指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值