My Birth
Serial communication is a commonly used communication protocol in industry, mainly RS232, RS485, and RS422. Any communication between two devices requires a communication protocol.
Manufacturer A has developed a temperature sensor that uses the RS485 communication interface and defined Protocol A;
Manufacturer B has also developed a temperature sensor, which similarly uses the RS485 communication interface and defined Protocol B;
Manufacturer C has developed a pressure sensor that uses the RS485 communication interface and defined Protocol C.
In a project that uses devices A, B, and C, although they all use the RS485 communication interface, the communication protocols are different, which means developers need to understand three protocols, increasing the development difficulty.
Is there a way to use a single communication protocol, where reading address 0x10 with device A retrieves the temperature value, reading address 0x20 with device B retrieves the temperature value, and reading address 0x30 with device C retrieves the pressure value? This way, future projects using devices with the same protocol will be much faster to develop.
This protocol is the Modbus communication protocol.
My Characteristics
The Modbus protocol is free, open-source, has no copyright fees, is simple to understand, greatly increases device connectivity, and reduces the workload for developers.
Modbus is a master-slave protocol, meaning the master station actively sends requests, and the slave station responds.
In devices using RS485 communication, since RS485 can be cascaded, multiple devices can be connected on a single bus, with one master station and multiple slave stations. To distinguish different slave stations, each slave is assigned a station number.
The Modbus communication protocol has RTU, ASCII, and TCP (UDP):
- RTU communication protocol is used in RS232, RS485, and RS422
- ASCII communication protocol is used less due to low efficiency
- TCP (UDP) communication protocol is used for devices connected via Ethernet, distinguishing slave stations by IP
Modbus Data Model
Modbus abstracts device data into four basic types, each with its own address space.
| Data Type | Object Type | Access Method | Description |
|---|---|---|---|
| Coils | Single Bit | Read/Write | Typically represents binary output, such as the switch state of a relay (ON/OFF) |
| Discrete Inputs | Single Bit | Read Only | Typically represents binary input, such as the state of buttons or limit switches (ON/OFF) |
| Holding Registers | 16-bit Word | Read/Write | Typically stores parameters that need to be modified or set, such as temperature setpoints, PID parameters, etc. |
| Input Registers | 16-bit Word | Read Only | Typically stores data read from sensors, such as current temperature, pressure, flow, etc. |
Common Function Codes
The function code is the core of the Modbus request frame, indicating what operation the slave should perform.
| Function Code | Name | Function |
|---|---|---|
| 01 (0x01) | Read Coils | Read the status of one or more coils |
| 05 (0x05) | Write Single Coil | Write to a single coil (force ON/OFF) |
| 15 (0x0F) | Write Multiple Coils | Write to multiple coils |
| 02 (0x02) | Read Discrete Inputs | Read the status of one or more discrete inputs |
| 03 (0x03) | Read Holding Registers | Read the values of one or more holding registers |
| 06 (0x06) | Write Single Register | Write to a single holding register |
| 16 (0x10) | Write Multiple Registers | Write to multiple holding registers |
| 04 (0x04) | Read Input Registers | Read the values of one or more input registers |
Message Frame
PDU (Protocol Data Unit): Function Code + Data
ADU (Application Data Unit): Based on PDU, adds network-related additional address and error check information, Additional Address + PDU + Error Check Information
In Modbus RTU/ASCII and Modbus TCP, the format of ADU is different,
Modbus RTU/ASCII:
Slave Address (1 byte) + PDU (Function Code + Data) + Error Check (2 bytes), forms the message frame
Modbus TCP:
MBAP Header (7 bytes) + PDU (Function Code + Data), forms the message frame
Specific meaning of MBAP:
| Name | Length | Description | Client | Server |
|---|---|---|---|---|
| Transaction Identifier | 2 bytes | Identifier for MODBUS request/response transaction processing | Client initiates | Server replicates from received request |
| Protocol Identifier | 2 bytes | 0=MODBUS protocol | Client initiates | Server replicates from received request |
| Length | 2 bytes | Number of following bytes | Client initiates (request) | Server (response) initiates |
| Unit Identifier | 1 byte | Identifier for remote slave connected via serial link or other bus | Client initiates | Server replicates from received request |