OPC UA C++ 监控项不回调函数

OPCUA C++ Monitored item doest'n callback the function

我在 opcua 的世界里还很陌生,我正在尝试用 C++ 中的客户端监视服务器变量。

我使用的是1.2.2版本的opcua

我在节点 (1,6070) 的服务器中有一个布尔变量,当我 运行 以下代码时,我收到日志 :

[2021-08-03 15:27:47.442 (UTC+0200)] info/session Connection 5 | SecureChannel 2 | Session ns=1;g=913a21de-f467-5bc9-ed9e-29b27b470490 | Subscription 2 | Created the Subscription with a publishing interval of 500.00 ms

但我从来没有达到 'handler_events_datachange' 的功能,我现在只在其中放置一个输出。 (顺便说一句,我确定节点 6070 中的值已更改)

感谢您的帮助!

int main(void) {
   signal(SIGINT, stopHandler); /* catches ctrl-c */

   UA_Client *client = UA_Client_new();
   UA_ClientConfig *cc = UA_Client_getConfig(client);
   UA_ClientConfig_setDefault(cc);

   UA_Client_connect(client, "opc.tcp://localhost");

   UA_MonitoredItemCreateResult result;
   UA_CreateSubscriptionResponse response; // warning memory leak
   UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();

   UA_Client_Subscriptions_create(client, request, NULL, NULL,  NULL);
   UA_Int32 subId = response.subscriptionId;

   if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
   {
      UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND ,  "subscription succed");
   } else {
      UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND ,  "subscription UNsucced");
   }

   UA_MonitoredItemCreateRequest monRequest = UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(1, 6070));

   result = UA_Client_MonitoredItems_createDataChange(client, subId, UA_TIMESTAMPSTORETURN_BOTH, monRequest, NULL, handler_events_datachange, NULL);

   while(running) {
    
   }
}

终于找到错误了!

The problem comme from the fact no means of handling asynchronous events automatically is provided. However, some synchronous function calls will trigger handling, but to ensure this happens a client should periodically call UA_Client_run_iterate explicitly.

所以解决方法是在while()中加入UA_Client_run_iterate(client,100)。

我不完全理解超时是怎么回事,但如果可以的话我会完成这个回答