0% found this document useful (0 votes)
5 views82 pages

OPC With Examples

The document provides an overview of OPC (OLE for Process Control), detailing its standards such as OPC DA, OPC UA, and their respective programming tools and servers. It includes practical examples using various software like LabVIEW, MATLAB, and Visual Studio, along with free simulation tools for testing OPC communication. The document highlights the advantages of OPC, including cross-platform compatibility with OPC UA, making it easier to integrate devices from different manufacturers.

Uploaded by

eilishbadguy30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views82 pages

OPC With Examples

The document provides an overview of OPC (OLE for Process Control), detailing its standards such as OPC DA, OPC UA, and their respective programming tools and servers. It includes practical examples using various software like LabVIEW, MATLAB, and Visual Studio, along with free simulation tools for testing OPC communication. The document highlights the advantages of OPC, including cross-platform compatibility with OPC UA, making it easier to integrate devices from different manufacturers.

Uploaded by

eilishbadguy30
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

https://www.halvorsen.

blog

OPC
with Practical Examples
Hans-Pe(er Halvorsen
Contents
• What is OPC?
• OPC DA
– OPC DA Servers
• MatrikonOPC Simulation Server
• “OPC Server Simulators” from Integration Objects
• NI OPC Servers
– OPC DA Programming Tools
• LabVIEW + DataSocket
• MATLAB + Industrial Communication Toolbox
• Visual Studio/C# + Measurement Studio
• OPC UA
– OPC UA Demo/Test Software
• “OPC UA Server Simulator” from Integration Objects
• “OPC UA Client” from Integration Objects
– OPC UA Programming Tools
• LabVIEW + LabVIEW OPC UA Toolkit
• MATLAB + Industrial Communication Toolbox
• Visual Studio/C# + “OPC UA .NET SDK“ from Traeger
Introduction
• In this Tutorial we give an overview
of OPC with some Practical Examples
• We use different OPC Software and
different types of Programming
Languages and Tools
h(ps://www.halvorsen.blog

What is OPC?

Hans-Petter Halvorsen Table of Contents


What is OPC?
• OPC is a standard that defines the communication of data
between devices from different manufactures
• OPC requires an OPC Server that communicates with one or
more OPC Clients
• OPC allows “plug-and-play”, gives benefits as reduces
installation time and the opportunity to choose products
from different manufactures
• We have different OPC standards:
– “Real-time” data (OPC DA),
– Historical data (OPC HDA)
– Alarm & Events data (OPC A&E)
– etc. https://opcfoundation.org/about/what-is-opc/
OPC Server and Clients
D ata OPC Client
Write
d/
Rea

Read/Write Data
OPC Server OPC Client
Read
/ Writ
Data Storage eD ata

OPC Client
Typical OPC Scenario
PLC, PAC, DCS, SCADA
Process Data
OPC-Server
Driver
Actuators Sensors
Data Acquisition
Process Network

OPC-Client
OPC-Client
OPC Specifications
“Classic” OPC “Next Generation” OPC

OPC DA
OPC HDA OPC UA

OPC A&E

https://opcfoundation.org/about/opc-technologies/opc-classic/
Next GeneraAon OPC
COM/DCOM XML, HTTP, SOAP
OPC Classic Next Generation OPC OPC UA
All specifications
Sp OPC DA Windows only Cross-platform
ec OPC HDA Windows, Linux, Mac,
collected in one (DA,
ific Embedded, VxWorks
ati OPC A&E HDA, A&E)
on
s Protocols: “UA Binary” or “UA XML”

OPC DA Server Simpler!!


OPC Client OPC UA Client
OPC HDA Server OPC UA Server
(DCOM)
OPC A&E Server
OPC UA
• UA – Unified Architecture
• The Next Generation OPC
• Cross Platform. “Classic” OPC works only for
Windows
• Based on Modern Software/Network Architecture
(No COM/DCOM problems!)
• It makes it easier to transmit and receive data in a
modern data network/Internet
https://opcfoundation.org/about/opc-technologies/opc-ua/
Classic OPC vs. OPC UA
Classic OPC (DCOM) OPC UA
Windows The server (or clients) can
be an embedded system,
OPC Client LINUX, Windows, etc. OPC UA Client
Windows Windows

OPC Server OPC Client OPC UA Client


OPC UA Server
Windows

OPC Client OPC UA Client


Classic OPC requires a Microsoft Windows operating system to implement COM/DCOM server functionality. By utilizing SOA and Web Services,
OPC UA is a platform-independent system that eliminates the previous dependency on a Windows operating system. By utilizing SOAP/XML
over HTTP, OPC UA can deploy on a variety of embedded systems regardless of whether the system is a general-purpose operating system, such
as Windows, or a deterministic real-time operating system.
http://www.ni.com/white-paper/13843/en/
https://www.halvorsen.blog

OPC DA

Hans-Petter Halvorsen Table of Contents


https://www.halvorsen.blog

OPC DA Servers

Hans-Petter Halvorsen Table of Contents


OPC DA Servers
• “MatrikonOPC Simulation Server” by
Matrikon
• “OPC Server Simulators” by
Integration Objects There exists hundreds of

• “NI OPC Servers” by NI but those menWoned here


different OPC DA Servers,

can be used for free for


demo and test purposes
https://www.halvorsen.blog

MatrikonOPC
Simulation Server
Hans-Petter Halvorsen Table of Contents
MatrikonOPC Simulation Server
“MatrikonOPC Simulation
Server” is free and can be
used for testing and
development of OPC DA
solutions.

In the software package


there is an OPC Test Client
called “MatrikonOPC
Explorer” which you can
use to test communication
with the OPC DA Server

https://www.matrikonopc.com/products/opc-drivers/opc-simulation-server.aspx
https://www.halvorsen.blog

“OPC Server Simulators”


by Integration Objects

Hans-Pe(er Halvorsen Table of Contents


“OPC Server Simulators”
“OPC Server Simulators“ by Integration
Objects are free OPC server test tools.
They allow end-users, developers and
integrators to conduct tests with any
third party OPC client software.
The package consists of an OPC DA/HDA
Server and an OPC A&E Server.

https://integrationobjects.com/sioth-opc/sioth-opc-servers/opc-server-simulators/
“OPC Server Simulators”
In the “OPC Server Simulators” software package there is a OPC server called
“Advanced OPC DA HDA Server Simulator”
Here we have connected to the server using the
“MatrikonOPC Explorer”:

The demo version runs for 48 hours


before you need to restart it
https://www.halvorsen.blog

NI OPC Servers

Hans-Petter Halvorsen Table of Contents


NI OPC Servers
A Demo version of “NI OPC Servers” is
included with “LabVIEW DSC Module” or
“LabVIEW Real-Time module” (so you may
already have it installed on your PC).
It can also be downloaded separately.

https://www.ni.com/en-no/support/downloads/software-products/download.opc-servers.html
https://www.halvorsen.blog

OPC DA
Programming Tools
Hans-Petter Halvorsen Table of Contents
OPC DA Programming Tools
Software and Programming Tools that can
be used for communicating with OPC DA
Servers:
• LabVIEW + DataSocket
• MATLAB + Industrial Communication
Toolbox (supports both OPC DA and UA)
• Visual Studio/C# + Measurement Studio
Many other alternatives exists
https://www.halvorsen.blog

LabVIEW + DataSocket

Hans-Petter Halvorsen Table of Contents


OPC DA in LabVIEW
You can use LabVIEW as an OPC client by connecting to an OPC server through a
DataSocket connection.

The DataSocket pale_e in LabVIEW:


Write Data to OPC

Read Data from OPC

Browse OPC Servers


and OPC Items
Open Connection
Close Connection
to OPC Server
to OPC Server
Note! Make sure to use LabVIEW 32bit version (even if you have 64bit operating system)
because the DataSocket feature is only supported by the 32bit version of LabVIEW.
Write Data to OPC DA Server
Write Data to OPC DA Server
Read Data from OPC DA Server
Read Data from OPC DA Server
https://www.halvorsen.blog

MATLAB + Industrial
Communication Toolbox
Hans-Petter Halvorsen Table of Contents
OPC with MATLAB
• In order to use OPC with MATLAB you can use the
“Industrial Communication Toolbox”.
• The “Industrial Communication Toolbox” supports the
following Protocols:
– OPC, both OPC DA and OPC UA (previously “OPC Toolbox”)
– MQTT
– Modbus
• Note! “Industrial Communication Toolbox” is a new
Toolbox that is included in “MATLAB R2022a” and
newer versions
Industrial Communication Toolbox
MATLAB and the Industrial
Communication Toolbox are
made by MathWorks

https://mathworks.com

https://mathworks.com/products/industrial-communication.html
% Connect to OPC Server
da = opcda('localhost', 'Matrikon.OPC.Simulation.1');
connect(da);
MATLAB OPC DA
% Create Group
grp = addgroup(da, 'DemoGroup'); This Example makes it
possible to select one
%Add Tags or more OPC Tag from a
ItmList = browsenamespace(da); pop-up window
itm = additem(grp, ItmList);

% Retrieve Data
data = read(grp);
opcdata = data.Value

%Clean Up
disconnect(da)
delete(da)
https://www.halvorsen.blog

Visual Studio/C#
+ Measurement Studio
Hans-Pe(er Halvorsen Table of Contents
Measurement Studio
• Measurement Studio (MS) is an
Add-on package to Visual Studio
created by NI (previously “National
Instruments”)
• Same vendor as LabVIEW
• Makes it possible to communicate
with an OPC DA Server from Visual
Studio Code
• Uses the DataSocket Library (same
as in LabVIEW)
NI Distributed System Manager
In order to configure
the OPC Item to be
used with
Measurement Studio,
we need to use NI
Distributed System
Manager
MS OPC Example
using NationalInstruments.NetworkVariable;

private NetworkVariableReader<double> _reader;


private const string NetworkVariableLocation = @"\\localhost\OPCProcess\Temperature";

public Form1()
{
InitializeComponent();
ConnectOPCServer();
}
MS OPC Read Example
private void ConnectOPCServer()
{
_reader = new NetworkVariableReader<double>(NetworkVariableLocation);

_reader.Connect();
private void btnGetData_Click(object sender, EventArgs e)
txtStatus.Text = _reader.ConnectionStatus.ToString();
{
}
NetworkVariableData<double> opcdata = null;
try
{
opcdata = _reader.ReadData();
txtOpcData.Text = opcdata.GetValue().ToString();
}
catch (TimeoutException)
{
MessageBox.Show("The read has timed out.", "Timeout");
return;
}
}
MS OPC Write Example
private void ConnectOPCServer()
{
_writer = new NetworkVariableWriter<double>(NetworkVariableLocation);

_writer.Connect(); private void btnWriteData_Click(object sender, EventArgs


{
txtStatus.Text = _writer.ConnectionStatus.ToString(); double temperature;
} try
{
temperature = Convert.ToDouble(txtOpcData.Text);

_writer.WriteValue(temperature);
}
catch (TimeoutExcepTon)
{
MessageBox.Show("The read has Tmed out.", "Timeout");
return;
}
}
https://www.halvorsen.blog

OPC UA

Hans-Petter Halvorsen Table of Contents


OPC UA Servers
• “OPC UA Server Simulator”
– “OPC UA Server Simulator” from “Integration
Objects” is an OPC UA Demo/Test Server
which you can download and use for free
• “LabVIEW OPC UA Server”
– With “LabVIEW OPC UA Toolkit” you can
create your own OPC UA Server
• Lots of other alternatives …
h(ps://www.halvorsen.blog

OPC UA
Demo/Test Software
Hans-Petter Halvorsen Table of Contents
OPC UA Demo/Test Software
• OPC UA Server
– E.g., “OPC UA Server Simulator” from
“Integration Objects”, which is an OPC UA
Demo/Test Server which you can download and
use for free
• OPC UA Client
– E.g., “OPC UA Client” from “Integration
Objects”, which is a free client tool that
supports the main OPC Unified Architecture
information models.
https://www.halvorsen.blog

OPC UA Server
Simulator
Hans-Petter Halvorsen Table of Contents
OPC UA Server Simulator

https://integrationobjects.com/sioth-opc/sioth-opc-unified-architecture/opc-ua-server-simulator/
OPC UA Server Simulator
For the OPC UA Part we
will use the “OPC UA
Server Simulator”, which is
an OPC UA Demo/Test
Server which you can
download and use for free
OPC UA Server Simulator
The “OPC UA Server Simulator” uses 2 CSV simulation files:
• “AddressSpace.csv” used to build the address space of
the OPC UA Server.
• “ValueSpace.csv” used to simulate the data values of
the OPC UA items.
• Those two files are located at the following path:
X:\Program Files (x86)\Integration Objects\Integration
Objects' OPC UA Server Simulator\OPC UA Server
Simulator\DATA
xxx
https://www.halvorsen.blog

“OPC UA Client”

Hans-Pe(er Halvorsen Table of Contents


“OPC UA Client”
• “OPC UA Client” is a free OPC client tool that
supports the main OPC Unified Architecture
information models.
• These models are Data Access, Alarms &
Conditions, and Historical Data Access
• Handy to use to test OPC communication
• https://integrationobjects.com/sioth-opc/sioth-
opc-unified-architecture/opc-ua-client/
“OPC UA Client”

https://integrationobjects.com/sioth-opc/sioth-opc-unified-architecture/opc-ua-client/
“OPC UA Client”
“OPC UA Client”
https://www.halvorsen.blog

OPC UA
Programming Tools
Hans-Petter Halvorsen Table of Contents
OPC UA Programming Tools
Software and Programming Tools that can
be used for communicating with OPC UA
Servers:
• LabVIEW + LabVIEW OPC UA Toolkit
• MATLAB + Industrial Communication
Toolkit (supports both OPC DA and UA)
• Visual Studio/C# + “OPC UA .NET SDK”
Many other alternaWves exists
https://www.halvorsen.blog

LabVIEW OPC UA
Toolkit
Hans-Petter Halvorsen Table of Contents
LabVIEW OPC UA Toolkit
LabVIEW and the
LabVIEW OPC UA
Toolkit are made by NI
https://www.ni.com

https://www.ni.com/en-no/support/downloads/software-products/download.labview-opc-ua-toolkit.html
OPC UA in LabVIEW

Write Data to OPC UA Server OPC UA Server Read Data from OPC UA Server

LabVIEW Application #1
OPC UA Client OPC UA Client

LabVIEW Application #2 LabVIEW Application #3


OPC UA Server Example in LabVIEW
OPC UA Client Write Data

Using OPC UA Toolkit with LabVIEW 2017 or newer


OPC UA Client Read Data
Block Diagram
https://www.halvorsen.blog

MATLAB Industrial
Communica?on Toolbox
Hans-Petter Halvorsen Table of Contents
OPC with MATLAB
• In order to use OPC with MATLAB you can use the
“Industrial Communication Toolbox”.
• The “Industrial Communication Toolbox” supports the
following Protocols:
– OPC, both OPC DA and OPC UA (previously “OPC Toolbox”)
– MQTT
– Modbus
• Note! “Industrial Communication Toolbox” is a new
Toolbox that is included in “MATLAB R2022a” and
newer versions
Industrial Communication Toolbox
• The Industrial Communication Toolbox
supports:
– OPC DA
– OPC HDA
– OPC UA
• Resources:
– https://mathworks.com/products/industrial-communication.html
– https://mathworks.com/help/icomm/data-reading-and-writing.html
OPC UA – Read Data
1. Locate Your OPC UA Server
serverList = opcuaserverinfo('localhost’)
2. Create an OPC UA Client
uaClient = opcua('localhost’, port)
3. Connect to the Server
connect(uaClient)
4. Browse OPC UA Server Namespace
serverNodes = browseNamespace(uaClient)
5. Read Current Values from the OPC UA Server
[val,ts,qual] = readValue(uaClient,serverNodes)
6. Disconnect
disconnect(uaClient)
OPC UA – Read Data
clear, clc

uaClient = opcua('localhost', 62640)

connect(uaClient)

serverNodes = browseNamespace(uaClient)

[val,ts,qual] = readValue(uaClient,serverNodes)

disconnect(uaClient);
OPC UA – Read Data 2
clear, clc
Read Data
uaClient = opcua('localhost', 62640)

connect(uaClient)
topNodes = uaClient.Namespace
serverChildren = topNodes(4).Children
findNode = findNodeByName(topNodes,'Tag7', '-once')

opcNode = opcuanode(2,'Tag7',uaClient)
[value,timestamp,quality] = readValue(uaClient,opcNode)
Here you don’t need to select the Tag from the
disconnect(uaClient); “Browse Name Space” window every time
OPC UA – Write Data
1. Locate Your OPC UA Server
serverList = opcuaserverinfo('localhost’)
2. Create an OPC UA Client
uaClient = opcua('localhost’, port)
3. Connect to the Server
connect(uaClient)
4. Browse OPC UA Server Namespace
serverNodes = browseNamespace(uaClient)
5. Write Current Values to the OPC UA Server
newValue = 22.5
writeValue(uaClient, serverNodes, newValue);
6. Disconnect
disconnect(uaClient)
OPC UA – Write Data
clear, clc

uaClient = opcua('localhost', 62640)

connect(uaClient)

serverNodes = browseNamespace(uaClient)

newValue = 21.7;
writeValue(uaClient, serverNodes, newValue);

[value,timestamp,quality] = readValue(uaClient,serverNodes)

disconnect(uaClient);
https://www.halvorsen.blog

Visual Studio/C# +
“OPC UA .NET SDK”
Hans-Petter Halvorsen Table of Contents
OPC UA with Visual Studio/C#
• Lots of Packages and Libraries do exist for
creating both OPC UA Clients and OPC UA
Servers in Visual Studio/C#
• Most of them are payment based
• Many of those can be evaluated for a trial
period or used forever with some restrictions
• In this Tutorial, “OPC UA .NET SDK” will be used
– It can be used in “Evaluation Mode” for Test and
Demo purposes
“OPC UA .NET SDK”
• The “OPC UA .NET SDK” comes
with an evaluation license which
can be used unlimited where each
application runs for 30 minutes
• It comes in a NuGet Package you
can install and use in your Visual
Studio Project
• https://opcfoundation.org/produc
ts/view/opc-ua-net-sdk-for-client-
and-server
NuGet Package
Visual Studio Project
System Overview
OPC UA Write
private void btnOpcWrite_Click(object sender, EventArgs e)
{
string opcUrl = "opc.tcp://localhost:62640/";
var tagName = "ns=2;s=Tag7";

var client = new OpcClient(opcUrl);


client.Connect();

double temperature;
temperature = Convert.ToDouble(txtOpcDataWrite.Text);

client.WriteNode(tagName, temperature);

client.Disconnect();
}
OPC UA Read
private void btnOpcRead_Click(object sender, EventArgs e)
{
string opcUrl = "opc.tcp://localhost:62640/";
var tagName = "ns=2;s=Tag7";

var client = new OpcClient(opcUrl);


client.Connect();

var temperature = client.ReadNode(tagName);


txtOpcDataRead.Text = temperature.ToString();

client.Disconnect();
}
Improved Example

OPC UA Write C# App OPC UA Read C# App


Summary
• What is OPC?
• OPC DA For all these Programming
– OPC DA Servers Languages and Packages, I have
• MatrikonOPC Simulation Server made separate Tutorials where I
• “OPC Server Simulators” from Integration Objects go through the development of
• NI OPC Servers
the Applications and the Code in
– OPC DA Programming Tools
• LabVIEW + DataSocket more details
• MATLAB + Industrial Communication Toolbox
• Visual Studio/C# + Measurement Studio
• OPC UA
– OPC UA Demo/Test Software
• “OPC UA Server Simulator” from Integration Objects
• “OPC UA Client” from Integration Objects
– OPC UA Programming Tools
• LabVIEW + LabVIEW OPC UA Toolkit
• MATLAB + Industrial Communication Toolbox
• Visual Studio/C# + “OPC UA .NET SDK“ from Traeger
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no

E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy