0% found this document useful (0 votes)
14 views

OPC UA With Visual Studio and CSharp

The document provides an overview of OPC UA, a communication protocol for data exchange in industrial systems, highlighting its advantages over Classic OPC, including cross-platform compatibility and ease of use. It details tools such as the OPC UA Server Simulator and OPC UA Client for testing and education, as well as the OPC UA .NET SDK for developing applications in Visual Studio/C#. Additionally, it includes examples of writing and reading data from an OPC UA server using C#.

Uploaded by

berna
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)
14 views

OPC UA With Visual Studio and CSharp

The document provides an overview of OPC UA, a communication protocol for data exchange in industrial systems, highlighting its advantages over Classic OPC, including cross-platform compatibility and ease of use. It details tools such as the OPC UA Server Simulator and OPC UA Client for testing and education, as well as the OPC UA .NET SDK for developing applications in Visual Studio/C#. Additionally, it includes examples of writing and reading data from an OPC UA server using C#.

Uploaded by

berna
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/ 46

https://www.halvorsen.

blog

OPC UA with Visual


Studio and C#
Using “OPC UA Server Simulator” and “OPC UA .NET SDK”

Hans-Petter Halvorsen
Contents
• Introduction to OPC
– Communication Protocol for Data Exchange between Devices from different
Manufactures typically used in Industrial and Automation Systems
• OPC UA
– The Next Generation OPC. Cross-platform. Works with IoT/IIoT
• OPC UA Server Simulator
– Free OPC UA Server that can be used for Testing and Education
• “OPC UA Client” Tool
– Free OPC UA Client that can be used for Testing and Education
• OPC UA .NET SDK
– Free Evaluation license which can be used unlimited for each application but
runs only for 30 minutes before restart is required
– Visual Studio/C# Example
– Improved Example
https://www.halvorsen.blog

OPC

Hans-Petter Halvorsen Table of Contents


What is OPC?
• A standard that defines the communication of data
between devices from different manufactures
• Requires an OPC server that communicates with the OPC
clients
• OPC allows “plug-and-play”, gives benefits as reduces
installation time and the opportunity to choose products
from different manufactures
• Different standards: “Real-time” data (OPC DA), Historical
data (OPC HDA), Alarm & Event data (OPC AE), etc.
Typical OPC Scenario
PLC, PAC, DCS, SCADA
Process Data
OPC-Server
Driver
Actuators Sensors
Data Acquisition
Process Network

OPC-Client
OPC-Client
OPC Server and Client(s)
OPC Server Data Storage

Write Data Read Data

OPC Client OPC Client

Monitoring and Analysis


Sensors Measurement Data
https://www.halvorsen.blog

OPC UA

Hans-Petter Halvorsen Table of Contents


OPC UA
• UA – Unified Architecture
• The Next Generation OPC
• Cross Platform. “Classic“ OPC works only for
Windows
• Based on Modern Software/Network
Architecture (No DCOM problems!)
• It makes it easier to transmit and receive data in
a modern data network/Internet
Classic OPC vs. OPC UA
Theory

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/
OPC Specifications
“Classic” OPC “Next Generation” OPC

OPC DA
OPC HDA OPC UA

OPC A&E
... (Many others)
Next Generation OPC Theory

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
Next Generation OPC Theory

OPC Classic Server Network OPC Classic Client


Fire Fire
wa wa
ll ll
To open DCOM through firewalls demanded a large hole in the firewall!
Impossible to route over Internet!

Network/
OPC UA Server OPC UA Client
Fire Internet
wa Fire
ll wa
ll
No hole in firewall (UA XML) or just a simple needle stick (UA Binary) is necessary
Easy to route over Internet!
https://www.halvorsen.blog

OPC UA Server
Simulator
Hans-Petter Halvorsen Table of Contents
OPC UA Server Simulator
• This free OPC UA Server tool supports data access
and historical access information models of OPC UA.
• Consequently, it provides simulated real-time and
historical data.
• It is possible to configure your own tags and the data
simulation via CSV files.
• OPC UA clients can monitor real-time data and
explore history data from this simulator.
• https://opcfoundation.org/products/view/opc-ua-
server-simulator
OPC UA Server Simulator
https://opcfoundation.org

https://opcfoundation.org/products/view/opc-ua-server-simulator
https://integrationobjects.com/sioth-opc/sioth-opc-unified-architecture/opc-ua-server-simulator/
OPC UA Server Simulator
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
https://www.halvorsen.blog

“OPC UA Client” Tool

Hans-Petter Halvorsen Table of Contents


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

