OPC UA open62541历史数据读取客户端未编译

Posted

技术标签:

【中文标题】OPC UA open62541历史数据读取客户端未编译【英文标题】:The OPC UA open62541 historical data reading client is not compiled 【发布时间】:2022-01-12 23:09:34 【问题描述】:

在图书馆的网站上,文档提供了functions for accessing historical data reading 的选项。还有一个example 在存储库中使用历史数据读取。我已经从存储库中简化了这个示例,但我仍然无法编译它。

#include <open62541/client_config_default.h>
#include <open62541/client_highlevel.h>

#include <stdio.h>
#include <stdlib.h>

static UA_Boolean readHist(UA_Client *client, const UA_NodeId *nodeId, UA_Boolean moreDataAvailable, const UA_ExtensionObject *data, void *unused)

    printf("\nRead historical callback:\n");
    printf("\tHas more data:\t%d\n\n", moreDataAvailable);

    return true;


int main(int argc, char *argv[])

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

    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:62544");
    if (retval != UA_STATUSCODE_GOOD)
    
        UA_Client_delete(client);
        return EXIT_FAILURE;
    

    UA_NodeId node = UA_NODEID_STRING(1, "MyTag");

    retval = UA_Client_HistoryRead_raw(client, &node, readHist, UA_DateTime_fromUnixTime(0), UA_DateTime_now(), UA_STRING_NULL, false, 10, UA_TIMESTAMPSTORETURN_BOTH, (void*) UA_FALSE);

    if (retval != UA_STATUSCODE_GOOD)
    
        printf("Failed. %s\n", UA_StatusCode_name(retval));
    

    UA_Client_disconnect(client);
    UA_Client_delete(client);
    return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;

编译结果:

01:02:53 **** Incremental Build of configuration Debug for project open62541 ****
Info: Internal Builder is used for build
gcc -std=c99 -DUA_ARCHITECTURE_WIN32 -DUA_ENABLE_HISTORIZING -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.c" 
In file included from C:/msys64/mingw64/include/open62541/client_config_default.h:12,
                 from ..\main.c:1:
C:/msys64/mingw64/include/open62541/client.h:389:18: error: unknown type name 'UA_HistoryReadResponse'
  389 | static UA_INLINE UA_HistoryReadResponse
      |                  ^~~~~~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/include/open62541/client.h:390:56: error: unknown type name 'UA_HistoryReadRequest'
  390 | UA_Client_Service_historyRead(UA_Client *client, const UA_HistoryReadRequest request) 
      |                                                        ^~~~~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/include/open62541/client.h: In function 'UA_Client_Service_historyRead':
C:/msys64/mingw64/include/open62541/client.h:391:5: error: unknown type name 'UA_HistoryReadResponse'; did you mean 'UA_ReadResponse'?
  391 |     UA_HistoryReadResponse response;
      |     ^~~~~~~~~~~~~~~~~~~~~~
      |     UA_ReadResponse
C:/msys64/mingw64/include/open62541/client.h:392:53: error: 'UA_TYPES_HISTORYREADREQUEST' undeclared (first use in this function); did you mean 'UA_NS0ID_HISTORYREADREQUEST'?
  392 |     __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_HISTORYREADREQUEST],
      |                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                     UA_NS0ID_HISTORYREADREQUEST
C:/msys64/mingw64/include/open62541/client.h:392:53: note: each undeclared identifier is reported only once for each function it appears in
C:/msys64/mingw64/include/open62541/client.h:393:30: error: 'UA_TYPES_HISTORYREADRESPONSE' undeclared (first use in this function); did you mean 'UA_NS0ID_HISTORYREADRESPONSE'?
  393 |         &response, &UA_TYPES[UA_TYPES_HISTORYREADRESPONSE]);
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              UA_NS0ID_HISTORYREADRESPONSE
C:/msys64/mingw64/include/open62541/client.h: At top level:
C:/msys64/mingw64/include/open62541/client.h:397:18: error: unknown type name 'UA_HistoryUpdateResponse'
  397 | static UA_INLINE UA_HistoryUpdateResponse
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/include/open62541/client.h:398:58: error: unknown type name 'UA_HistoryUpdateRequest'
  398 | UA_Client_Service_historyUpdate(UA_Client *client, const UA_HistoryUpdateRequest request) 
      |                                                          ^~~~~~~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/include/open62541/client.h: In function 'UA_Client_Service_historyUpdate':
