You are on page 1of 12

Connect to Any PLC

Approximate Time
You can complete this exercise in approximately 40 minutes.

Background
LabVIEW programs are called virtual instruments, or VIs, because their appearance and
operation imitate physical instruments, such as oscilloscopes and multimeters. LabVIEW
contains a comprehensive set of tools for acquiring, analyzing, displaying, and storing data, as
well as tools to help you troubleshoot code you write.

In LabVIEW, you build a user interface, or front panel, with controls and indicators. Controls are
knobs, push buttons, dials, and other input mechanisms. Indicators are graphs, LEDs, and other
output displays. After you build the user interface, you add code using VIs and structures to
control the front panel objects. The block diagram contains this code.

Overview
LabVIEW can communicate with any PLC in a variety of ways. In this exercise you will learn how
to use LabVIEW to communicate with a networked PLC using OPC.

OLE for Process Control (OPC) defines the standard for communicating real-time plant data
between control devices and human machine interfaces (HMIs). OPC Servers are available for
virtually all PLC and Programmable Automation Controllers (PAC).

In this exercise the LabVIEW Datalogging and Supervisory Control (DSC) Module is used. 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
OPC 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.

1
Completed Exercise

2
Step-by-Step Instructions

1. View Existing PLC Tags with NI OPC Servers

a. Select Start » Programs » National


Instruments » NI OPC Servers » NI OPC
Servers. This Will Launch NI OPC
Servers. With NI OPC Servers, you can
create, configure and view tags that are
associated to your PLCs.

b. NI OPC Servers should launch with a PLC simulation project already loaded.

If this simulation project is not already loaded, in NI OPC Servers, select File »
Open… and browse to C:\Program Files\National Instruments\Shared\NI OPC
Servers\Projects\simdemo.opf.

c. View the Sine tags by expanding


Channel_0_User_Defined and selecting Sine.
The tags will populate in the right-most
window. These tags, which are bound to
registers on the PLCs, will be read by LabVIEW.

3
2. View the data from the PLCs’ OPC tags
a. In NI OPC Servers, select Tools » Launch
OPC Quick Client. This will launch the
OPC Quick Client, which allows you to
view the OPC tag data.
b. Expand the National
Instruments.NIOPCServers folder and
select Channel_0_User_Defined.Sine.
This selects the device to monitor.
c. Notice that all the Sine tags populate in
the right most window and are updating
with simulated sine data.

For a list of supported devices/drivers for NI OPC, visit ni.com/opc.

3. Connect LabVIEW to OPC Tags by Creating an I/O Server


In this section you will create a LabVIEW interface to the OPC tags called an I/O Server. The I/O
Server will automatically update LabVIEW with the current tag values at a rate you specify.

a. In the Getting Started window of LabVIEW,


click File » New Project. This will open a new
project explorer window.

b. 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.

4
c. In the LabVIEW Project window, right-click on
My Computer and select New » I/O Server.
d. Select OPC Client in the Create New I/O Server
Window and click Continue.

e. Choose National Instruments.NIOPCServers from the Registered OPC servers field


and set Update rate (ms) to be 100. This will create a connection from LabVIEW to
the OPC tags, which updates every 100ms.
f. Select OK. A library will automatically be created in your project explorer window to
manage the I/O Server.

g. Save the project as OPCDemoProject and the library as OPCDemoLibrary by selecting


File » Save All from the project explorer window.

5
4. Create Shared Variables that Connect to the OPC Tags through the
I/O Server
In this section you will first create Shared Variables, which are bound to the OPC tags, 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.

a. In the LabVIEW Project window, right-click


on My Computer and select New » Library.
This creates a new library for the Shared
Variables, which are used to connect to the
PLCs’ OPC tags.

b. Right click on the newly created library and select Create Bound Variables…
c. In the Create Bound Variables window, select the OPC tags to bind the Shared
Variables to by browsing down to the simulated sine data from the OPC server.
d. Select all the sine items and click Add and OK.

e. In the Multiple Variable Editor, select Done. This will add the new Shared Variables
to the library that was created earlier.

