EventHandler OPC UA

时间:2018-05-03 09:47:02

标签: events notifications opc-ua

我正在尝试使用opc ua将服务器传送到客户端,它工作正常,直到我希望我的程序返回值。我使用OnNotification函数:

public void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
{
   // Debug.Log("Entre aqui");

    foreach (var value in item.DequeueValues())
    {
       // Debug.Log("Entre aqui otra vez");
        //Console.WriteLine("{0}: {1}, {2}, {3}", item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
        Debug.Log("{0}: {1}, {2}, {3}" + item.DisplayName + value.Value + value.SourceTimestamp + value.StatusCode);
        //rotZ = float.Parse(value.Value.ToString());
        //J1.Rotate(0, 0, rotZ);
    }
}

这是我创建Monitored Item的地方,我将MonitoredItem EventHandler添加到函数中,但我不知道为什么i.Notification事件永远不会发生,因此它永远不会运行OnNotification函数。 我应该触发事件发生吗?我做错了什么?

        var filter = new EventFilter();


        var triggeringItemId = new MonitoredItem(subscription.DefaultItem)
        {
            NodeClass = NodeClass.Object,
            StartNodeId = ObjectIds.Server,
            AttributeId = Attributes.EventNotifier,
            MonitoringMode = MonitoringMode.Reporting,
            SamplingInterval = -1,
            QueueSize = 100,
            CacheQueueSize = 100,
            Filter = filter 
        };
        // Log("Step 5 - Add a list of items you wish to monitor to the subscription.");
        var list = new List<MonitoredItem> {
            triggeringItemId,
            /*
            monitoredItem,
            monitoredItem2
            */
        };

        list.ForEach(i => i.Notification += OnNotification);

        Debug.Log(list);

        subscription.AddItems(list);

        // Log("Step 6 - Add the subscription to the session.");
        session.AddSubscription(subscription);
        subscription.Create();

2 个答案:

答案 0 :(得分:0)

按此顺序,它对我有用:

libfoo.so

然后我创建session.AddSubscription(subscription); subscription.Create(); ,然后将它们添加到订阅中,以及您错过的最后一步:

MonitoredItems

答案 1 :(得分:0)

尝试添加一些&#39;选择&#39; EventFilter的子句。以下列表对应于基本事件类型。 AlarmCondition类型提供了更多字段。

var filter = new EventFilter();

filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.EventId);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.EventType);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.SourceNode);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.SourceName);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.Time);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.ReceiveTime);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.LocalTime);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.Message);  filter.AddSelectClause(ObjectTypes.BaseEventType,Opc.Ua.BrowseNames.Severity);