Getting Started with Modbus

Reprinted from: http://bbs.gkong.com/archive.aspx?id=340353

A communication protocol commonly used in the industry, a communication convention. Modbus protocol includes RTU, ASCII, TCP. Among them, MODBUS-RTU is the most commonly used, relatively simple, and it is easy to realize on the single-chip microcomputer. Although RTU is relatively simple, reading the protocol materials and manuals is too professional, and many contents are difficult to understand at first.
    What is the so-called agreement? It's a mutual agreement. If you don't let others know, it's a secret code. Now let's define a new simplest protocol. For example, the

protocol: "A" -- "LED off"
       "B" -- "Alarm"
       "C" -- "LED on" The
microcontroller receives "A" to control an LED off, and the microcontroller receives "B" to control the alarm, The microcontroller receives "A" and controls an LED to light up. Then when the corresponding information is received, the corresponding action is performed. This is the protocol, which is very simple.

  Let's briefly analyze a MODBUS-RTU message, for example: 01 06 00 01 00 17 98 04 
    01 06 00 01 00 17 98 04 
  slave address function number data address data CRC check

this string of data means: 0x0017 (Decimal 23) Write the data address of No. 1 slave address 0x0001.

First understand the following things.

1. Message
   A message is a frame of data, and a data frame is a message: It refers to a string of complete command data, just like the string of data above.

2. CRC check
Meaning: For example, the above 98 04 is the result calculated by the data in front of it (01 06 00 01 00 17) through an algorithm (see Appendix 2, very simple), which is actually like calculating the cumulative sum. (The cumulative sum: it is the value of 010600010017 added up, and then its algorithm is addition).
Function: In the process of data transmission, there may be errors in the data, and the CRC test detects whether the received data is correct. For example, if the host sends 01 06 00 01 00 17 98 04, the slave will calculate the CRC check value according to 01 06 00 01 00 17 after receiving it, and the slave will judge whether the CRC check calculated by itself is the same as the received CRC check. (calculated by the 98 04 host) are equal. If they are not equal, it means that there is an error in the data transmission. These data cannot be requested.

3. Function number
  meaning: modbus definition. See Appendix 1.
  Function: Indicates a specific operation.

MODBUS-RTU
1. A message analysis
First declare our purpose, we want two devices to communicate, using the MODBUS protocol. The above briefly introduces: "message", "CRC check" and "function number".

Take out a part of the memory (RAM) in the microcontroller for communication between two devices, for example:

附件
数组后面的注释,说明
OX[20]   代表是输出线圈,用功能码 0x01,0x05,0x0F 访问, 开头地址是 0 (这个后续说明)
IX[20]    代表是输入线圈,用功能码 0x02 访问,             开头地址是 1 (这个后续说明)
另外两个一样的道理。
注意:所谓的“线圈”“寄存器”就是“位变量”“16位变量”,不要被迷惑。之所以称“线圈”我觉得应该是对于应用的设备,MODBUS协议是专门针对485总线设备(例PLC)开发的。

1、主机对从机写数据操作
如果单片机接收到一个报文那么就对报文进行解析执行相应的处理,如上面报文:
    01             06            00 01           00 17          98 04 
  从机地址        功能号          数据地址          数据         CRC校验

假如本机地址是 1 ,那么单片机接收到这串数据根据数据计算CRC校验判断数据是否正确,如果判断数据无误,则结果是:
            HoldDataReg[1]  =  0x0017;
MODBUS主机就完成了一次对从机数据的写操作,实现了通讯。

2、主机对从机读数据操作
主机进行读HoldDataReg[1] 操作,则报文是:
    01             03            00 01           00 01          D5 CA 
 从机地址        功能号          数据地址      读取数据个数       CRC校验
那么单片机接收到这串数据根据数据计算CRC校验判断数据是否正确,如果判断数据无误,则结果是:返回信息给主机,返回的信息也是有格式的:
返回内容:  
    01         03            02             0017          F8 4A
  从机地址   功能号     数据字节个数    两个字节数据    CRC校验
MODBUS主机就完成了一次对从机数据的读操作,实现了通讯。


二、MODBUS报文模型

以上了解到了MODBUS的一帧报文是如何通讯的,其实每个报文的格式都基本一样的。

附件

                             

这里两个缩略词以前不知道,但是现在要明白指的是什么,“ADU”“PDU”
ADU: 应用数据单元
PDU: 协议数据单元

三、MODBUS数据模型

附件
附件
  


四、MODBUS事务处理
  下列状态图描述了在服务器侧MODBUS事务处理的一般处理过程。
           
附件

五、MODBUS请求与响应
  看MODBUS协议手册,中文第 10 页开始,英文第 24 页开始。手册非常详细举例说明了MODBUS协议各个功能号的请求与响应。 



                                                         modbus协议在单片机上实现过程


MODBUS 任务处理函数

附件

函数中,RcvBuf 为串口接收缓冲区,如果接收了一个报文则,RcvBuf[0] 为从机地址,RcvBuf[0] 为MODBUS功能号。根据功能号做出响应,而具体的操作根据功能号在各自的函数中执行,相当于解析接收到的数据。

附录1:MODBUS-RTU功能码
 最常用功能码:
 下面“线圈”“寄存器”其实分别直的就是“位变量”“16位变量”
        01 (0x01)        读线圈 
        02 (0x02)        读离散量输入
        03 (0x03)        读保持寄存器
        04(0x04)         读输入寄存器
        05 (0x05)        写单个线圈 
        06 (0x06)        写单个寄存器
        15 (0x0F)        写多个线圈 
        16 (0x10)        写多个寄存器

附件

附录2 :CRC Generation

附件

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325952211&siteId=291194637