Analysis of common problems in the use of Modbus protocol

The Modbus protocol was originally developed by Modicon, which is an application layer bus device protocol based on the master-slave mode of the serial bus designed for PLC devices. ModbusTCP is a Modbus protocol encapsulated in TCP packets. Although there are some changes, it is basically a master-slave mode.

With the development of embedded technology, the control and acquisition units of many domestic systems are independently developed by the company. It is generally recommended that the serial communication protocol of these companies use the Modbus protocol. Many users have misunderstandings in the modbus protocol. The analysis is as follows:

1. The holding and input registers of modbus are in units of word (16bit).

For example, 4 **** (holding register / output register) and 3 **** (input register) are in word units. So, if you read a 16-bit unsigned number at the beginning of the 40001 register, then return 2 Bytes, and you can read the next 16-bit unsigned number from 40002.

However, if you read a 32-bit floating-point number starting with the 40001 register, then return 4 bytes, and the next 32-bit floating-point number must start from 40003.

common problem:

1). Define 40001 as a Byte of data;

2). Define 40001 as a 32-bit floating point number, and 40002 as the next 32-bit floating point number.

2. The minimum address of the register is 1, and the starting address of the message is 0.

In the data message, all modbus addresses start from 0. That is, the address of the first occurrence of the data item in the message is 0. such as:

1. In the controller, the address of "coil 1" in the address field of the Modbus message is 00 00.

2. The hexadecimal message address of coil 127 is 007E hex (126 decimal)

3. The message address of holding register 40001 is 00 00. Because the message function code clearly wants to operate the "hold register", the protocol uses "4XXXX" to represent this register.

4. The message address of holding register 40108 is 006B hex (decimal 107)

In short, Modbus address generally refers to 4 **** (holding register / output register) and 3 **** (input register), at this time the application level:

For example, the device manual can briefly explain that the device supports the Modbus RTU standard protocol, and describe in detail its address correspondence: 40001-analog acquisition channel 1, 16-bit signed number, ..... For example, the address setting of the configuration software is generally an output register, starting from address 1, how many consecutive. Or specify 400001: 16-bit signed number. However, at the data message level, the register start address starts at 0.

The data message includes: device address + function code + start address + number of registers + check digit. Among them, the starting address starts from 0.

For example: the device 17 reads the data of the two register data starting from 40001

Device address Function code start address Register number check

11 03 00 00 00 02-

common problem:

1. Use and define 40,000 addresses;

2. When analyzing the message, the start address of the message is directly used as the address of the application layer register.

3. Different Modbus write register commands.

Common Modbus registers are: Coil, Input, Holding Registers, and Input Registers.

From the perspective of the Modbus device, the input is the information collected by the host computer to the Modbus device, that is, these registers are read-only, so the Modbus protocol does not have commands to write Input and Input Registers.

Coil (Coil) is the state quantity, corresponding to the switching output (DO) of the Modbus device, holding registers (analog), corresponding to the analog output (AO) of the Modbus device, these registers need to be set by the host computer of the Modbus device, It is a register that can be written.

In Modicon_Modbus protocol, there are two write commands for writing coil (Coil) and holding registers (Holding Registers):

1), write a single register:

Set single coil (Force Single Coil) function code 05 (0x05)

Write a single register (Preset Single Holding Register) function code 06 (0x06)

2), write multiple registers

Write multi-coil (Force MulTIple Coils) function code 15 (0x0F)

Write multiple registers (Preset MulTIple Registers) function code 16 (0x10)


401-999Kva Diesel Generator

401-999Kva Diesel Generator,Shanghai Genset,Shanghai Power Generator,Power Generator Set

Shanghai Kosta Electric Co., Ltd. , https://www.kostagenerators.com