OPC-UA open62541 sdk运行服务器后动态添加变量节点

问题描述 投票:1回答:2

阅读OPC-UA基础和OPC-UA open62541 sdk的文档和示例,始终在语句之前添加变量节点以开始运行服务器。服务器启动后是否可以添加它们?如果我更改不起作用的语句的顺序。

请与我一起考虑以下情况:我们需要尽快开始运行应用程序/软件(而不是服务器)异步进行一些http请求。然后服务器启动,在我的http请求完成后,我根据从Web返回的信息添加了变量节点。

我对代码做了一些评论,以澄清我想要做的事情。

int main() {
    signal(SIGINT, stopHandler);
    signal(SIGTERM, stopHandler);

    UA_ServerConfig *config = UA_ServerConfig_new_default();
    UA_Server *server = UA_Server_new(config);

    // If I put this statement after the other statement:
    // UA_StatusCode retval = UA_Server_run(server, &running);
    // The variables are not added.
    addVariable(server);

    // I would like to add some variables nodes after this statement, 
    // for example, like I said I request some information online 
    // and I will add the nodes after return from this request asynchronous.
    UA_StatusCode retval = UA_Server_run(server, &running);
    UA_Server_delete(server);
    UA_ServerConfig_delete(config);
    return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
c++ opc-ua open62541
2个回答
1
投票

是的,可以使用UA_Server_addVariableNode,因为你已经在addVariable()中使用它(可能)。我猜您的代码基于https://github.com/open62541/open62541/blob/master/examples/tutorial_server_variable.c

简单地重新排序代码就不起作用了

UA_StatusCode retval = UA_Server_run(server, &running);

阻止。

您需要更改此设置以使用迭代方法:

UA_StatusCode retval = UA_Server_run_startup(server);
if(retval != UA_STATUSCODE_GOOD)
   return 1;

while(running) {
    UA_UInt16 timeout = UA_Server_run_iterate(server, waitInternal);

    // HERE you can add any node to the server you like.
    // e.g. call addVariable2().
    // Make sure that you only call it once in the loop.

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = timeout * 1000;
    select(0, NULL, NULL, NULL, &tv);
}
retval = UA_Server_run_shutdown(server);

请注意,open62541目前不支持多线程。如果要在另一个线程中添加变量,则需要确保对静态访问server对象进行静音。

上面的例子基于:https://github.com/open62541/open62541/blob/master/examples/server_mainloop.c

另一种方法是启动另一个处理异步请求的线程,然后在分离的线程中调用UA_Server_addVariableNode。仍然要确保您使用互斥锁。


0
投票

可能有一个解决方案,但它必须是OPC UA客户端的triggererd。

OPC UA规范定义了一些服务,允许客户端添加/删除节点或引用(AddNodesAddRefererencesDeleteNodesDeleteReferences

OPC UA客户端和服务器都需要支持这些服务。

© www.soinside.com 2019 - 2024. All rights reserved.