在博文【opcua】从编译文件到客户端的收发、断连、节点查询等实现 中,我们已经介绍了如何在QT 中创建opucaClient 。在本期的博文中,我们基于之前的部署环境,介绍一下如何构建opcuaServer,所用的第三方库依旧是open62541。
OPC UA(Open Platform Communications Unified Architecture)是一种开放的工业通信标准,用于在各种工业自动化系统和设备之间进行通信和数据交换。OPC UA Server是一个实现了OPC UA协议的服务器,用于接收、处理和传输数据。它可以提供一个统一的接口,使不同类型的设备和系统能够相互通信。
OPC UA Server具有以下特点和功能:
OPC UA Server可以用于各种工业应用领域,包括制造业、能源管理、楼宇自动化、物联网等。它为不同的设备和系统提供了统一的数据通信和集成方案,促进了工业自动化的发展和应用。
我们基于官方示例server.cpp,在qt中进行QMainWindow 的创建,(由于是基于之前客户端的文件中进行再次创建,因此需要在main.cpp 文件中切换新的cpp 文件)
#include "mainwindow.h"
#include "opcserversht.h"
#include
#include "QJsonObject"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//client
// MainWindow w;
// w.show();
//server
opcserversht w;
w.show();
return a.exec();
}
然后就可以在opcserversht 类中进行服务端创建,代码如下:
#include "opcserversht.h"
#include "ui_opcserversht.h"
#include
#include
#include
//static void ostopHandler(int sign) {
// UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
//}
opcserversht::opcserversht(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::opcserversht)
{
ui->setupUi(this);
//
// signal(SIGINT, stopHandler);
// signal(SIGTERM, stopHandler);
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
// add a variable node to the adresspace
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = 42;
UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(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_NULL, attr, NULL, NULL);
/* allocations on the heap need to be freed */
UA_VariableAttributes_clear(&attr);
UA_NodeId_clear(&myIntegerNodeId);
UA_QualifiedName_clear(&myIntegerName);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
qDebug() << (retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE);
}
opcserversht::~opcserversht()
{
delete ui;
}