OPCUaClient
OPCUaClient copied to clipboard
Monitoring / Subscription of multiple objects
Hi Luis
You could put multiple addresses in one subscription. I did it like this:
Note: I need the namespace 3. In your code, the value is fixed to value 2.
public ushort namespaceIndex = 3;
List<MonitoredItem> itemsMulti;
public void MonitoringMultiInit()
{
itemsMulti = new List<MonitoredItem>();
}
public void MonitoringMulti(string address, MonitoredItemNotificationEventHandler monitor)
{
MonitoredItem monitored = new MonitoredItem();
monitored.StartNodeId = new NodeId(address, namespaceIndex);
monitored.AttributeId = Attributes.Value;
monitored.Notification += monitor;
itemsMulti.Add(monitored);
}
public void MonitoringMultiExec(int miliseconds)
{
var subscription = this.Subscription(miliseconds);
subscription.AddItems(itemsMulti);
this.Session.AddSubscription(subscription);
subscription.Create();
subscription.ApplyChanges();
}
How to use
client.MonitoringMultiInit();
client.MonitoringMulti("Device.Counter.Value1", (_, 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);
});
client.MonitoringMulti("Device.Counter.Value2", (_, 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);
});
//....... add up to 1000 for Siemens S7 PLC
client.MonitoringMultiExec(1000);
regards RaLa
Hi @Ralalu, if you want make a pull request for your changes, do not forget add an example in the README.md