C:/msys64/mingw64/include/open62541/client.h:399:5: error: unknown type name 'UA_HistoryUpdateResponse'
  399 |     UA_HistoryUpdateResponse response;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
C:/msys64/mingw64/include/open62541/client.h:400:53: error: 'UA_TYPES_HISTORYUPDATEREQUEST' undeclared (first use in this function); did you mean 'UA_NS0ID_HISTORYUPDATEREQUEST'?
  400 |     __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_HISTORYUPDATEREQUEST],
      |                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                     UA_NS0ID_HISTORYUPDATEREQUEST
C:/msys64/mingw64/include/open62541/client.h:401:30: error: 'UA_TYPES_HISTORYUPDATERESPONSE' undeclared (first use in this function); did you mean 'UA_NS0ID_HISTORYUPDATERESPONSE'?
  401 |         &response, &UA_TYPES[UA_TYPES_HISTORYUPDATERESPONSE]);
      |                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                              UA_NS0ID_HISTORYUPDATERESPONSE

01:02:56 Build Failed. 10 errors, 0 warnings. (took 2s.585ms)

在编译时,它会输出类似这样的错误,这些错误指的是没有类型声明的文件“client.cpp”。

我什至不知道需要做什么。我正在尝试编译开发人员提供的示例,但即使它也无法编译。这是一个错误,一个缺陷,还是我做错了什么?实现一种通过 OPC UA 读取历史数据的方法是非常有必要的。我请求帮助解决这个问题。

我的工具:

Windows 10 MSYS2 (MinGW) Eclipse IDE open62541 (v1.2.3)

更新

所以,我重建了分支库 1.2。构建成功:

User@WIN-RRMNF8HHDN5 MINGW64 ~/open62541
# git submodule update --init --recursive
Submodule path 'deps/ua-nodeset': checked out '393b633468a5d1d062dd253e1488d1d8ba335b6f'

User@WIN-RRMNF8HHDN5 MINGW64 ~/open62541
# mkdir build && cd build

User@WIN-RRMNF8HHDN5 MINGW64 ~/open62541/build
# cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUA_NAMESPACE_ZERO=FULL ..
-- Building for: Ninja
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: C:/msys64/mingw64/bin/python.exe (found version "3.9.7")
-- Found Git: C:/msys64/usr/bin/git.exe (found version "2.34.1")
-- open62541 Version: 1.2.3
-- The selected architecture is: win32
-- Test CC flag -std=c99
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -pipe
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wall
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wextra
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wpedantic
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Werror
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wno-static-in-inline
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wno-overlength-strings
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wno-unused-parameter
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wc++-compat
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wformat
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wformat-security
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wformat-nonliteral
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wmissing-prototypes
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wstrict-prototypes
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wredundant-decls
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wuninitialized
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Winit-self
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wcast-qual
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wstrict-overflow
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wnested-externs
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wmultichar
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -Wundef
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -fno-strict-aliasing
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Test CC flag -fexceptions
-- Performing Test flag_supported
-- Performing Test flag_supported - Success
-- Could NOT find Sphinx (missing: SPHINX_EXECUTABLE)
-- Could NOT find LATEX (missing: LATEX_COMPILER)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/msys64/home/User/open62541/build

