簡體   English   中英

OPC-UA open62541 sdk 運行服務器后動態添加變量節點

[英]OPC-UA open62541 sdk Add Variable nodes dynamically after run the server

閱讀OPC-UA基礎和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 仍然確保您正在使用互斥鎖。

可能有解決方案,但必須由 OPC UA 客戶端觸發。

OPC UA 規范定義了一些服務以允許客戶端添加/刪除節點或引用( AddNodesAddRefererencesDeleteNodesDeleteReferences

您的 OPC UA 客戶端和服務器都需要支持這些服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM