OPC With Examples
OPC With Examples
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?
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
OPC DA Servers
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.
https://www.matrikonopc.com/products/opc-drivers/opc-simulation-server.aspx
https://www.halvorsen.blog
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”:
NI OPC Servers
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
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;
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.WriteValue(temperature);
}
catch (TimeoutExcepTon)
{
MessageBox.Show("The read has Tmed out.", "Timeout");
return;
}
}
https://www.halvorsen.blog
OPC UA
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”
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
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
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
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";
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";
client.Disconnect();
}
Improved Example
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog