Quick Start
Quick Start
Connect and Read a Value
The simplest use case: connect to an OPC UA server, read a node value, and disconnect.
using OpcSharp.Client;
using OpcSharp.Types;
var client = new OpcSharpClientBuilder()
.WithEndpoint("opc.tcp://localhost:4840")
.Build();
await client.ConnectAsync();
// Read the Server.ServerStatus.CurrentTime node
var results = await client.ReadAsync(new[]
{
new ReadValueId
{
NodeId = new NodeId(0, 2258),
AttributeId = AttributeIds.Value
}
});
Console.WriteLine($"Server time: {results[0].Value}");
await client.DisconnectAsync();Write a Value
var statusCodes = await client.WriteAsync(new[]
{
new WriteValue
{
NodeId = new NodeId(2, "MyVariable"),
AttributeId = AttributeIds.Value,
Value = new DataValue(new Variant(42))
}
});Browse the Address Space
var browseResults = await client.BrowseAsync(new[]
{
new BrowseDescription
{
NodeId = ObjectIds.ObjectsFolder,
BrowseDirection = BrowseDirection.Forward,
ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
IncludeSubtypes = true,
ResultMask = (uint)BrowseResultMask.All
}
});
foreach (var reference in browseResults[0].References)
{
Console.WriteLine($"{reference.DisplayName} ({reference.NodeId})");
}Subscribe to Data Changes
var subscription = await client.CreateSubscriptionAsync(
publishingInterval: 1000);
var monitoredItems = await client.CreateMonitoredItemsAsync(
subscription.SubscriptionId,
new[]
{
new MonitoredItemCreateRequest
{
ItemToMonitor = new ReadValueId
{
NodeId = new NodeId(0, 2258),
AttributeId = AttributeIds.Value
},
MonitoringMode = MonitoringMode.Reporting,
RequestedParameters = new MonitoringParameters
{
SamplingInterval = 500,
QueueSize = 10
}
}
});
client.DataChanged += (sender, args) =>
{
Console.WriteLine($"Value changed: {args.Value}");
};
// Keep the application running to receive notifications
await Task.Delay(Timeout.Infinite);Complete Getting Started Sample
A standalone sample project is available at samples/GettingStarted that uses OpcSharp via NuGet packages (not project references) — exactly how end users would consume the SDK.
Create the project
mkdir MyOpcUaApp && cd MyOpcUaApp
dotnet new console
dotnet add package OpcSharp.ClientFull example
The complete source is at samples/GettingStarted/Program.cs. It connects to an OPC UA server, reads values, browses the address space, discovers endpoints, subscribes to data changes for 5 seconds, and cleans up.
Run it
# Clone and run
git clone https://github.com/patdhlk/OpcSharp.git
cd OpcSharp
# Start the test OPC UA server
docker compose -f docker/docker-compose.yml up -d
# Run the sample (uses NuGet packages, not project references)
dotnet run --project samples/GettingStartedUsing Dependency Injection
services.AddOpcSharpClient("MyClient", options =>
{
options.EndpointUrl = "opc.tcp://localhost:4840";
options.Security.PolicyUri = SecurityPolicyUris.None;
});
// Inject IOpcSharpClientFactory and create a named client
public class MyService(IOpcSharpClientFactory clientFactory)
{
public async Task ReadAsync()
{
var client = clientFactory.CreateClient("MyClient");
await client.ConnectAsync();
// ...
}
}