Modbus简介

参考资料

  1. 官方文档: Modbus_Application_Protocol_V1_1b.pdf
  2. wiki: Modbus - Wikipedia (jinzhao.wiki)
  3. RTU与ASCII比较: Modbus RTU (ozeki.hu)
  4. 详细说明: Modicon Modbus Protocol Reference Guide
  5. modbus TCP: Modbus_Messaging_Implementation_Guide_V1_0b_tcp.pdf

名词解释

  • ADU: Application Data Unit 应用数据单元
  • PDU: Protocol Data Unit 协议数据单元
  • RTU: Remote Terminal Unit
  • CRC: Cyclic Redundancy Check 循环冗余校验
  • LRC: Longitudinal Redundancy Check 纵向冗余校验
  • 离散变量: 指其数值只能用自然数或整数单位计算的则为离散变量

协议简介

由Modicon (现在的施耐德电气)在1979年发布,用于PLC通信,已经成为事实上的工业领域通信协议的业界标准。

特点: 简单,应用广泛,请求/应答,功能码支持。

modbus支持多种通信栈传输

  1. TCP/IP over Ethernet, 端口为502
  2. Asynchronous serial 传输 over EIA/TIA-232-E 或 EIA/TIA-485 或 fiber, radio 等
  3. MODBUS PLUS, 一种高速令牌传输网络
  4. 其他

其中串口通讯最为常见。

总体结构

modbus是由 modbus ADU 构成,不同的通讯协议会对它做一定的封装,ADU的结构如下

Additinal addressModbus PDUError Check
地址域协议数据单元差错校验

PDU的结构如下

Function codeData
功能码数据

PDU的格式将在下一段详细介绍。

modbus有很多种通讯协议,下面是最为常见的三种类型。

Modbus RTU

最常用, Modbus messages are framed (separated) by idle (silent) periods.数据密度高,用于RS-485/EIA-485 等异步串口

NameLength (bits)Function
Start3.5 x 8At least 3+1⁄2 character times (28 bits) of silence (mark condition)
Address8Station address
Function8Indicates the function code; e.g., read coils/holding registers
Datan × 8Data + length will be filled depending on the message type
CRC16Cyclic redundancy check
End3.5 x 8At least 3+1⁄2 character times (28 bits) of silence (mark condition) between frames

Modbus ASCII

易读,有前导码和终止码,主要用于7-bits或8-bits的异步串口线,

NameLength (bytes)Function
Start1Colon : (ASCII value 0x3A)
Address2Station address
Function2Indicates the function codes like read coils / inputs
Datan × 2Data + length will be filled depending on the message type
LRC2Checksum (Longitudinal redundancy check)
End2Carriage return + line feed (CR/LF) pair (ASCII values 0x0D and 0x0A)

RTU和ASCII发送同样的原始数据,区别如下

源数据RTU模式ASCII模式
0x450x450x34 0x35
0xA10xA10x41 0x31
0xa10xA10x61 0x31

Modbus TCP

结构最复杂,运行在以太网上,TCP502端口。

Modbus/TCP应用层报文不需要校验(个人猜测是因为IP层/TCP层已经将校验做掉了)

modbus TCP的报文整体结构如下

Mac headerIP headerTCP headerModbus ADU
Mac头IP头TCP头Modbus应用数据单元

下面是modbus中的ADU结构

NameLength (bytes)Function
Transaction identifier2For synchronization between messages of server and client
Protocol identifier20 for Modbus/TCP
Length field2Number of remaining bytes in this frame
Unit identifier1Server address (255 if not used)
Function code1Function codes as in other variants
Data bytesnData as response or commands
NameLength (bytes)说明
Transaction identifier2事务识别符,无限制,TC和TS一致
Protocol identifier2协议识别符, 0
Length field2后续长度, 2~254
Unit identifier1单元识别符, 小于等于247或255
Function code1见PDU结构章节
Data bytesn见PDU结构章节

wireshark中将前7个固定字节成为MBAP header(Modbus Application Protocol),个人认为这相当于通用结构中的地址域(Additional address)。ADU总长度最大为260字节。

PDU

根据文档定义,功能码占一个字节,范围是1~127,分为3类

  1. 公共功能码:除去下面两种类型的功能码,均可使用,但是在文档中只有部分已定义
  2. 使用者自定义功能码:65~72, 100~110
  3. 保留功能码:被某些公司使用或者暂时不可用:8,9,10,13,14,41,42,90,91,125,126,127;

在具体解析功能码前,我们首先来了解一下功能类型和其作用的对象类型

功能类型

  1. Data Access 数据存取
  2. Diagnostics 诊断,用于查看链路状态和设备状态,只支持串口
  3. Other(Encapsulated Interface Transport?)

对象类型

Object type说明AccessSizeAddress Space
Coil线圈Read-write1 bit00001 – 09999
Discrete input离散输入,相当于保持线圈Read-only1 bit10001 – 19999
Holding register保持寄存器Read-only16 bits30001 – 39999
Input register输入寄存器Read-write16 bits40001 – 49999

功能码与数据

本文解析的功能码只限于文档中出现的已定义公共功能码,如下表所示

