Skip to content

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.

This content was machine translated and may contain errors. Read in German

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:

csharp
using Opc.UaFx.Server;
vbnet
Imports Opc.UaFx.Server

2. 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:

csharp
var server = new OpcServer("opc.tcp://localhost:4840/");
vbnet
Dim server = New OpcServer("opc.tcp://localhost:4840/")

Start the server ​

3. Start the server and serve clients:

csharp
server.Start();
vbnet
server.Start()

4. Your code that handles the client requests:

csharp
// Your code to process client requests.
vbnet
'Your code to process client requests.

Stop the server ​

5. Before the application exits, close all sessions and shut down the server:

csharp
server.Stop();
vbnet
server.Stop()

Server in a using block ​

6. With a using block, it looks like this:

csharp
using (var server = new OpcServer("opc.tcp://localhost:4840/")) {
    server.Start();
    // Your code to process client requests.
}
vbnet
Using server As new OpcServer("opc.tcp://localhost:4840/")
    server.Start()
    'Your code to process client requests.
End Using

Server 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:

csharp
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);
                }
            }
        }
    }
}
vbnet
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 Namespace

The 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 ​