C# communicates with the ModbusTcp server of Siemens PLC1500 3--Building a ModbusTcp server

1. Open the simulation tool and create a PLC. Be careful not to close it after the creation is complete.

 Note that this IP address must be on the same network segment as the IP address of the Siemens virtual network card and the IP address of the virtual machine's network card

 

2. Open Portal V15, create a project, and name it Lan project

 

3. Add 1500 series CPU1513

 

4. Set the IP address and properties

 

 

 5. Add communication module MB_SERVER

Find the program block main, find the instructions on the right, find communication, find others, drag MB_SERVER to program segment 1

 

 Focus on the mb_hold_reg and connect parameters. Disconnet is false to indicate passive connection, which means that the server waits for the client to connect, instead of the server actively connecting to the client, that is to say, the client should actively connect to the server, and then the client will pass the IP address and port to connect to the server, so the connection to the server must be through the CONNECT pin. After the connection is successful, the client needs data, and the MB_HOLD_REG pin is needed to reply the data.

Now here is? ? ? , don’t worry about it, save the project, and continue with the following, because you haven’t set up other

6. Add a global data block DB2 and create a variable of data type TCON_IP_v4 to match CONNECT

The definition of each pin in the figure:

  Among them, the id and localport can be set by yourself, but the interfaceid is 64, which cannot be changed casually, because the reading device has an ID number, this parameter is implicit, and it must be consistent with the device, as shown in the figure

 As for which remoteport indicates which external IPs can connect to this server, it is all IPs by default, and generally do not need to be modified unless there are special requirements, so keep the default here, that is, allow any IP address to connect to this server. As for the ID, it can be understood as Process ID, the range is 1-4095, cannot be 0, each connection ID must be independent, in this case it is 1 (modify the initial value to 1);

DB2 compile, success

7. Add a global data block DB3 to match the pin parameter "MB_HOLD_REG" of the function block "MB_SERVER"

 Add several variables, here are 8, you can according to the actual situation

     

 Set block properties

 The compilation is successful, and you can see the offset of the variable

 8. Set MB_HOLD_REG pin parameters

 MB_HOLD_REG means to hold the register, which is the 03 function code in the Modbus protocol

Double-click the pin and fill in "P#DB3.DBX0.0 BYTE 20". Here I will focus on how to configure this pin. P means pointer. DB3 is because of the data block name I added in step 10 above. It is DB3, you have to change it according to the actual name of the data block you added, and then 0.0 BYTE 20 means from 0 to 20 bytes, why is it 20, see the picture below

Word is a word, occupying a register, occupying 2 bytes, real is two words, occupying 2 registers, occupying 4 bytes, so the address of the first variable m1-speed is 0, 1, 8 variables The bytes accounted for are:

m1-speed:0,1
m1-duaror:2,3
m1-level:4,5
m1-temp:6,7,8,9
m2-speed:10,11
m2-duaror:12,13
m2-level:14,15
m2-temp:16,17,18,19

A total of 20 bytes from 0 to 19, the byte is represented by BYTE, you must understand this meaning, so it is 0.0 BYTE 20, you can’t write indiscriminately here, if you write wrong, you can’t read the value of the variable, many people here are easy to mess up Wrong, you have to write according to the byte range of your variable , it can only be less, not more, such as 0.0 BYTE 22, which is wrong, because there are no 22 bytes, only 20 bytes, but it can be 0.0 BYTE 16, This means that only the first 7 variables are read, and the eighth variable is not read, unless you have such a requirement. Of course, you can add many variables later,

 Added 2 more variables as above, the byte range is 24 bytes (0---23), but my pointer is P#DB3.DBX0.0 BYTE 20, only the first 20 bytes will be read, that is The value of the first 8 variables, the latter 9, 10 variables will not be read, especially to understand this place, many articles do not explain clearly, it is confusing, and many people have not figured out how to write here. The meaning of the pin MB_HOLD_REG is to point to which range of bytes in which data block to respond to the MODBUSTCP request of an external program or device. This byte range is only less than or equal to, and cannot be greater than the range of the data block.

9. Set CONNECT pin parameters

The meaning of this pin refers to which data block is used to respond to the external connection to the server, because when MODBUSTCP is connected, the IP address and port of the server need to be specified, as shown in the figure

 Doesn't this "tcp_connector".Connector refer to the variable Connector in the DB2 data block?

 In the previous step 6, the data block 2 was created, and the variable Connector was created.

 10. Compile the main program block successfully

 11. Download the program to the PLC

 

 

 12. Monitor variables for DB3

13. Summary

 At this point, the PLC is built as a MODBUSTCP server, and the C# program is written later. The basic process is: create a PLC, create a project, set the device IP address, add the mb_server module, add 2 data blocks, add variables to the data block, and set pin parameters , compile and download the program to the PLC.

After the download is successful, it means that the modbustcp server has been started and is ready to wait for the client to connect and read the data value of the saved register, no additional operations are required.

Guess you like

Origin blog.csdn.net/hqwest/article/details/132388663