Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

For more exciting content, click to follow “PLC Enthusiasts”

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

The Siemens S7-1200 can communicate via Modbus RTU through communication modules, with the PLC serving as the controller, typically acting as the Modbus master. When functioning as a Modbus master, it generally polls parameters from multiple slave devices. This article introduces the timed polling program for the Siemens S7-1200 when acting as a Modbus RTU master.

1

Main Hardware and Software Used

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

2

Communication Tasks

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

3

Program Logic

To achieve this functionality, a timer polling method is used to read and write parameters, as shown in the flowchart below.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

4

PLC Program

In this program, the FB block method is used for encapsulation, with the PLC variables, PLC data types, global data, and FB interface as shown below.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Power-on Initialization Program

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Change the effective working mode to 4, which is half-duplex (RS485) two-wire mode.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Initialize the Modbus configuration port, defining the first cycle, hardware identifier, baud rate, and parity in the parameters. MB_DB comes from the background data block of the Modbus RTU master.

Timer Program

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Define a 100ms timer pulse, which can be adjusted according to the reading and writing speed of the Modbus master.

Counter Program

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Define the value of the counter, incrementing by 1 with the signal from the timer. When the counter value is greater than or equal to the number of tasks, reset the counter value. The number of tasks here is the number of tasks during Modbus polling, which is 4 in this program.

Modbus RTU Master Communication

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Since the PLC data type (UDT) array starts from 0, the counter value is decremented by 1.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

The Modbus master polling program uses the timer signal as the trigger signal, with input data and status using PLC data types (UDT), and different tasks addressed using indexes.

Calling FB Block in OB

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

Call this FB block in OB1 and associate it with actual parameters.

5

Parameter Settings

In this project, 5 tasks need to be completed (see 2. Communication Tasks), since tasks 1 and 2 do not run simultaneously, 4 tasks are polled. The parameters are set in the global data block, as shown in the figure.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

The slave address, read/write mode, data address, and data length need to be set according to the actual tasks.

For one of the task requirements, reading and writing are separated, so external conditions can modify the mode and given values, as shown in the figure below.

Case Study: Timed Polling Program Based on S7-1200 Modbus RTU

When parameters need to be written, a write command is given, which is the “Material Collection Complete” signal here. Through this signal, modify the read/write mode and value of task 1 (index 0). When this task is completed or an error occurs, reset this signal.

6

Conclusion

This is the complete content of the program. The advantage of this timed polling method is that even if one device fails, due to its timed polling nature, it does not affect other devices. It is suitable for scenarios where slave data is similar, the read/write speeds are close, and fault handling is not required. If anyone has more applications related to Modbus, feel free to leave a message for discussion.

Article Source: Jicheng Training Network Original, Author: Liu Chen; Reproduction without authorization is prohibited, and violators will be prosecuted!

Previous Recommendations

Essential calculation software for electrical engineers (automatically generated), recommended for everyone!

Do you have your own work laboratory? Let’s appreciate others’!

How does PLC convert and process analog signals? Solve it in 3 minutes!

[Case Study] Detailed steps for MODBUS communication between Siemens frequency converters and PLCs~

Why are PLC engineers also learning advanced programming techniques for upper-level machines?

Detailed resources for Siemens 1200 PLC communication connections, how many devices can be connected?

👇👇👇Click ‘Read the original text’ to directly receive a copy of electrical materials~

Leave a Comment