PLC program example 2: ModBusTCP client programming example and test method

1. Requirements description

1. When the device is used as the server, the corresponding test method needs to be given, that is, the PLC is required to be used as the client, and the device is used as the server, so it is required to write the ModBusTCP client of the PLC

2. First understand the ModBusTCP network triggering business logic of the device as the server

(1) The device needs to set the network trigger, and set the trigger command (if it is TR), and the ModBusTCP server settings as shown in the figure

(2) The PLC connects the equipment through the information shown in the figure

(3) PLC sends command TR to the device

(4) The device receives the TR command and compares it with the TR command set by the network trigger

(5) Trigger if the command is consistent

(6) The triggered result is written to the transmission address of the device, which requires the PLC to actively access and obtain

2. Official documents and explanations

1. View the official documents through the host computer software

As follows, the concept of master station and slave station is involved here, which is somewhat different from the description of server and client. The explanation is as follows: This is better explained in English. The master station MASTER also means "supervisor", and the slave station SLAVE also has " slave" means. In terms of the communication process, the master station issues a communication command, and the slave station responds.

Therefore, the PLC is the master station and the device is the slave station. Don't confuse this concept with the concept of "server and client".

 2. After understanding the concept of master station and slave station, we should find the implementation document of PLC as the master station function, as shown in the figure below

 

3. Programming according to official documents

1. Connection settings

 

2. Protocol settings

Operation path: [Tool] [Protocol Support Function] [File] [New] "Protocol Setting" screen [Edit] [Add Protocol]

This example uses 2 protocols:  03: RD Holding Registers + WR Multi Registers

 

 

 

 

 

4. Programming

 1. The above code explanation and experiment

 (1) Change the length of D403 to 20, the maximum received length is 20, the program is set as:

 (2) Change the assignment value of D402 to 10, that is, the value address of the device is the area from 10 to 20 (11 data), and the program is set as:

 (3) Correspondence of connection numbers

 

(5) Correspondence between protocol numbers

 (6) The station number of the equipment is 1, and the program setting is:

 (7) Based on the above operations, we have realized the read operation of the PLC to the device

(8) Add the WR Multi Registers protocol and send it to the PLC

(9) Code

 

 

Guess you like

Origin blog.csdn.net/weixin_43431593/article/details/128217253