OPC UA with C#

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
https://www.halvorsen.blog

OPC UA .NET SDK

Hans-Petter Halvorsen Table of Contents


OPC UA .NET SDK
• The “OPC UA .NET SDK” comes
with an evaluation license which
can be used unlimited for each
application run 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
https://www.halvorsen.blog

Visual Studio/C#
Example
Hans-Petter Halvorsen Table of Contents
Visual Studio/C# Example
Visual Studio/C# Example
• Not that this is a simplified example in order
demonstrate the principle of Writing Data to an
OPC UA Server and Reading Data from an OPC
Server
• Write and Read the same OPC Tag in the same
Application makes no sense in a real scenario
• Typically, the OPC clients are distributed in a
network and the different Applications are
located on different computers in a network
NuGet Package
Visual Studio Project
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();
}
Test 1 (OPC UA Server Simulator)
Test 2 (LabVIEW OPC Server)
https://www.halvorsen.blog

Improved Example

Hans-Petter Halvorsen Table of Contents


Improved Example
We will implement some Improvements:
• Separate Applications for Write and Read
• Start and Stop Buttons
• Formatting Number of Decimals
• Property window (preventing resizing of the window):
– form1.FormBorderStyle = FormBorderStyle.FixedSingle;
– Form1.MaximizeBox = false;
• Using a Timer (Writing/Reading at specific Intervals)
• General Improvements in the Code
OPC UA Write C# App

OPC UA Read C# App


using Opc.UaFx.Client;

namespace OPCUARead
{
public partial class Form1 : Form
{
public OpcClient client = new OpcClient("opc.tcp://localhost:62640/");
OPC UA Write public Form1()
{

}
InitializeComponent();
timer1.Interval = 10000;

private void Form1_FormClosing(object sender, FormClosingEventArgs e)


{
if (client != null)
client.Disconnect();
}

private void timer1_Tick(object sender, EventArgs e)


{
double sensorValue;

sensorValue = ReadSensorData();
OpcWrite(sensorValue);
}

double ReadSensorData()
{
var rand = new Random();
int minValue = 20, maxValue = 30;
double sensorValue;

sensorValue = rand.NextDouble() * (maxValue - minValue) + minValue;


txtSensorValue.Text = sensorValue.ToString("#.##");
DateTime sensorDateTime = DateTime.Now;
txtTimeStamp.Text = sensorDateTime.ToString("yyyy-MM-dd HH:mm:ss");

return sensorValue;
}

void OpcWrite(double sensorValue)


{
string tagName = "ns=2;s=Tag7";
client.WriteNode(tagName, sensorValue);
}

private void btnStart_Click(object sender, EventArgs e)


{
client.Connect();
timer1.Start();
lblStatusMessage.Text = "Logging Started and Connected to OPC Server";
}

private void btnStop_Click(object sender, EventArgs e)


{
timer1.Stop();
if (client != null)
client.Disconnect();
lblStatusMessage.Text = "Logging Stopped and Disconnected from OPC Server";
}

}
}
using Opc.UaFx.Client;

namespace OPCUARead
{
public partial class Form1 : Form
{
OPC UA Read public OpcClient client = new OpcClient("opc.tcp://localhost:62640/");

public Form1()
{
InitializeComponent();
timer1.Interval = 2000;
}

private void timer1_Tick(object sender, EventArgs e)


{
OpcRead();
}

void OpcRead()
{
string tagName = "ns=2;s=Tag7";

Opc.UaFx.OpcValue opcData = client.ReadNode(tagName);

double temperature = (double)opcData.Value;

txtOpcValue.Text = temperature.ToString("#.##");
}

private void btnStart_Click(object sender, EventArgs e)


{
client.Connect();
timer1.Start();
lblStatusMessage.Text = "Connected to OPC Server";

private void btnStop_Click(object sender, EventArgs e)


{
timer1.Stop();
if (client != null)
client.Disconnect();
lblStatusMessage.Text = "Disconnected from OPC Server";
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)


{
if (client != null)
client.Disconnect();
}

}
}
More Improvements
We will implement some more Improvements:
• Possible to specify ServerUrl and NodeId
(TagName) from the GUI
OPC UA Write C# App

OPC UA Read C# App


Furter Work
Still, lots of improvements can be made
• Possible to change the Logging Interval from the
GUI
• Add a Chart
• Get Data from a real Sensor
• Add Units
• Etc.
• In addition, the “OPC UA .NET SDK” comes with
many features that will not be demonstrated here
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no

E-mail: [email protected]
Web: https://www.halvorsen.blog

You might also like