Function typeFunction nameFunction codeComment
Data AccessBit AccessPhysical Discrete InputsRead Discrete Inputs2
Internal Bits or Physical CoilsRead Coils1
Write Single Coil5
Write Multiple Coils15
16-bit accessPhysical Input RegistersRead Input Registers4
Internal Registers or Physical Output RegistersRead Multiple Holding Registers3
Write Single Holding Register6
Write Multiple Holding Registers16
Read/Write Multiple Registers23
Mask Write Register22
Read FIFO Queue24
File Record AccessRead File Record20
Write File Record21
DiagnosticsRead Exception Status7
Diagnostic8
Get Com Event Counter11
Get Com Event Log12
Report Server ID17
Read Device Identification43
OtherEncapsulated Interface Transport43

下面根据开发需求将PDU的功能码分成4种类型

  1. 只含有功能码,无后续数据(4种)

  2. 基本读写功能码,长度固定为5字节(6种)

  3. 长度大于5字节的PDU(7种)

  4. 含有子功能码的PDU(2种)

单功能码

这几种功能码都是只支持串行线(serial line only),对应的PDU中只有功能码,无后续数据

function code定义说明
0x07read exception status读取远端设备8bit异常状态输出(因设备而异)
0x0Bget common event count读取远端设备通信事件计数器
0x0Cget common event log时间计数 + 消息计数 + 事件描述
0x11report slave ID读取远端设备ID,运行状态和额外数据

基本读写

对应的PDU长度固定为5字节

类型说明function code 1B地址 2B数量/数值 2B
read coils读线圈0x01starting addressquantity of coils
read discrete inputs读离散输入0x02starting addressquantity of inputs
read holding registers读保持寄存器0x03starting addressquantity of registers
read input registers读输入寄存器0x04starting addressquantity of registers
write single coil写单个线圈0x05output addressoutput value
write single registers写单个寄存器0x06register addressregister value

注:

  • 如果starting address为3,说明是第4个线圈开始
  • input register是只读的,holding register可读可写

不定长

对应的PDU后续数据长度不定,功能码0x14和0x15后面的数据还可能存在多个请求

类型说明function code 1B地址 2B数量 2B字节数 1B数值 NB
write multiple coils写多个线圈0x0Fstarting addressquantity of outputsbyte countoutputs value
类型说明function code 1B地址 2B数量 1B字节数 1B数值 2NB
write multiple registers写多个寄存器0x10starting addressquantity of registersbyte countregisters value
类型说明function code 1B字节数 1B参考类型 1B文件号 2B记录号 2B记录长度 2B重复 7NB
read file record读取文件记录(读取通用参数)0x14byte count0x06(fixed)file numberrecord numberrecord length4个字段一组
类型说明function code 1B数据长度 1B参考类型 1B文件号 2B记录号 2B记录长度 2B记录数据 2NB重复 (7+2N)*MB
write file record写文件记录(读取通用参数)0x15request data length0x06(fixed)file numberrecord numberrecord lengthregister data5个字段一组
类型说明function code 1B地址 2B与掩码 2B或掩码 2B
mask write register掩写寄存器0x16reference addressand_maskor_mask
类型说明function code 1B起始读地址 2B读取数量 2B起始写地址 2B写数量 2B后续字节数 1B写寄存器值 2NB
read/write multiple registers读写多个寄存器0x17read starting addressquantity to readwrite starting addressquantity to writewrite byte countwrite registers value
类型说明function code 1B地址 2B
read FIFO queue读先进先出队列0x18FIFO pointer address

子功能码

含有子功能码的PDU格式如下

function codesub-function codedata
功能码 1B子功能码 1B/2B数据 不定长
(0x08)Diagnostics(Serial line only)

子功能码长度为2B,范围为0~65535,其中 5~9, 19, 21~65535 保留功能码。已定义的子功能码对应的数据大部分为2字节,值为0;子功能码 0x00 对应数据为任意长度;子功能码 0x01 对应的数据有两种;子功能码 0x03 的数据为字符串’CHAR’ + 00;

类型说明sub-function code 2Bdata 不定长
return query data发送若干数据,返回若干相同数据0000any
restart communication option清空事件计数器,Log是否保留可选0010000/FF00(clear log)
return diagnostic register返回诊断寄存器00020000
change ASCII input delimiter提示不需要换行符(Line Feed)0003CHAR 00
force listen only mode进入只听模式00040000
clear counters and diagnostic register清空所有计数器和诊断寄存器000A0000
return bus message count返回消息数量000B0000
return bus communication error count返回CRC校验错误数量000C0000
return bus exception error count返回异常回复数量000D0000
return slave message count返回远端设备已处理消息数量000E0000
return slave no response count返回远端设备未回复消息数量000F0000
return slave NAK count返回远端设备未确认异常回复数量00100000
return slave busy count返回远端设备忙的异常回复的数量00110000
return bus character overrun count返回字符速度过快的异常回复数量00120000
clear overrun counter and flag清空overrun计数器,重置标志00140000
(0x2B)Encapsulated Interface Transport

本类型用于隧道服务和方法调用,下属子功能码的名称为MEI type(Modbus Encapsulated Interface),常用的有两种,其他均为保留

MEI type 1Bdata NB
0x0D与CANopen 系统和设备进行交互
MEI type 1Bread device ID code 1Bobject ID 1B
0x0E01/02/03/040x00~0xFF
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值