S7-1200 MODBUS TCP Communication Multi-Request Handling

S7-1200 MODBUS TCP Communication Multi-Request Handling

Rules to be followed for individual client connections:

1. Each “MB_CLIENT” connection needs to use a different background data block;

2. Each “MB_CLIENT” connection must specify a server IP address;

3. Each “MB_CLIENT” connection must specify a unique connection ID;

4. Whether a unique IP port number is required depends on the server configuration;

The connection ID and background data block form a unique pair for each connection.

For multi-request handling, there are two methods:

Method 1: If TCP connections are abundant, multiple different connections can be established to send requests, that is, each request uses an independent connection, calling independent “MB_CLIENT” function blocks. For specific operations, refer to the “Modbus TCP” section, link.

Sending multiple requests through different connections involves multiple calls to the “MB_CLIENT” function block, but requires using different background data blocks, different ID numbers, and different port numbers.

Method 2: Sending multiple requests through a common connection.

This method is suitable when the CPU accesses a Modbus TCP server, but there are multiple jobs, such as both reading and writing, or reading multiple areas, etc.

Sending multiple requests through a common connection involves multiple calls to the “MB_CLIENT” function block, but requires using the same background data block, connection ID number, IP address, and port number, modifying modes, addresses, lengths, pointers, etc. as needed.

At any time, only one MB_CLIENT request can be active. After one request is completed, the next request can begin execution, in a polling manner.

Method 2 Special Case: MODBUS TCP clients accessing multiple MODBUS RTU slaves after a gateway.

This method is suitable for the CPU to access multiple Modbus RTU slave data through a Modbus TCP to Modbus RTU gateway. The CPU only establishes one Modbus TCP connection with the gateway to access data from multiple stations.

Sending multiple requests through a common connection involves multiple calls to the “MB_CLIENT” function block, but requires using the same background data block, connection ID number, IP address, and port number. The parameter “MB_Unit_ID” uses different values, modifying modes, addresses, lengths, pointers, etc. as needed.

At any time, only one MB_CLIENT request can be active. After one request is completed, the next request can begin execution, in a polling manner.

Method 2 Polling Operation

1. Hardware and Software Requirements

The use of the MB_Client instruction requires: software version V11 SP1 or above, CPU firmware version V1.02 or above.

2. Testing Conditions

Hardware: CPU 1214C(DC/DC/DC) V4.5, Computer (ModSim32 debugging software);

Software: TIA PORTAL STEP 7 V17

Experiment: S7-1200 acts as a Modbus TCP client, first reading 2 words starting from address 40001 using function code 3, then writing 2 words starting from address 40003 using function code 16.

3. Configuration

1. The IP address of S7-1200 is 192.168.1.42, as shown in Figure 1.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 1. IP Address Setting

2. ModSim32 Debugging Software Settings

The server uses ModSim32 software for testing. First, set the testing computer’s IP address to 192.168.1.201, as shown in Figure 2.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 2. Computer’s IP Address

Open ModSim32 software, then through “File”>”New”, create settings for address area 03, starting address 40001, length 4, device ID 255, etc., as shown in Figure 3.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 3. ModSim32 Debugging Software Slave Settings

Select “Modbus/TCP Svr” in “Connection”, and set the local server port number to 502, as shown in Figure 4.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 4. Connection Settings

4. Programming

1. Create a new FB

As shown in Figure 5, create a new FB named Comm, and add parameters in static variables.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 5. Setting FB Parameters

Refer to Table 1 for the meaning of parameters:

Table 1. Meaning of FB Interface Parameters

Parameter Meaning
statStep Step Number
instClient MB_CLIENT Multiple Backgrounds
statReq Actual parameter of MB_CLIENT, its value will be modified in the program when needed
statMode
statAddr
statLen
statPtr
statConn Connection parameters, specific meanings refer to the link document
statRead Data read
statWrite Data to be written

2. Write polling function

As shown in Figure 6, the example uses the CASE OF method in SCL to write the polling program. The CASE OF instruction in SCL can refer to the link: document.

The idea is as follows:

Step 0: Wait for the connection to be established. When the connection is established, jump to Step 1.

