Tutorial
Getting started
Set up your first server with the OPC UA .NET SDK: import the namespace, create the server, start it, and stop it again.
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.Advanced(Downloads) - 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)
Create the server
1. Add a reference to the Opc.UaFx.Server namespace (package Opc.UaFx.Advanced). Most other types are in the Opc.UaFx namespace:
using Opc.UaFx.Server;Imports Opc.UaFx.Server2. Create an instance of the OpcServer class with the default base address you want. If you omit it, the server uses the address opc.tcp://localhost:4840:
var server = new OpcServer("opc.tcp://localhost:4840/");Dim server = New OpcServer("opc.tcp://localhost:4840/")Start the server
3. Start the server and serve clients:
server.Start();server.Start()4. Your code that handles the client requests:
// Your code to process client requests.'Your code to process client requests.Stop the server
5. Before the application exits, close all sessions and shut down the server:
server.Stop();server.Stop()Server in a using block
6. With a using block, it looks like this:
using (var server = new OpcServer("opc.tcp://localhost:4840/")) {
server.Start();
// Your code to process client requests.
}Using server As new OpcServer("opc.tcp://localhost:4840/")
server.Start()
'Your code to process client requests.
End UsingServer in tests
You can start a server with test nodes in the same process as a client to test client logic automatically without a plant. The Test clients page shows how.
Complete example
Your own server that provides a temperature node, updates its value cyclically, and serves the subscriptions of clients:
namespace Server
{
using System.Threading;
using Opc.UaFx;
using Opc.UaFx.Server;
/// <summary>
/// This sample demonstrates how to setup an OPC UA server that provides a temperature node and
/// updates its value once per second.
/// </summary>
public class Program
{
public static void Main()
{
var temperatureNode = new OpcDataVariableNode<double>("Temperature", 100.0);
using (var server = new OpcServer("opc.tcp://localhost:4840/", temperatureNode)) {
server.Start();
while (true) {
if (temperatureNode.Value == 110)
temperatureNode.Value = 100;
else
temperatureNode.Value++;
temperatureNode.ApplyChanges(server.SystemContext);
Thread.Sleep(1000);
}
}
}
}
}Imports System.Threading
Imports Opc.UaFx
Imports Opc.UaFx.Server
Namespace Server
''' <summary>
''' This sample demonstrates how to setup an OPC UA server that provides a temperature node and
''' updates its value once per second.
''' </summary>
Public Class Program
Public Shared Sub Main()
Dim temperatureNode = New OpcDataVariableNode(Of Double)("Temperature", 100.0)
Using server = New OpcServer("opc.tcp://localhost:4840/", temperatureNode)
server.Start()
While True
If (temperatureNode.Value = 110) Then
temperatureNode.Value = 100
Else
temperatureNode.Value += 1
End If
temperatureNode.ApplyChanges(server.SystemContext)
Thread.Sleep(1000)
End While
End Using
End Sub
End Class
End NamespaceThe server provides the Temperature node and raises its value once per second from 100 to 110, then starts again at 100. To check it, use the client from the complete example of client development: it reads the value once per second and prints it.
Next steps
Manage nodes
Create nodes in the address space of the server, control their visibility, and update them at runtime.
Read and write values
Provide the values of nodes in the server and handle read and write access from clients.
Node models
Use OpcNodeModel to define which child nodes are created with a typed node, and take initial values from the type definition.
Secure servers
Secure the server: endpoints, certificates, users, roles and access control lists, audit events, and a checklist for production.

