Connect LabVIEW To Any PLC With Modbus
Connect LabVIEW To Any PLC With Modbus
Overview
NI LabVIEW software can communicate with a programmable logic controller (PLC) in a variety of ways. Modbus is a serial
communication protocol published by Modicon in 1979 to communicate with PLC, and was then extended to the TCP protocol.
Modbus became one of the de facto standard communications protocols in the industry because its availability. In this tutorial,
learn how to use LabVIEW to communicate with a networked PLC using Modbus.
The LabVIEW Datalogging and Supervisory Control (DSC) Module is used in this tutorial. This module includes tools for logging
data to a networked historical database, real-time and historical trending, managing alarms and events, networking LabVIEW
Real-Time targets and Modbus devices into one complete system, and adding security to user interfaces. With these features,
LabVIEW becomes a powerful HMI/SCADA package for industrial control applications.
Requirements
Windows 7/Vista/XP/Server 2003 R2/Server 2008 R2
NI LabVIEW Full Development System and LabVIEW DSC
Table of Contents
In this section, you will create a LabVIEW interface to the Modbus addresses called an I/O Server. The I/O Server automatically
updates LabVIEW with the current values.
1. In the Getting Started window of LabVIEW, click File»New Project. This opens a new LabVIEW Project.
2. If the Context Help window is not visible, press Ctrl+H to display the window. Keep this window open for helpful information
about items under your cursor.
3. In the LabVIEW Project window, right-click My Computer and select New»I/O Server, as shown in Figure 1.
1/8 www.ni.com
Figure 2: Configuring Modbus Master I/O Server
1. Enter the IP Address of your target Modbus Slave in the IP address field. If you don’t know how to determine a computer’s IP
address refer this web page: How to Find IP Information. In this example we are running the Modbus Slave on the same
computer as the master, so we use the localhost IP address, 127.0.0.1.
2. Select OK. A library is automatically created in your Project Explorer window to manage the I/O Server.
3. Save the project as ModbusDemoProject and the library as ModbusDemoLibrary by selecting File»Save All from the project
explorer window.
Bind Shared Variables to Modbus Addresses Through the I/O Server
In this section, you will learn to create shared variables bound to the Modbus addresses, giving you native access in LabVIEW to
PLC data. With the shared variable you can share data across LabVIEW applications on a single computer or across the network.
1. Create new shared variables that are bound to the Modbus addresses.
1. Right-click the newly created ModbusDemoLibrary and select Create Bound Variables…
2. In the Create Bound Variables window, expand the tree down to the Modbus1 in order to select the desired address range.
3. Then select 000001-065535, and click on Add Range. Leave the default of Data Item as 000001 and Number of items as
1. Hit OK.
2/8 www.ni.com
Writing to Modbus Addresses in LabVIEW
1. From the Project Explorer, right-click My Computer and select New»VI. This creates a new virtual instrument or VI. A VI is used
to create a user interface and executable graphical code.
2. By default, you see the Front Panel, which is the user interface of the VI. LabVIEW has many built-in UI components, such as
graphs, charts, dials, and so on, which you can use to build a powerful, intuitive UI. Select View»Controls Palette or right-click
anywhere on the Front Panel to bring up the Controls palette. Mouse over the various categories to explore the UI components
in LabVIEW.
3. Select a push button from the Controls palette by selecting Express»Button & Switches»Push Button, and place it on the
Front Panel, as shown in Figure 4 and 5.
4/8 www.ni.com
Figure 9: Placing a While Loop around the Shared Variable and Control
The while loop causes the code within it to execute continuously until stopped by the user or additional logic in the VI.
1. As the while loop is now, it will run as fast as possible. A Wait Until Next ms Multiple VI should be added to the loop so that
the loop will execute every set amount of seconds, in this case 100ms.
5/8 www.ni.com
Figure 12: Completed Front Panel - Controlling a Modbus Coil
1. Congratulations! You successfully accessed PLC data in your LabVIEW application, so you can incorporate powerful analysis
and control functions in your solution.
Viewing Shared Variables with Distributed System Manager
1. From the Project Explorer window, select Tools»Distributed System Manager. This opens a window that you can use to
manage your shared variables in various ways (view, deploy, undeploy, etc.).
2. In the Tree pane of the Variable Manager, expand the localhost item under the My Systems category. Locate and expand the
ModbusDemoLibrary library, and select Coil1 to display Auto View on the right hand side of the window
3. You can now toggle the Boolean push button your front panel and see the result in Distributed System Manager like in Figure
13.
In the first section of the document we created a Modbus Master that writes a Boolean value to a Modbus coil. This coil value is
being stored in a Shared Variable that we then were able to see in the Distributed System Manager. In this next section we will
show how a Modbus Slave can read the coil value that the Master is updating. The Modbus Master we created in part one is
6/8 www.ni.com
show how a Modbus Slave can read the coil value that the Master is updating. The Modbus Master we created in part one is
looking for a Modbus Slave at IP address 127.0.0.1 (localhost). So we need to create a Modbus slave on your local machine if we
want to talk via Modbus to our Modbus Master.
1. In LabVIEW open the NI Example Finder by clicking on Help»Find Examples.
2. In the NI Example Finder browse to Toolkits and Modules» Datalogging and Supervisory Control» I/O Servers double click
on Modbus Simulator.lvproj. See Figure 16.
Figure 15: Modbus Simulator running and displaying Coil1's true value
1. Click Quit on the Modbus Simulator, and then press Ctrl+E to switch to the block diagram.
2. On the block diagram notice that Modbus address 000001 is being read with a Shared Variable. This Shared Variable is
connected to the Modbus Master’s Coil1 value.
7/8 www.ni.com
Figure 16: Modbus Simulator Block Diagram
Extra Challenge
To improve your understanding of the relationship between Modbus Masters and Slaves, a useful challenge would to be modifying
your Modbus Master program to read and control the rest of the values in the Modbus Simulator. You will need to add bound
Shared Variables to the Modbus I/O Server in the ModDemoProject.
Related Links
8/8 www.ni.com