Blog

Communicating Between a Siemens PLC and LabVIEW Using Modbus TCP

Communicating Between a Siemens PLC and LabVIEW Using Modbus TCP

Have you ever needed to integrate LabVIEW into a Siemens PLC project or vice versa? PLCs offer features such as reliability and safety, whereas LabVIEW has plug and play libraries for a variety of hardware.

The list of different benefits these two offerings is long, but what if you need both?

Modbus TCP is an excellent way to transfer data between the two programs. Modbus is an easy to deploy protocol that enables multiple devices on the same network to communicate. To set up Modbus Communication in TIA Portal, drag an “MB_Server” block to the Main[OB1]. This will be the only rung you need in Main [OB1].

program blocks

You will need to create a data block for holding registers, which contain the information you are trying to send and receive. Mine is named “dbHoldingRegisters” and I filled it with an array of random values to send to LabVIEW.

You will also need space in a data block to save configuration information to connect using Modbus. The data block should be of type TCON_IP_v4, which includes all address parameters required for establishing a connection. Details on how to populate this are listed below.

populate data blocks

  • InterfaceID: This references hardware identifier number, found in “Devices and Networks”, pictured below.
  • ID: A unique ID for the connection within the CPU ranging from 1 to 4095. If you have multiple “MB_Server” blocks, they will each need unique numbers.
  • Connection Type: 11 is for TCP connections
  • Active Established: Select false for passive connection establishment.
  • Remote Address: The default address is 0.0.0.0 (any IP address), but you can enter a specific IP address so that the server only responds to requests from this address. For example, if the IP address of my computer is 192.168.0.73, the addresses will be.
    • addr[1] = 192
    • addr[2] = 168
    • addr[3] = 0
    • addr[4] = 73
  • Remote Port: This is the incoming tunnel port number of the remote connection partner (value range: 1 to 49151) or 0 for any.
  • Local Port: This is the outgoing port number of the local connection partner (value range: 1 to 49151), 502 is the default. A Modbus client (LabVIEW) sends its messages on the port 502, and a Modbus server (PLC) receives messages on 502.

On the LabVIEW side, the code is straightforward. All VIs are taken from the Modbus Master.lvclass and are listed below. You will need to create a TCP connection to the IP address of the PLC as well as use the same local port number (502) in your “serverData” data block. Then, you can write and read registers as you choose. As always, make sure to close the connection when you are done.

In this example, the value 999 was written to address 0, and then all the holding register values were read and populated in the “register values” indicator. And Voila! The random array of values we sent from the PLC has been transmitted successfully. You are now reading and writing data from LabVIEW to a PLC.

Happy programming!

Learn more about DMC's LabVIEW Programming services and contact us with any project inquiries!

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Related Blog Posts