Client for OPC UA Server
OPC UA Foundation library
The certificates are in the same folder the application executable.
dotnet add package OPCUaClient
using OPCUaClient;
UaClient client = new UaClient("test", "opc.tcp://192.168.0.50:4840", true, true);
UaClient auth = new UaClient("test", "opc.tcp://192.168.0.50:4840", true, true, "admin", "password");
int timeOut = 30;
client.Connect(timeOut, true);
client.Disconnect();
Tag tag = client.Read("Device.Counter.OK");
//Or
tag = await client.Read("Device.Counter.OK");
Console.WriteLine($"Name: {tag.Name}");
Console.WriteLine($"Address: {tag.Address}");
Console.WriteLine($"Value: {tag.Value}");
Console.WriteLine($"Quality: {tag.Quality}");
Console.WriteLine($"Code: {tag.Code}");
var address = new List<String>
{
"Device.Counter.OK",
"Device.Counter.NG",
"Device.Counter.Model",
"Device.Counter.CycleTime"
}
var tags = client.Read(address);
//Or
tags await = client.ReadAsync(address);
foreach(var tag in tags)
{
Console.WriteLine($"Name: {tag.Name}");
Console.WriteLine($"Address: {tag.Address}");
Console.WriteLine($"Value: {tag.Value}");
Console.WriteLine($"Quality: {tag.Quality}");
Console.WriteLine($"Code: {tag.Code}");
}
client.Write("Device.Counter.Model", "NewModel");
//Or
await client.WriteAsync("Device.Counter.Model", "NewModel");
var tags = new List<Tag>
{
new Tag {
Address = "Device.Counter.OK",
Value = 0,
},
new Tag {
Address = "Device.Counter.NG",
Value = 0,
},
new Tag {
Address = "Device.Counter.Model",
Value = "OtherModel",
},
new Tag {
Address = "Device.Counter.CycleTime",
Value = 10,
},
}
client.Write(tags);
//Or
await client.WriteAsync(tags);
client.Monitoring("Device.Counter.OK", 500, (_, e) => {
// Anything you need to be executed when the value changes
// Get the value of the tag being monitored
var monitored = (MonitoredItemNotification)e.NotificationValue;
Console.WriteLine(monitored.Value);
});
var devices = client.Devices(true);
//Or
devices = await client.DevicesAsync(true);
foreach(var device in devices)
{
Console.WriteLine($"Name: {device.Name}");
Console.WriteLine($"Address: {device.Address}");
Console.WriteLine($"Groups: {device.Groups.Count()}");
Console.WriteLine($"Tags: {device.Tags.Count()}");
}
var groups = client.Group("Device", true);
//Or
groups = await client.GroupAsync("Device", true);
foreach(var group in groups)
{
Console.WriteLine($"Name: {group.Name}");
Console.WriteLine($"Address: {group.Address}");
Console.WriteLine($"Groups: {group.Groups.Count()}");
Console.WriteLine($"Tags: {group.Tags.Count()}");
}
var tags = client.Tags("Device.Counter");
//Or
tags = await client.TagsAsync("Device.Counter");
foreach(var tag in tags)
{
Console.WriteLine($"Name: {tag.Name}");
Console.WriteLine($"Address: {tag.Address}");
}
Icon for Freepik