服务支持

26

2023

-

09

Introduction to the Modbus TCP Protocol


Developed by MODICON in 1979, Modbus is an industrial fieldbus protocol standard. In 1996, Schneider Electric introduced a Modbus protocol based on Ethernet TCP/IP: Modbus TCP. The Modbus protocol is an application-layer message transmission protocol that supports three message types: ASCII, RTU, and TCP. The standard Modbus protocol’s physical-layer interfaces include RS-232, RS-422, RS-485, and Ethernet, and it employs a master/slave communication mode. Reprinted from “Automation and Engineering Technology.”

01-Modbus TCP Data Frame

A Modbus TCP data frame consists of two parts: MBAP and PDU.

(1) MBAP Message Header: MBAP is the message header, with a length of 7 bytes, and is composed as follows:

(2) Frame Structure PDU: A PDU consists of a function code plus data. The function code is 1 byte, while the data length is variable and determined by the specific function.

(1) Function Codes: Modbus supports four types of data objects: coils, discrete inputs, holding registers, and input registers.

(2) Depending on the device, Modbus function codes include:

(3) Provide a more detailed table:

02-Detailed Structure of the PDU

(1) 0x01: Read Coils: Reads the status of 1 to 2,000 consecutive coils on the slave device, where ON = 1 and OFF = 0.
Request: MBAP Function Code, Start Address High Byte, Start Address Low Byte, Quantity High Byte, Quantity Low Byte (total of 12 bytes)
Response: MBAP Function Code Data Length Data (1 bit per address)
For example: In slave station 0x01, read coil data starting at address 0x0002, reading 0x0008 bits.
00 01 00 00 00 06 01 01 00 02 00 08
Reply: The data length is 0x01 byte, with the data value being 0x01; the first coil is ON, and all others are OFF.
00 01 00 00 00 04 01 01 01 01

(2) 0x05: Write Single Coil: Writes the output of a slave device to ON or OFF. A value of 0xFF00 requests the output to be set to ON, while a value of 0x000 requests it to be set to OFF.
Request: MBAP Function Code, Output Address H, Output Address L, Output Value H, Output Value L (total of 12 bytes)
Response: MBAP Function Code, Output Address H, Output Address L, Output Value H, Output Value L (total of 12 bytes)
For example: Set the coil at address 0x0003 to ON.
00 01 00 00 00 06 01 05 00 03 FF 00
Reply: Write successful
00 01 00 00 00 06 01 05 00 03 FF 00

(3) 0x0F: Write Multiple Coils: Forces each coil in a sequence of coils on a slave device to ON or OFF. A bit set to 1 in the data field requests the corresponding output bit to be set to ON, while a bit set to 0 requests the corresponding output bit to be set to OFF.
Request: MBAP Function Code, Start Address High, Start Address Low, Number of Outputs High, Number of Outputs Low, Byte Length, Output Value High, Output Value Low
Response: MBAP Function Code, Start Address High Byte, Start Address Low Byte, Number of Outputs High Byte, Number of Outputs Low Byte

(4) 0x02: Read Discrete Inputs: Reads 1 to 2,000 consecutive discrete input states from a slave device.
Request: MBAP Function Code, Start Address High Byte, Start Address Low Byte, Quantity High Byte, Quantity Low Byte (total of 12 bytes)
Response: MBAP Function Code Data Length Data (Length: 9 + ceil(Quantity / 8))
For example: Read 0x0012 discrete input points starting from address 0x0000.
00 01 00 00 00 06 01 02 00 00 00 12
Response: The data length is 0x03 bytes, with the data sequence 0x01 04 00, indicating that the first discrete input and the 11th discrete input are ON, while all others are OFF.
00 01 00 00 00 06 01 02 03 01 04 00

(5) 0x04: Read Input Registers: Reads 1 to 2,000 consecutive input registers from a remote device.
Request: MBAP Function Code, Start Address High, Start Address Low, Number of Registers High, Number of Registers Low (total of 12 bytes)
Response: MBAP Function Code Data Length Register Data (Length: 9 + Number of Registers × 2)
For example: Read the register data starting at address 0x0002 with a count of 0x0005.
00 01 00 00 00 06 01 04 00 02 00 05
Reply: The data length is 0x0A, with the first register containing 0x0C and the remaining registers all set to 0x00.
00 01 00 00 00 0D 01 04 0A 00 0C 00 00 00 00 00 00 00 00

