用open62541建立一个opcua服务器,再添加一个模型。
在单独的线程中进行值修改,当客户端修改某个值时,服务端可以感知。
下载 open62541 并且编译、安装
git clone https://github.com/open62541/open62541.git
cd open62541
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release \
-DUA_ENABLE_AMALGAMATION=ON \
-DUA_BUILD_EXAMPLES=OFF
make -j && make install
opcua server 代码:
#include "open62541.h"
#include <iostream>
#include <string>
#include <thread>
using namespace std;
UA_Boolean running = true;
static void manuallyDefineStudent(UA_Server * server)
{
UA_NodeId studentId; /* get the nodeid assigned by the server */
UA_ObjectAttributes stuAttr = UA_ObjectAttributes_default;
stuAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Student");
UA_Server_addObjectNode(server, UA_NODEID_NULL,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
UA_QUALIFIEDNAME(1, "Student"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE),
stuAttr, NULL, &studentId);
// add name
UA_VariableAttributes nameAttr = UA_VariableAttributes_default;
UA_String studentName = UA_STRING("Xiao Ming");
UA_Variant_setScalar(&nameAttr.value, &studentName, &UA_TYPES[UA_TYPES_STRING]);
UA_NodeId nameNodeId = UA_NODEID_STRING(1, "name");
nameAttr.displayName = UA_LOCALIZEDTEXT("en-US", "Name");
UA_Server_addVariableNode(server, nameNodeId, studentId,
UA_NODEID_NUMERIC(0, UA_NS0ID_H