Modbus RTU Communication: How to Write for PLCs and Easily Connect Device Data
Implementing Modbus RTU communication with Siemens PLCs to achieve data exchange among multiple devices, applicable in industrial sites. Today, I will guide you step by step!
Project Requirements and Functionality Description
The main requirements of this project are:
-
Enable data communication between Siemens S7-1200 PLC and devices that support the Modbus RTU protocol, such as inverters, instruments, etc. -
The PLC acts as the master station, actively reading real-time data from slave devices, such as current, voltage, temperature, etc. -
Support the PLC sending control commands to slave devices, such as starting and stopping operations. -
Display communication status and data returned from the slave on the HMI for easy monitoring. -
The system must handle communication exceptions, such as timeouts or CRC validation failures, with timely alarms or retries.
Decomposition of Implementation Logic
The entire Modbus RTU communication logic can be broken down into several parts:
-
Communication Initialization Logic: Configure the PLC’s serial port parameters, such as baud rate, parity, data bits, etc., to ensure proper communication with slave devices. -
Data Reading Logic: The PLC uses Modbus function code 03 to read the holding register data from the slave device. -
Data Writing Logic: The PLC uses Modbus function code 06 to write data to a single register of the slave device, enabling the sending of control commands. -
Communication Exception Handling: Handle communication timeouts, data validation failures, etc., to prevent system operation errors. -
Status Display Logic: Real-time display of communication status and data returned from the slave on the HMI, allowing operators to quickly view and confirm.
Code Implementation
Below is the detailed ladder diagram code for implementing Modbus RTU communication with Siemens S7-1200. The logic is clear and easy to understand.
1. Input/Output Allocation Table
First, organize resource allocation for more structured programming.
Name | Address | Description |
---|---|---|
Serial Communication Status Display | DB1.DBX0.0 | HMI displays serial communication status |
Slave Data Reading Register | DB1.DBD0 | Data read from slave register by PLC |
Control Command Writing Register | DB1.DBD4 | Data written by PLC to the slave |
Communication Error Alarm Signal | Q0.0 | Triggered when communication is abnormal |
Data Refresh Trigger Signal | M0.0 | Manual signal to trigger PLC data refresh |
2. Communication Initialization Logic
Use the PLC’s communication function block (MB_COMM_LOAD) to set serial communication parameters, such as baud rate, data bits, stop bits, and parity.
| MB_COMM_LOAD
|---| |-------( )---| // Initialize Modbus communication module
In the MB_COMM_LOAD block, configure the PLC’s communication port, such as RS485 or RS232, and the baud rate (usually 9600) to ensure normal communication.
3. Data Reading Logic
Use the Modbus function block (MB_MASTER) with function code 03 to read the holding register data from the slave device, such as real-time current or temperature values.
| M0.0 MB_MASTER
|---| |-------( )---| // Trigger reading slave data
| MB_MASTER DB1.DBD0
|---|MOV|------( )---| // Write data returned from slave to data block
MB_MASTER needs to set the slave address (e.g., 1), the starting address of the register, and the number of registers to read, with the returned data stored in the PLC’s data block.
4. Data Writing Logic
Use the Modbus function block (MB_MASTER) with function code 06 to write data to a single register of the slave device, such as sending a start command.
| DB1.DBD4 MB_MASTER
|---|MOV|------( )---| // Send control command to slave device via Modbus
The register address and data content to be written need to be set according to the slave device’s manual, such as the register address corresponding to the start signal.
5. Communication Exception Handling
Monitor the status of MB_MASTER to determine if communication is normal. If there is a timeout or validation failure, trigger an alarm signal and log the error information.
| NOT MB_MASTER_STATUS Q0.0
|---| |-------( )---| // Trigger alarm signal for communication exception
| MB_MASTER_STATUS DB1.DBX0.0
|---| |-------( )---| // Update HMI status when communication is normal
6. Status Display Logic
Real-time display of communication status and data returned from the slave on the HMI, allowing operators to quickly view device status.
| DB1.DBD0 HMI_DISPLAY
|---|MOV|------( )---| // Write slave data to HMI display
| DB1.DBX0.0 HMI_COMM_STATUS
|---| |-------( )---| // Write communication status to HMI
Optimization and Common Issues
-
Communication Timeout Issue: Slow response from the slave device may cause timeouts; consider increasing the timeout duration for MB_MASTER to avoid misjudging communication exceptions. -
CRC Validation Failure Issue: Signal interference may cause data validation failures; it is recommended to use shielded twisted pairs and ensure proper grounding to reduce interference. -
Register Address Error Issue: Incorrect register addresses and function code settings for the slave device can lead to data reading failures; carefully check the device manual before programming. -
Multiple Slave Communication Issues: When communicating with multiple slaves, different slave addresses need to be set to avoid address conflicts, and the program must also distinguish and handle data from each slave.
Complete Code Framework
Below is the complete ladder diagram code framework:
| MB_COMM_LOAD // Initialize communication
|---| |-------( )---|
| M0.0 MB_MASTER // Read slave data
|---| |-------( )---|
| MB_MASTER DB1.DBD0 // Write slave data to data block
|---|MOV|------( )---|
| DB1.DBD4 MB_MASTER // Write control command to slave
|---|MOV|------( )---|
| NOT MB_MASTER_STATUS Q0.0 // Alarm for communication exception
|---| |-------( )---|
| MB_MASTER_STATUS DB1.DBX0.0 // Display communication status
|---| |-------( )---|
| DB1.DBD0 HMI_DISPLAY // Display slave data
|---|MOV|------( )---|
Conclusion
The key to Modbus RTU communication is the configuration and matching of function codes; debugging clearly will ensure stability! If you have any questions, feel free to leave a message, and we will solve it together!