How Modbus Communication Protocol Works
Resources
How does Modbus Communication Protocol Work?
Overview
Modbus is a widely used communication protocol in industrial automation, allowing devices to communicate with each other. This guide explains the structure and operation of Modbus messages and how Modbus communication works between devices.
Modbus Message Structure
Each Modbus message consists of four basic elements, which follow the same sequence and order in all messages, making it easy to parse the content.
1. Slave Address:
Identifies the device the message is intended for.
2. Function Code:
Specifies the action to be performed (e.g., read or write).
3. Data:
Contains the data to be read or written.
4. Error Checking Field:
Ensures the integrity of the message.
Communication Process
Master/Slave Communication:
In a Modbus network, the master device initiates communication by sending a message to a slave device. The slave then processes the message and responds.
Physical Addressing:
The message header contains the slave's address, ensuring only the intended device responds while others ignore the message.
Modbus Functions and Registers
Modbus functions perform read and write operations on the slave's internal memory registers, which are organized into four basic data types:
1. Discrete Inputs:
Single-bit inputs (e.g., status of a button).
2. Coils Outputs:
Single-bit outputs (e.g., status of a relay).
3. Input Registers:
16-bit read-only data (e.g., sensor readings).
4. Holding Registers:
16-bit read/write data (e.g., control setpoints).
Modbus Protocol Data Unit (PDU)
The PDU consists of:
Function Code:
Indicates the operation to be performed (e.g., read or write).
Data:
Includes the address of the memory register and the number of registers to read/write.
Common Modbus Function Codes
Function Code 01: Read Coils
Function Code 02: Read Discrete Inputs
Function Code 03: Read Holding Registers
Function Code 04: Read Input Registers
Function Code 05: Write Single Coil
Function Code 06: Write Single Register
Function Code 15: Write Multiple Coils
Function Code 16: Write Multiple Registers
Error Handling
If an error occurs, the slave device returns a message with an error code. The error code is the original function code with the most significant bit set to 1, along with an error description.