Skip to content

Latest commit

 

History

History
182 lines (141 loc) · 3.53 KB

README.md

File metadata and controls

182 lines (141 loc) · 3.53 KB

Client for OPC UA Server

Build with

OPC UA Foundation library

Certificates

The certificates are in the same folder the application executable.

How to use

Install

dotnet add package OPCUaClient

Import

 using OPCUaClient;

Create a instance

 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");

Create a session on the server

 int timeOut = 30;
 client.Connect(timeOut, true);

Close session

 client.Disconnect();

Read a tag

 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}");

Read multiple tags

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}");
 }

Write a tag

 client.Write("Device.Counter.Model", "NewModel");
 //Or
 await client.WriteAsync("Device.Counter.Model", "NewModel");

Write multiple tags

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);

Monitoring a tag

 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);
 });

Scan OPC UA Server

 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()}");
 }

Scan group

 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()}");
 }

Scan an address and recovery the tags

 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}");
 }

License

MIT

Icon for Freepik