6
The LabVIEW DSC Module enhances Shared Variables by adding the ability to log
data, alarms and events directly to a database without ever writing a LabVIEW
application.

f. Save the new library as OPCItems.lvlib in the project explorer window by right
clicking on the library and selecting Save As.
g. Deploy the shared variables by right clicking on the OPCItems library and selecting
Deploy. This will publish the shared variables, making them available on the
network to other computers, OPC clients, and LabVIEW Real-Time PAC. You now
have access to PLC data natively in LabVIEW, through the Shared Variables.

5. Viewing Shared Variables with Variable Manager


a. From the Project Explorer, select Tools » Shared Variable » Variable Manager. This
will open a window that allows you to manage your shared variables in various ways
(i.e. view, deploy, un-deploy, etc.).
b. In the Tree pane of the Variable Manager, expand the OPCItems library, select all the
shared variables and drag them over to the Watched Variables: window. This will
display the Shared Variables, which are bound to the PLCs’ OPC tags, updating with
the simulated sine data.

7
6. Using OPC Tag Data in LabVIEW
a. From the Project Explorer, right click on 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.
b. By default, you will be looking at the front panel, which is the user interface of the
VI. 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.
c. Select a Waveform Chart from the Controls
Palette, by selecting Express » Graph
Indicators » Chart, and place it on the front
panel.

d. Press <Ctrl-E> to show the block diagram. The block diagram is where you build the
behavior of your application. Notice the icon on the block diagram, which represents
the chart on the Front Panel.
e. In the Project Explorer, expand the OPCItems library and select the Sine1 Shared
Variable.

8
f. Drag-and-drop the Sine1 Shared Variable from the Project Explorer to the Block
Diagram of the VI. The Shared Variable acts as a source of data to other terminals
on the block diagram.
g. Select View » Tools Palette or press <Shift–Right click>,
to show the Tools Palette, which contains various tools
for building the block diagram. By default the Automatic
Tool Selection tool is used, which selects the
appropriate tool, based on the location of the cursor.
h. Select the Connect Wire tool as shown below. This tool
is used to wire terminals together on the block diagram.

i. Use the Connect Wire tool to wire the Sine1


Shared Variable to the Waveform Chart, by
clicking on the Sine1 Shared Variable and then
on the Waveform Chart. Now data will flow
from the Shared Variable to the Waveform
Chart when the VI is running.

j. Select the Automatic Tool Selection tool from


the Tools Palette.

It is more effective to program a VI using the automatic tool selector because


LabVIEW automatically selects the correct tool when it is placed near a desired
object. This allows for more efficient programming compared to manually selecting
the correct tool from the Tools Palette.

9
k. Open the Functions Palette by selecting View »
Functions Palette or right-clicking anywhere on the
block diagram. The Functions Palette contains
hundreds of analysis functions, control functions,
and structures for graphical programming.
l. Select a While Loop from the Functions Palette by
navigating to Express » Execution Control » While
Loop.

m. Once you select the While Loop your cursor will appear. This allows you
to wrap a While Loop around a section of code.

n. Using the While Loop cursor, place a While Loop around the Shared Variable and
Waveform Chart, by clicking and dragging the cursor.

o. The While Loop causes the code within it to execute continuously, until stopped by
the user or additional logic in the VI.

10
p. A Timed Loop is an advanced While Loop,
which contains additional configuration
options for timing and execution control.
Convert the While Loop into a Timed Loop
by right-clicking on the While Loop and
selecting Replace with Timed Loop.

q. To configure the Timed Loop, double-click the Timed Loop input node. This opens
the Configure Timed Loop dialog box.

11
r. In the Loop Timing Attributes field, set Period to 100ms
and click OK. This configures the Timed Loop to execute
the contained code every 100ms.

s. Return to the Front Panel by selecting Window » Show Front Panel or pressing
Ctrl+E.
t. Click the Run button on the tool bar to execute the VI.

u. Click Close on the Deploy… window once the deployment completes. Once the
application begins executing you will see the Sine1 sine wave being displayed on the
Waveform Chart.

v. Congratulations! You successfully accessed PLC data in your LabVIEW application,


enabling you incorporate powerful analysis and control functions into your solution.

12

You might also like