(6) 0x03: Read Holding Registers: Reads the contents of a contiguous block of holding registers from a remote device.
Request: MBAP Function Code, Start Address High, Start Address Low, Number of Registers High, Number of Registers Low (total of 12 bytes)
Response: MBAP Function Code Data Length Register Data (Length: 9 + Number of Registers × 2)
For example: the starting address is 0x0000, and the number of registers is 0x0005.
00 01 00 00 00 06 01 03 00 00 00 05
Reply: The data length is 0x0A, with the first register containing 0x0C and the remaining registers all set to 0x00.
00 01 00 00 00 0D 01 04 0A 00 0C 00 00 00 00 00 00 00 00

(7) 0x06: Write Single Holding Register: Writes a single holding register in a remote device.
Request: MBAP Function Code, Register Address High, Register Address Low, Register Value High, Register Value Low (total of 12 bytes)
Response: MBAP Function Code, Register Address High, Register Address Low, Register Value High, Register Value Low (total of 12 bytes)
For example: Write the data 0x000A to the register at address 0x0000.
00 01 00 00 00 06 01 06 00 00 00 0A
Reply: Write successful
00 01 00 00 00 06 01 06 00 00 00 0A

(8) 0x10: Write Multiple Holding Registers: Writes a contiguous block of registers (1 to 123 registers) in a remote device.
Request: MBAP Function Code, Start Address High, Start Address Low, Number of Registers High, Number of Registers Low, Byte Length, Register Values (13 + Number of Registers × 2)
Response: MBAP Function Code, Start Address High Byte, Start Address Low Byte, Number of Registers High Byte, Number of Registers Low Byte (total of 12 bytes)
For example: Write data to a register starting at address 0x0000 with a count of 0x0001, where the data length is 0x02 and the data value is 0x000F.
00 01 00 00 00 09 01 10 00 00 00 01 02 00 0F
Reply: Write successful
00 01 00 00 00 06 01 10 00 00 00 01

03-Modbus TCP Example Message

The data fields of Modbus TCP are consistent with those of serial-link Modbus; for details, refer to the serial Modbus specification. Below are several explanations for parsing Modbus TCP links to help newcomers analyze messages.

04-Modbus TCP Communication

(1) Communication Methods
Modbus devices are classified as either masters (polling) or slaves. There is only one master, while there can be multiple slaves. The master sends request frames to each slave, and the slaves respond accordingly. When using TCP communication, the master acts as the client and actively initiates the connection, whereas the slave acts as the server and waits for the connection to be established.
Master station request: Function code + data
Normal response from the slave: Request function code + response data
Slave device abnormal response: abnormal function code + exception code, where the abnormal function code sets the most significant bit of the requested function code to 1, and the exception code indicates the type of error.
A timeout management mechanism is required to prevent indefinite waiting for a response that may never arrive.
The Internet Assigned Numbers Authority (IANA) has assigned TCP port 502 to the Modbus protocol, which is currently the only port number allocated in the instrumentation and automation industry.

(2) Communication Process
connect: Establish a TCP connection
Prepare Modbus message
Use the send command to send a message.
Wait for the response on the same connection
Use the recv command to read the message, completing a single data exchange.
Close the TCP connection when the communication task is completed.

05-Simulation Software

Modbus Poll and Modbus Slave are a pair of Modbus simulation software applications that support Modbus RTU, TCP, and serial port emulation, among other features.
Simulation software website: https://modbustools.com/download.html
In Modbus TCP, the Modbus Poll acts as the client to request data, while the Modbus Slave acts as the server to process the requests.
When using C to develop a client that connects to a Modbus slave, pay attention to the data format: send each command in a single transmission; otherwise, the connection will fail.
When using the software, you must specify the function code, which can be configured in Setup → Slave Definition or Poll Definition.
– Slave ID: Slave station number (transaction identifier)
– function: Function code; 0x01 corresponds to coil operations, 0x02 corresponds to discrete input operations, 0x03 corresponds to holding register operations, and 0x04 corresponds to input register operations.
– address: starting address
– quantity: the number of registers/coils/discrete points