一、前言
为了统一公司的驱动层开发,准备采用OpcUA的方式转发底层数据,而服务器有Windows Server,也有CentOS,因此想用Qt开发一个基于MinGW的OpcUA Server,这样就能跨平台部署。这里记录一下,希望对你也有用。
二、准备open62541
open62541是一款基于C语言实现的OPC UA通信库。该库实现了OPC UA标准的客户端和服务器端,并支持各种操作系统和编译器。该OPC UA通信库功能丰富、易于移植、可扩展性好、性能优越,被广泛应用于工业自动化、智能制造、物联网等领域,是开发OPC UA应用的一个重要选择
1. 下载open62541
地址:https://github.com/open62541/open62541/releases/tag/v1.3.9
2. 安装CMake
3. 安装编译环境:MinGW
MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net
4. 设置环境变量
5. 安装Python3
三、编译
1.、在open62541目录下建立build_mingw文件夹。
2、打开CMake GUI,选择路径源文件路径和要编译输出的路径build_mingw
出现以下问题,则表示他没找到make的路径,需要点击Add Entery,手动添加CMAKE_MAKE_PROGRAM。类的的其他问题,也可以手动添加相关路径之后再重试。此时最好同时勾选Grouped和Advanced复选框后面有用。如下所示,然后点击Configure,如果不成功就重启下Cmake。
3、Configure完成后,在UA根节点下选择UA_ENABLE_AMALGAMATION,这是生成单独源文件和头文件的命令;
4. 点击Generate就会生成Makefile文件。
5、在build_mingw文件夹下执行make命令进行编译。
四、用Qt调用open62541
在windows下就用CMake生成VS版本比较好,一些windows依赖库也就自己加到工程里去了,如果要用Qt,注意以下几点:
1. 用Qt5建立Non-Qt-Project / C++ Plain Application工程,Qt6目前测试有问题;
2. 要用qMake,这样不用写cMake代码配置工程;
3. 我们将上一节生成的.h和.a及.c文件放到open62541文件夹中并拷贝到工程目录下,在工程中添加库.a和.h文件(或者.c搭配.h文件,这种方式可以改一些配置。另外,在windows下基于mingw编译的.a静态库也能执行,不过要跨平台,在Linux下仍然需要重新编译.a静态库);
4. 如果有using namespace std,要放到 #include "open62541/open62541.h" 上面;
5. 需要在.pro中加入LIBS += -lpthread libwsock32 libws2_32。
以下给一个Server的例子,该例子来自于源码根目录下的examples\tutorial_server_variable.c,只是我们已经生成了open62541.h就可以干掉源码里的以下两句:
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
用一句 #include "open62541/open62541.h"代替就好了,免得有其他引用问题。
/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
* See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
/**
* Adding Variables to a Server
* ----------------------------
*
* This tutorial shows how to work with data types and how to add variable nodes
* to a server. First, we add a new variable to the server. Take a look at the
* definition of the ``UA_VariableAttributes`` structure to see the list of all
* attributes defined for VariableNodes.
*
* Note that the default settings have the AccessLevel of the variable value as
* read only. See below for making the variable writable.
*/
//#include <open62541/plugin/log_stdout.h>
//#include <open62541/server.h>
//#include <open62541/server_config_default.h>
#include "../open62541/open62541.h"
#include <signal.h>
#include <stdlib.h>
static void
addVariable(UA_Server *server) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = 42;
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Add the variable node to the information model */
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
}
static void
addMatrixVariable(UA_Server *server) {
UA_VariableAttributes attr = UA_VariableAttributes_default;
attr.displayName = UA_LOCALIZEDTEXT("en-US", "Double Matrix");
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Set the variable value constraints */
attr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
UA_UInt32 arrayDims[2] = {2,2};
attr.arrayDimensions = arrayDims;
attr.arrayDimensionsSize = 2;
/* Set the value. The array dimensions need to be the same for the value. */
UA_Double zero[4] = {0.0, 0.0, 0.0, 0.0};
UA_Variant_setArray(&attr.value, zero, 4, &UA_TYPES[UA_TYPES_DOUBLE]);
attr.value.arrayDimensions = arrayDims;
attr.value.arrayDimensionsSize = 2;
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "double.matrix");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "double matrix");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
attr, NULL, NULL);
}
/**
* Now we change the value with the write service. This uses the same service
* implementation that can also be reached over the network by an OPC UA client.
*/
static void
writeVariable(UA_Server *server) {
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
/* Write a different integer value */
UA_Int32 myInteger = 43;
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
UA_Server_writeValue(server, myIntegerNodeId, myVar);
/* Set the status code of the value to an error code. The function
* UA_Server_write provides access to the raw service. The above
* UA_Server_writeValue is syntactic sugar for writing a specific node
* attribute with the write service. */
UA_WriteValue wv;
UA_WriteValue_init(&wv);
wv.nodeId = myIntegerNodeId;
wv.attributeId = UA_ATTRIBUTEID_VALUE;
wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
wv.value.hasStatus = true;
UA_Server_write(server, &wv);
/* Reset the variable to a good statuscode with a value */
wv.value.hasStatus = false;
wv.value.value = myVar;
wv.value.hasValue = true;
UA_Server_write(server, &wv);
}
/**
* Note how we initially set the DataType attribute of the variable node to the
* NodeId of the Int32 data type. This forbids writing values that are not an
* Int32. The following code shows how this consistency check is performed for
* every write.
*/
static void
writeWrongVariable(UA_Server *server) {
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
/* Write a string */
UA_String myString = UA_STRING("test");
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
}
/** It follows the main server code, making use of the above definitions. */
static volatile UA_Boolean running = true;
static void stopHandler(int sign) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
UA_ServerConfig* config = UA_Server_getConfig(server);
config->verifyRequestTimestamp = UA_RULEHANDLING_ACCEPT;
#ifdef UA_ENABLE_WEBSOCKET_SERVER
UA_ServerConfig_addNetworkLayerWS(UA_Server_getConfig(server), 7681, 0, 0, NULL, NULL);
#endif
addVariable(server);
addMatrixVariable(server);
writeVariable(server);
writeWrongVariable(server);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
至此,你发现已经可以运行了,下载一个UaExpert当做客户端来测一下吧。