User@WIN-RRMNF8HHDN5 MINGW64 ~/open62541/build
# cmake --build .
[1/66] Generating src_generated/open62541/nodeids.h
[2/66] Generating src_generated/open62541/statuscodes.h, src_generated/open62541/statuscodes.c
[3/66] Generating src_generated/open62541/transport_generated.c, src_generated/open62541/transport_generated.h, src_generated/open62541/transport_generated_handling.h, src_generated/open62541/transport_generated_encoding_binary.h
[4/66] Generating src_generated/open62541/types_generated.c, src_generated/open62541/types_generated.h, src_generated/open62541/types_generated_handling.h, src_generated/open62541/types_generated_encoding_binary.h
[5/66] Generating src_generated/open62541/namespace0_generated.c, src_generated/open62541/namespace0_generated.h
INFO:__main__:Preprocessing C:/msys64/home/User/open62541/deps/ua-nodeset/Schema/Opc.Ua.NodeSet2.xml
INFO:__main__:Generating Code for Backend: open62541
INFO:__main__:NodeSet generation code successfully printed
[6/66] Building C object CMakeFiles/open62541-object.dir/src_generated/open62541/statuscodes.c.obj
[7/66] Building C object CMakeFiles/open62541-object.dir/src_generated/open62541/transport_generated.c.obj
[8/66] Building C object CMakeFiles/open62541-object.dir/src_generated/open62541/types_generated.c.obj
[9/66] Building C object CMakeFiles/open62541-object.dir/src/ua_types_print.c.obj
[10/66] Building C object CMakeFiles/open62541-object.dir/src/ua_types.c.obj
[11/66] Building C object CMakeFiles/open62541-object.dir/src/ua_types_encoding_binary.c.obj
[12/66] Building C object CMakeFiles/open62541-object.dir/src/ua_connection.c.obj
[13/66] Building C object CMakeFiles/open62541-object.dir/src/ua_timer.c.obj
[14/66] Building C object CMakeFiles/open62541-object.dir/src/ua_util.c.obj
[15/66] Building C object CMakeFiles/open62541-object.dir/src/ua_securechannel.c.obj
[16/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_session.c.obj
[17/66] Building C object CMakeFiles/open62541-object.dir/src/ua_securechannel_crypto.c.obj
[18/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_ns0.c.obj
[19/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server.c.obj
[20/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_nodes.c.obj
[21/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_utils.c.obj
[22/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_discovery.c.obj
[23/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_config.c.obj
[24/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_binary.c.obj
[25/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_networkmessage.c.obj
[26/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_writer.c.obj
[27/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_server_async.c.obj
[28/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_ns0.c.obj
[29/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_reader.c.obj
[30/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_method.c.obj
[31/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_manager.c.obj
[32/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_subscription.c.obj
[33/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_attribute.c.obj
[34/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_session.c.obj
[35/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_monitoreditem.c.obj
[36/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_view.c.obj
[37/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_discovery.c.obj
[38/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_discovery_multicast.c.obj
[39/66] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client_discovery.c.obj
[40/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_nodemanagement.c.obj
[41/66] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client.c.obj
[42/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_services_securechannel.c.obj
[43/66] Building C object CMakeFiles/open62541-object.dir/deps/aa_tree.c.obj
[44/66] Building C object CMakeFiles/open62541-object.dir/deps/libc_time.c.obj
[45/66] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client_connect.c.obj
[46/66] Building C object CMakeFiles/open62541-object.dir/deps/base64.c.obj
[47/66] Building C object CMakeFiles/open62541-object.dir/deps/pcg_basic.c.obj
[48/66] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client_highlevel.c.obj
[49/66] Building C object CMakeFiles/open62541-object.dir/src/client/ua_client_subscriptions.c.obj
[50/66] Building C object CMakeFiles/open62541-object.dir/src/pubsub/ua_pubsub_config.c.obj
[51/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_subscription.c.obj
[52/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_subscription_monitoreditem.c.obj
[53/66] Building C object CMakeFiles/open62541-object.dir/src/server/ua_subscription_datachange.c.obj
[54/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/ua_log_stdout.c.obj
[55/66] Building C object CMakeFiles/open62541-object.dir/src/ua_types_lex.c.obj
[56/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/ua_nodestore_ziptree.c.obj
[57/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/ua_config_default.c.obj
[58/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/ua_nodestore_hashmap.c.obj
[59/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/ua_accesscontrol_default.c.obj
[60/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/crypto/ua_pki_none.c.obj
[61/66] Building C object CMakeFiles/open62541-plugins.dir/arch/win32/ua_clock.c.obj
[62/66] Building C object CMakeFiles/open62541-plugins.dir/plugins/crypto/ua_securitypolicy_none.c.obj
[63/66] Building C object CMakeFiles/open62541-plugins.dir/arch/win32/ua_architecture_functions.c.obj
[64/66] Building C object CMakeFiles/open62541-plugins.dir/arch/network_tcp.c.obj
[65/66] Building C object CMakeFiles/open62541-object.dir/src_generated/open62541/namespace0_generated.c.obj
C:/msys64/home/User/open62541/build/src_generated/open62541/namespace0_generated.c: In function 'namespace0_generated':
C:/msys64/home/User/open62541/build/src_generated/open62541/namespace0_generated.c:117159:15: note: variable tracking size limit exceeded with '-fvar-tracking-assignments', retrying without
117159 | UA_StatusCode namespace0_generated(UA_Server *server) 
       |               ^~~~~~~~~~~~~~~~~~~~
[66/66] Linking C shared library bin\libopen62541.dll

安装库后,我尝试再次构建我的代码。这次弹出了一个完全不同的错误:

03:18:12 **** Rebuild of configuration Debug for project open62541 ****
Info: Internal Builder is used for build
gcc -std=c99 -DUA_ARCHITECTURE_WIN32 -DUA_ENABLE_HISTORIZING -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "..\\main.c" 
gcc -o open62541.exe main.o -lopen62541 -lws2_32 
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: main.o: in function `main':
C:\msys64\home\User\open62541\Debug/../main.c:78: undefined reference to `__imp_UA_Client_HistoryRead_raw'
collect2.exe: error: ld returned 1 exit status

03:18:14 Build Failed. 2 errors, 0 warnings. (took 2s.452ms)

这是我要编译的代码本身:

#include <open62541/client_config_default.h>
#include <open62541/client_highlevel.h>

#include <stdio.h>
#include <stdlib.h>

static UA_Boolean readHist(UA_Client *client, const UA_NodeId *nodeId, UA_Boolean moreDataAvailable, const UA_ExtensionObject *data, void *unused)

    printf("\nRead historical callback:\n");
    printf("\tHas more data:\t%d\n\n", moreDataAvailable);

    return true;


int main(int argc, char *argv[])

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

    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:62544");
    if (retval != UA_STATUSCODE_GOOD)
    
        UA_Client_delete(client);
        return EXIT_FAILURE;
    

    UA_NodeId node = UA_NODEID_STRING(1, "TNPZ.P_NEFT.R1.M");

    retval = UA_Client_HistoryRead_raw(client, &node, readHist, UA_DateTime_fromUnixTime(0), UA_DateTime_now(), UA_STRING_NULL, false, 10, UA_TIMESTAMPSTORETURN_BOTH, (void*) UA_FALSE);

    if (retval != UA_STATUSCODE_GOOD)
    
        printf("Failed. %s\n", UA_StatusCode_name(retval));
    

    UA_Client_disconnect(client);
    UA_Client_delete(client);
    return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;

【问题讨论】:

【参考方案1】:

为了使读取历史数据的机制起作用,需要启用 UA_ENABLE_HISTORIZING 参数WHEN BUILDING THE LIBRARY,该参数未在库的the official documentation 中指定。

cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUA_ENABLE_HISTORIZING=ON -DUA_NAMESPACE_ZERO=FULL ..

【讨论】:

以上是关于OPC UA open62541历史数据读取客户端未编译的主要内容,如果未能解决你的问题,请参考以下文章

open62541 客户端断开连接时的 OPC UA 堆栈服务器端回调

基于open62541的OPC UA服务器建立

基于open62541的opc ua 服务器开发实现

如何使用 open62541 一次使用 OPC-UA 编写多个节点?

从 open62541Client 读取节点的 UA_Arguments

open62541:同时获取订阅的所有变更监控项