Step 1: Set read parameters, trigger reading. When reading is normal, store the read data in the specified data area, then jump to Step 2. If there is a fault, jump to Step 100.

Step 2: Set write parameters, send the data to be written to the specified area, trigger writing. When writing is normal, jump to Step 1. If there is a fault, jump to Step 100.

Step 100: When the fault disappears, jump to Step 0.

If more read and write needs arise, more steps can be established.

The source file compressed package link for the following program: source file. After downloading and decompressing the package, the file “Comm.scl” is obtained, which can be imported into the TIA Portal project for use. For the method of importing source files, refer to the link: document.

The following example is for reference only. Readers who wish to adopt it do so at their own risk.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 6. Example Program

3. Call this FB in the main program, as shown in Figure 7.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 7. Calling Program

5. Communication Testing

Monitor the FB background data block with Modsim32 software, as shown in Figure 8.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 8. Test Results

S7-1200 MODBUS TCP Communication Multi-Request HandlingNote:

  1. If more read and write tasks are needed, they can be added according to the example.

  2. ModSim32 is a third-party software for testing Modbus RTU slaves and Modbus TCP servers, which can be downloaded from the internet.

Method 2 Special Case Polling Operation

1. Hardware and Software Requirements

The use of the MB_Client instruction requires: software version V11 SP1 or above, CPU firmware version V1.02 or above.

2. Testing Conditions

Hardware: CPU 1214C(DC/DC/DC) V4.5, Computer (ModSim32 debugging software);

Software: TIA PORTAL STEP 7 V17

Experiment: S7-1200 acts as a Modbus TCP client, reading 2 words starting from address 40001 with ID 1 using function code 3 and then reading 2 words starting from address 40003 with ID 2.

This experiment simulates reading data from multiple Modbus RTU slaves after a Modbus TCP to Modbus RTU gateway.

3. Configuration

1. CPU hardware configuration refers to the settings in Figure 1.

2. Computer IP address settings refer to the settings in Figure 2.

3. ModSim32 creates 2 files through “File”>”New”, setting address area 03, starting address 40001, length 2, and device IDs of 1 and 2, as shown in Figure 9. Then refer to Figure 4 for connection settings.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 9. ModSim32 Settings

4. Programming

1. Create a new FB

As shown in Figure 10, create a new FB named CommUnitId, and add parameters in static variables.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 10. Setting FB Parameters

Refer to Table 2 for the meaning of parameters:

Table 2. Meaning of FB Interface Parameters

Parameter Meaning
statStep Step Number
instClient MB_CLIENT Multiple Backgrounds
statReq Actual parameter of MB_CLIENT, its value will be modified in the program when needed
statMode
statAddr
statLen
statPtr
statConn Connection parameters, specific meanings refer to the link document
statRead1 Data read from station 1
statRead2 Data read from station 2

2. Write polling function

As shown in Figure 11, the example uses the CASE OF method in SCL to write the polling program. The CASE OF instruction in SCL can refer to the link: document.

The idea is as follows:

Step 0: Wait for the connection to be established. When the connection is established, jump to Step 1.

Step 1: Set read parameters, set MB_Unit_ID = 1, trigger reading. When reading is normal, store the read data in the specified data area, then jump to Step 2. If there is a fault, jump to Step 2. If the connection is interrupted, jump to Step 0.

Step 2: Set read parameters, set MB_Unit_ID = 2, trigger reading. When reading is normal, store the read data in the specified data area, then jump to Step 1. If there is a fault, jump to Step 1. If the connection is interrupted, jump to Step 0.

If more read and write needs arise, more steps can be established.

The source file compressed package link for the following program: source file. After downloading and decompressing the package, the file “CommUnitId.scl” is obtained, which can be imported into the TIA Portal project for use. For the method of importing source files, refer to the link: document.

The following example is for reference only. Readers who wish to adopt it do so at their own risk.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 11. Example Program

3. Call this FB in the main program, as shown in Figure 12.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 12. Calling Program

5. Communication Testing

The communication results are shown in Figure 13.

S7-1200 MODBUS TCP Communication Multi-Request Handling

Figure 13. Test Results

Leave a Comment