To read/write data from two Modbus RTU slave devices and access multiple registers, you need to configure your S7-300 or S7-1200 as the Modbus RTU Master. Below is a step-by-step guide:
1. Hardware Requirements
For S7-300 (Modbus RTU Master)
- CP341 (with RS485 interface) + Modbus RTU Loadable Driver
OR - RS232/USB to RS485 Modbus Converter (like Anybus, Moxa, or Westermo)
For S7-1200 (Modbus RTU Master)
- CM1241 (RS485 Module) (if your PLC doesn't have a built-in RS485 port)
2. Software Configuration in TIA Portal or STEP 7
Function Blocks for Modbus RTU
- MB_COMM_LOAD → Initializes the serial port.
- MB_MASTER → Sends requests to read/write Modbus registers.
3. Steps to Read/Write Multiple Registers from Two Slaves
Step 1: Configure the Modbus Communication Port
- For S7-300, configure the CP341/CP340 module.
- For S7-1200, use MB_COMM_LOAD to initialize RS485.
MB_COMM_LOAD Parameters
| Parameter | Description |
|---|---|
| PORT | RS485 (CM1241 or CP341) |
| BAUD | 9600, 19200, etc. |
| PARITY | Even/Odd/None |
| STOP_BITS | 1 or 2 |
Step 2: Read Registers from Modbus Slaves
Use MB_MASTER to read multiple registers from each slave device.
MB_MASTER Parameters
| Parameter | Description |
|---|---|
| REQ | Trigger signal (rising edge) |
| MODE | 1 (Read Holding Registers) |
| DATA_ADDR | Start register address |
| DATA_LEN | Number of registers to read |
| SLAVE | Slave ID (e.g., 1 or 2) |
| DONE | High when data is received |
| ERROR | High if communication fails |
| DATA_PTR | Pointer to store data |
Example
-
Slave 1 (ID = 1), Read Registers 40001-40010
SLAVE = 1DATA_ADDR = 0DATA_LEN = 10- Store data in DB10.DBB0
-
Slave 2 (ID = 2), Read Registers 40011-40020
SLAVE = 2DATA_ADDR = 10DATA_LEN = 10- Store data in DB10.DBB10
Step 3: Write Data to Multiple Registers
To write to a Modbus slave, use MB_MASTER with MODE = 2 (Write Multiple Registers).
- Example: Write data to Slave 1 at address 40050-40055
SLAVE = 1MODE = 2DATA_ADDR = 50DATA_LEN = 6- Source data from DB20.DBB0
4. Execution Logic
- Call MB_COMM_LOAD once at startup.
- Continuously call MB_MASTER in a cyclic loop (OB1).
- Use a state machine or timer to alternate between Slave 1 and Slave 2.
- Use the DONE bit to trigger the next read/write operation.
5. Summary
- Use MB_MASTER twice (once for each Slave).
- Configure correct Slave ID and Register Addresses.
- Ensure polling intervals are set properly.
- Implement error handling (timeout, retries).
Next Steps
- Do you need a sample PLC program for Modbus RTU communication?
- What are the register addresses for your Modbus devices?