09/21/2020
I'm having trouble with OPC UA Subscriptions and Notifications. I want to subscribe server data. So I defined a monitoredItem list to capture the notification.
var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1000 }; var list = new List<MonitoredItem> { new MonitoredItem(subscription.DefaultItem) { DisplayName = "Speed", StartNodeId = "ns=2;i=4"}, }; Debug.Log(list[0].StartNodeId); list.ForEach(i => i.Notification += OnNotification); subscription.AddItems(list); list.ForEach(i => Debug.Log(i.Subscription.Id));
When ever it get new notification, it suppose to call the OnNotification deligate. But it my case the OnNotification is never be called.
private static void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e) { Debug.Log("OnNotification"); foreach (var value in item.DequeueValues()) { Debug.LogFormat("{0}: {1}, {2}, {3}", item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode); } }
How can I implement subscription mechanism in this case?
09/21/2020
Here is what I have done till now. Could you please tell me what else I need to change in my code?
Debug.Log("Step 4 - Create a subscription. Set a faster publishing interval if you wish.");
var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1000 };Debug.Log("Step 5 - Add a list of items you wish to monitor to the subscription.");
var list = new List<MonitoredItem> {
new MonitoredItem(subscription.DefaultItem) { DisplayName = "Speed", StartNodeId = "ns=2;i=2"},
};
Debug.Log(list[0].StartNodeId);
list.ForEach(i => i.Notification += OnNotification);
subscription.AddItems(list);
list.ForEach(i => Debug.Log(i.Subscription.Id));
subscription.PublishingEnabled = true;Debug.Log("Step 6 - Add the subscription to the session.");
session.AddSubscription(subscription);
subscription.Create();Debug.Log("Finished client initialization");
Console.WriteLine("Finished client initialization");
1 Guest(s)