Implement subscription mechanism|OPC UA Implementation: Stacks, Tools, and Samples|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
Implement subscription mechanism
Avatar
Soaib Safi
New Member
Members
Forum Posts: 2
Member Since:
09/21/2020
sp_UserOfflineSmall Offline
1
09/21/2020 - 04:40
sp_Permalink sp_Print

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?Cry

Avatar
Randy Armstrong
Admin
Forum Posts: 1564
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
2
09/21/2020 - 10:18
sp_Permalink sp_Print

There are many potential issues:

1) Subscription is disabled;

2) Subscription has not been created;

3) CreateMonitoredItems was not called;

4) MonitoringMode != Reporting;

5) The event on the subscription object has not been subscribe to.

Avatar
Soaib Safi
New Member
Members
Forum Posts: 2
Member Since:
09/21/2020
sp_UserOfflineSmall Offline
3
09/25/2020 - 01:56
sp_Permalink sp_Print sp_EditHistory

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

Avatar
Randy Armstrong
Admin
Forum Posts: 1564
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
4
09/25/2020 - 14:49
sp_Permalink sp_Print

Check the item Status.Error. Should be Good. If not it tells why it was not created.

Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 50
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1435
Posts: 4855