OPC UA With Visual Studio and CSharp
OPC UA With Visual Studio and CSharp
blog
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
OPC-Client
OPC-Client
OPC Server and Client(s)
OPC Server Data Storage
OPC UA
OPC DA
OPC HDA OPC UA
OPC A&E
... (Many others)
Next Generation OPC Theory
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 with C#
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";
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();
}
Test 1 (OPC UA Server Simulator)
Test 2 (LabVIEW OPC Server)
https://www.halvorsen.blog
Improved Example
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;
sensorValue = ReadSensorData();
OpcWrite(sensorValue);
}
double ReadSensorData()
{
var rand = new Random();
int minValue = 20, maxValue = 30;
double sensorValue;
return sensorValue;
}
}
}
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;
}
void OpcRead()
{
string tagName = "ns=2;s=Tag7";
txtOpcValue.Text = temperature.ToString("#.##");
}
}
}
More Improvements
We will implement some more Improvements:
• Possible to specify ServerUrl and NodeId
(TagName) from the GUI
OPC UA Write C# App
E-mail: [email protected]
Web: https://www.halvorsen.blog