阅读OPC-UA foundation和OPC-UA open62541 sdk的文档和示例,变量节点总是添加在开始运行服务器的语句之前。 是否可以在服务器启动后添加它们?如果我更改不起作用的语句的顺序。
和我一起思考以下情况:一旦我们开始异步运行应用程序/软件(而不是服务器),我就需要做一些 http 请求。然后服务器启动,在我的http请求完成后,我根据从网络返回的信息添加了变量节点。
我在代码上添加了一些注释以阐明我正在尝试做的事情。
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;
}
最佳答案
是的,可以使用 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
。仍然确保您使用的是互斥量。
关于c++ - OPC-UA open62541 sdk 运行服务器后动态添加变量节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55169396/