Skip to main content

Quick Start (C#)

Get Darra OPC UA ClientSDK Connected and Up and Running in 5 Minutes OPC UA Server, Starting from ScratchConnect / Browse / Read/Write / Subscribe / Method Call End-to-End. This page uses C# For examples, see the usage in other languages at Java · Python · C++ · Rust · C.

Advanced Topics

Preparations​

Before starting the first demo, make sure you have the following three items ready.

Environment​

  • Operating System — Windows 10 / 11 x64
  • .NET — .NET 8
  • Port — OPC UA Default 4840/tcp, allow through the firewall

Install SDK (NuGet)​

In your .NET project directory, run:

dotnet add package Darra.OpcUa

NuGet automatically copies Darra.OpcUa.SDK.dll + Darra.OpcUa.Core.dll to the output directory; no additional action is required.

Other Languages: Python pip install darra-opcua · Java xyz.darra:darra-opcua-sdk:0.6.0 · Rust cargo add darra-opcua · C++ (darra/opcua/client.hpp 18 headers, C++17 header-only) · C opcua.h. For installation and usage instructions for each language, see Java · Python · C++ · Rust · C.

Server endpoint​

Prepare an accessible endpoint OPC UA Server in the form of opc.tcp://<host>:4840.

The First Complete Program​

A Complete C# Console Program: A Step-by-Step Guide Create Session → Connect → Browse → Read → Write → Subscribe → Method Call → Disconnect. Copy it to any .NET project to run it.

using DarraOpcUa_Client;
using System;
using System.Threading.Tasks;

class Program
{
static async Task Main()
{
// ========== 1. Create a Session ==========
// The endpoint points to port 4840 on the industrial control computer where the Service PLC is running; use `localhost` when debugging on this machine.
using var ua = new DarraOpcUa("opc.tcp://192.168.1.100:4840");
ua.Events.Connected += (s, e) => Console.WriteLine($"[OK] 已连接 {e.EndpointUrl}");
ua.Events.Disconnected += (s, e) => Console.WriteLine("[--] 已断开");

// ========== 2. Connecting to the Server ==========
ua.Connect();
Console.WriteLine($"会话状态: {ua.State}");

// ========== 3. Browsing Nodes ==========
// Starting with the Objects folder (i=85), the Darra Server PLC mounts the data PLC under the following folder DarraPLC :
// DarraPLC / Tags / DataBlocks / GVL / Diagnostics
Console.WriteLine("\n--- Objects 文件夹下的子节点 ---");
foreach (var r in ua.Browse("i=85"))
Console.WriteLine($" {r.NodeId,-32} [{r.NodeClass}] {r.DisplayName}");

// ========== 4. Reading a Single Node ==========
using (var dv = ua.Read("i=2258")) // i=2258 = ServerStatus.CurrentTime (OPC UA standard node)
Console.WriteLine($"\n服务器时间 = {dv.Value.AsDateTime} [{dv.Status}]");

// ========== 5. Write-Only Node ==========
// ns=2 = Darra PLC namespace; Here, declare a GVL global variable (assuming `Speed` is defined in the project)
var speed = new Variant();
speed.Int32 = 1500;
var st = ua.Write("ns=2;s=GVL.Speed", speed);
Console.WriteLine($"写入 GVL.Speed=1500, 返回状态 = {st}");

// ========== 6. Subscription Data Changes ==========
using var sub = ua.CreateSubscription(publishingIntervalMs: 500);
sub.DataChanged += (s, e) =>
Console.WriteLine($" [推送] {e.NodeId} = {e.ValueString} ({e.Status})");
sub.Add("i=2258"); // Server time changes every second
sub.Add("ns=2;s=GVL.Speed"); // PLC Global variable; push when the value changes

Console.WriteLine("\n收 5 秒订阅推送...");
await Task.Delay(5000);

// ========== 7. Method Calls (Optional) ==========
// Use only when the remote server provides a ` OPC UA Method` node (PLC whether the server has a `Method` is determined by the project configuration).
try
{
using var a = new Variant(); a.Int32 = 3;
using var b = new Variant(); b.Int32 = 5;
var outputs = ua.Call("ns=2;s=Methods", "ns=2;s=Methods.Add", a, b);
foreach (var o in outputs)
using (o) Console.WriteLine($"\n调用 Add(3, 5) = {o.AsInt32}");
}
catch (OpcUaException ex)
{
Console.WriteLine($"\n方法调用跳过 (服务端未提供 Method 节点): {ex.StatusCode}");
}

// ========== 8. Disconnect (using automatic release) ==========
Console.WriteLine("\n程序结束, using 块结束时自动 Disconnect + Dispose.");
}
}

Code Highlights​

  • using var ua = ... Automatically released when exiting the scope Disconnect + Dispose; no manual release required
  • Read Returned DataValue / Call Returned Variant all hold resource handles; they must using otherwise a leak will occur
  • Default: Anonymous + None security mode; Sign for production use SignAndEncrypt, see Enable Encryption
  • Subscription callbacks run in a separate UI thread; in WinForms, they must WPF be Invoke switched back to the main thread
  • Batch Scenarios: ReadMany / BrowseMany / sub.AddMany Once RPC Equivalent to N times

For a complete list of required and API optional parameters, see C# SDK Overview.

How it looks in motion​

Save the complete program above as Program.cs and place it in a dotnet new console project, then add dotnet add package Darra.OpcUa, make sure the remote OPC UA Server is running (for example, the Darra Software PLC service is started and exposing port 4840), and then dotnet run. When Darra PLC Server connecting to, the expected output will look something like this:

[OK] Connected to opc.tcp://192.168.1.100:4840
Session state: Connected

--- Child nodes under the Objects folder ---
i=2253 [Object] Server
ns=2;s=DarraPLC [Object] DarraPLC

Server time = 2026-04-25T08:00:00Z [Good]
Wrote GVL.Speed=1500, returned status = Good

Receiving 5-second subscription updates...
[推送] i=2258 = 2026-04-25T08:00:00.500Z (Good)
[推送] ns=2;s=GVL.Speed = 1500 (Good)
[推送] i=2258 = 2026-04-25T08:00:01.000Z (Good)
...

Method call skipped (server does not provide a Method node): BadNodeIdUnknown

Program finished; Disconnect + Dispose are called automatically at the end of the using block.
[--] Disconnected

Node Structure: DarraPLC Below are Tags / DataBlocks / GVL / Diagnostics four subfolders, corresponding to the PLC project's tag list, data blocks, global variables, and diagnostic nodes, respectively. Continue Browse("ns=2;s=DarraPLC") to drill down further.

Next Step​

Things I Want to DoLook here
Encrypted Connection (Sign / SignAndEncrypt)Extensions — Enable Encryption
Server detected (GetEndpoints / FindServers)Extensions — Discovery
Historical Data (HistoryRead 5 Modes)Extensions — History: Historical Access
Alarms and ConditionsExtensions — Event Subscriptions
Complete C# API ReferencesC# SDK Overview
Usage in Other LanguagesJava · Python · C++ · Rust · C
Real-World ExamplesMulti-Source Subscription Collection · End-to-end CSV · Modbus Bridging · Mass Surveillance