Tutorial
Getting started
Set up your first client with the OPC UA .NET SDK: import the namespace, connect, and disconnect again, also for OPC Classic servers.
Tutorial
Getting started
Prerequisites
- Target framework: .NET or .NET Framework 4.7.2 or later, all supported frameworks under Target frameworks
- NuGet package:
Opc.UaFx.Client, for OPC ClassicOpc.UaFx.Advanced(Downloads) - OPC UA server: a reachable server, for first tests for example the server from the complete example of server development
- License: not required for the evaluation. Without a license key, the SDK runs with its full feature set, limited to 30 minutes per application launch (Set up the license)
Connect to an OPC UA server
1. Add a reference to the client namespace Opc.UaFx.Client:
using Opc.UaFx.Client;Imports Opc.UaFx.Client2. Create an instance of the OpcClient class with the address of the server:
var client = new OpcClient("opc.tcp://localhost:4840/");Dim client = New OpcClient("opc.tcp://localhost:4840/")3. Connect to the server and start the session:
client.Connect();client.Connect()4. Work with the server (your code):
// Your code to interact with the server.'Your code to interact with the server.5. Disconnect before the application exits:
client.Disconnect();client.Disconnect()6. With a using block, it looks like this:
using (var client = new OpcClient("opc.tcp://localhost:4840/")) {
client.Connect();
// Your code to interact with the server.
}Using client As New OpcClient("opc.tcp://localhost:4840/")
client.Connect()
'Your code to interact with the server.
End UsingHow you continue depends on whether you know the nodes and NodeIds of the server:
- Unknown: The section Explore the address space shows which nodes the server provides.
- Known: Read values or Write values
Find servers
Types usedOpcDiscoveryClientOpcApplicationDescription
If you do not know the address of a server, a discovery server (for example the Local Discovery Server on port 4840) returns the servers registered with it, along with their discovery addresses. For each server, DiscoverServers returns its name, its type, and its addresses. An overload of DiscoverServers restricts the result to servers with specific application URIs, and an overload of DiscoverEndpoints restricts it to endpoints with specific transport profiles. To find out which endpoints a server offers, continue with the section Choose a secure endpoint:
using (var discovery = new OpcDiscoveryClient("opc.tcp://localhost:4840/")) {
foreach (OpcApplicationDescription server in discovery.DiscoverServers()) {
Console.WriteLine("{0} ({1})", server.Name, server.Type);
foreach (Uri url in server.DiscoveryUrls)
Console.WriteLine(" {0}", url);
}
}Using discovery = New OpcDiscoveryClient("opc.tcp://localhost:4840/")
For Each server As OpcApplicationDescription In discovery.DiscoverServers()
Console.WriteLine("{0} ({1})", server.Name, server.Type)
For Each url As Uri In server.DiscoveryUrls
Console.WriteLine(" {0}", url)
Next
Next
End UsingConnect to an OPC Classic server
To connect to an OPC Classic server, the client must run on Windows, and the OPC Classic runtime must be installed on the computer. Support for OPC Classic is only included in the Opc.UaFx.Advanced package.
The page OPC Classic describes what you can do after connecting to a Classic server (DA, AE, and HDA with the same API).
1. Add references to the Classic and client namespaces:
using Opc.UaFx.Client;
using Opc.UaFx.Client.Classic;Imports Opc.UaFx.Client
Imports Opc.UaFx.Client.Classic2. Create an instance of the OpcClient class with the address of the server:
var client = new OpcClient("opc.com://localhost:48410/<progId>/<classId>");Dim client As New OpcClient("opc.com://localhost:48410/<progId>/<classId>")3. Connect to the server and start the session:
client.Connect();client.Connect()4. Work with the server (your code):
// Your code to interact with the server.'Your code to interact with the server.5. Disconnect before the application exits:
client.Disconnect();client.Disconnect()6. With a using block, it looks like this:
using (var client = new OpcClient("opc.com://localhost:48410/<progId>/<classId>")) {
client.Connect();
// Your code to interact with the server.
}Using client As New OpcClient("opc.com://localhost:48410/<progId>/<classId>")
client.Connect()
'Your code to interact with the server.
End UsingHow you continue depends on whether you know the nodes and NodeIds of the server:
- Unknown: The section Explore the address space shows which nodes the server provides.
- Known: Read values or Write values
The address of the server is structured like this: opc.com://localhost:48410/<progId>/<classId>
opc.comstates that the client connects to an OPC Classic server.localhoststands for the name or IP address of the computer on which the OPC Classic server runs.48410is the optional port number that the OPC UA wrapper server[1] uses. If you omit it, the wrapper server uses a free port assigned by the system.<progId>is an optional placeholder. Replace it with theProgIDfrom the DCOM application information of the OPC Classic server, for exampleOPCManager.DA.XML-DA.Server.DA. The addressopc.com://localhost/<classId>without<progId>works as well.<classId>is a placeholder. Replace it with theClassId(also CLSID orAppID) from the DCOM application information of the OPC Classic server, for example{E4EBF7FA-CCAC-4125-A611-EAC4981C00EA}. You must specify theClassIdas a GUID.
Determine the <classId> or <progId> of the server:
Method: OpcClassicDiscoveryClient
using (var discoveryClient = new OpcClassicDiscoveryClient("<host>")) {
var servers = discoveryClient.DiscoverServers();
foreach (var server in servers) {
Console.WriteLine(
"- {0}, ClassId={1}, ProgId={2}",
server.Name,
server.ClassId,
server.ProgId);
}
}Using discoveryClient As New OpcClassicDiscoveryClient("<host>")
Dim servers = discoveryClient.DiscoverServers()
For Each server In servers
Console.WriteLine( _
"- {0}, ClassId={1}, ProgId={2}", _
server.Name, _
server.ClassId, _
server.ProgId)
Next
End UsingMethod: system information. Perform the following steps on the computer of the OPC Classic server:
- Determine the
AppIDin Component Services- Control Panel
- Administrative Tools
- Component Services
- Expand "Component Services"
- Expand "Computers"
- Expand "My Computer"
- Expand "DCOM Config"
- Select
<OPC Classic Server> - Open "Properties"
- First tab "General"
- Copy "Application ID:", which is the
AppID
- Determine the
ProgIdin the system registry- "Windows key" + "R"
- Type
regedit - Click "OK"
- Expand the "Computer" key
- Then the "HKEY_LOCAL_MACHINE" key
- Expand "SOFTWARE"
- Expand "WOW6432Node" (only on 64-bit systems, otherwise skip this step)
- Expand "Classes"
- Expand "CLSID"
- Expand
<classId> - Select
ProgID - Copy the value "(Default)" in the "Data" column, which is the
ProgID
Complete example
Complete program: a client that connects to a server and reads a temperature node once per second:
namespace Client
{
using System;
using System.Threading;
using Opc.UaFx.Client;
/// <summary>
/// This sample demonstrates how to connect to an OPC UA server and read the value of a node
/// once per second.
/// </summary>
public class Program
{
public static void Main()
{
using (var client = new OpcClient("opc.tcp://localhost:4840")) {
client.Connect();
while (true) {
var temperature = client.ReadNode("ns=2;s=Temperature");
Console.WriteLine("Current Temperature is {0} °C", temperature);
Thread.Sleep(1000);
}
}
}
}
}Imports System
Imports System.Threading
Imports Opc.UaFx.Client
Namespace Client
''' <summary>
''' This sample demonstrates how to connect to an OPC UA server and read the value of a node
''' once per second.
''' </summary>
Public Class Program
Public Shared Sub Main()
Using client = New OpcClient("opc.tcp://localhost:4840")
client.Connect()
While True
Dim temperature = client.ReadNode("ns=2;s=Temperature")
Console.WriteLine("Current Temperature is {0} °C", temperature)
Thread.Sleep(1000)
End While
End Using
End Sub
End Class
End NamespaceIf the server from the complete example of server development runs on the same computer, the client prints a line Current Temperature is … °C once per second. The value rises from 100 to 110 and then starts again at 100.
Next steps
Browse nodes
Explore the address space of a server: node by node from the ObjectsFolder, or several levels in one pass.
Read and write values
Read and write the values of single and multiple nodes, access other attributes and parts of arrays, and evaluate the results with their status.
Subscriptions
Subscribe to value changes and events of nodes, handle notifications, manage subscriptions, and narrow them down with filters.
Secure clients
Secure the client: choose an endpoint, validate server certificates, protect your own certificate, authenticate users, and use a checklist for production.
The OPC UA client starts the OPC UA wrapper server automatically and stops it when it disconnects. Each client starts its own wrapper server. The OPC UA wrapper server wraps the requests of the OPC UA client and sends them to the OPC Classic server. The OPC UA wrapper server sends the responses of the OPC Classic server back to the OPC UA client as OPC UA responses. ↩︎

