1.准备源码

2.Windows下编译

需要先安装python3

用管理员身份运行x64 Native Tools Command Prompt for VS

进入源码目录

创建build目录

mkdir build

进入build目录

cd build

配置

cmake .. -G Ninja -DBUILD_SHARED_LIBS=OFF

编译

ninja

安装

ninja install

静态库文件在C:\Program Files (x86)\open62541\lib下

3.Linux下编译

进入源码目录

新建build目录

进入build目录

配置

cmake .. -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release

编译

cmake --build . -j$(nproc)

安装

sudo cmake --install .

4.程序测试

TypeDef.h

#pragma once
#include <open62541/client.h>

typedef enum {
	NODE_TYPE_BOOL = 0,
	NODE_TYPE_SBYTE = 1,
	NODE_TYPE_BYTE = 2,
	NODE_TYPE_INT16 = 3,
	NODE_TYPE_UINT16 = 4,
	NODE_TYPE_INT32 = 5,
	NODE_TYPE_UINT32 = 6,
	NODE_TYPE_INT64 = 7,
	NODE_TYPE_UINT64 = 8,
	NODE_TYPE_FLOAT = 9,
	NODE_TYPE_DOUBLE = 10,
	NODE_TYPE_STRING = 11,
	Node_TYPE_FOLDER = 998,
	OPC_TYPE_UNKNOWN = 999
} OpcDataType;


typedef struct ClientContext {
	UA_Client* client;
} ClientContext;
typedef void* OpcClientHandle;

OpcUaClient.h

#pragma once
#include "TypeDef.h"
#include <open62541/client.h>
#include <open62541/client_highlevel.h>
#include <string>
#include <thread>
#include <atomic>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;

class OpcUaClient
{
public:
	OpcUaClient();
	void Connect(string endPointUrl);

	/**
	 * 泛型,添加节点 返回添加成功还是失败,模板实现必须放头文件
	 */
	template<typename T>
	bool Write(string nodeIdStr, T value) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_BOOL, &value);
		}
		else if constexpr (is_same_v<T, int8_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_SBYTE, &value);
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_BYTE, &value);
		}
		else if constexpr (is_same_v<T, int16_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT16, &value);
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT16, &value);
		}
		else if constexpr (is_same_v<T, int32_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT32, &value);
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT32, &value);
		}
		else if constexpr (is_same_v<T, int64_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT64, &value);
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT64, &value);
		}
		else if constexpr (is_same_v<T, float>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_FLOAT, &value);
		}
		else if constexpr (is_same_v<T, double>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_DOUBLE, &value);
		}
		else if constexpr (is_same_v<T, string>) {
			ret = OpcUa_Client_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_STRING, (void*)value.c_str());
		}
		else {
			//不支持的类型
		}
		return ret == 0;
	}

	/**
	 * 泛型,添加节点 返回添加成功还是失败,模板实现必须放头文件
	 */
	template<typename T>
	T Read(string nodeIdStr) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			bool* v = new bool;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_BOOL, (uint8_t*)v, sizeof(bool));
			bool value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");

			return value;
		}
		else if constexpr (is_same_v<T, int8_t>) {
			int8_t* v = new int8_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_SBYTE, (uint8_t*)v, sizeof(int8_t));
			int8_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			uint8_t* v = new uint8_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_BYTE, (uint8_t*)v, sizeof(uint8_t));
			uint8_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int16_t>) {
			int16_t* v = new int16_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT16, (uint8_t*)v, sizeof(int16_t));
			int16_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			uint16_t* v = new uint16_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT16, (uint8_t*)v, sizeof(uint16_t));
			uint16_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int32_t>) {
			int32_t* v = new int32_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT32, (uint8_t*)v, sizeof(int32_t));
			int32_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");

			return value;
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			uint32_t* v = new uint32_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT32, (uint8_t*)v, sizeof(uint32_t));
			uint32_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");

			return value;
		}
		else if constexpr (is_same_v<T, int64_t>) {
			int64_t* v = new int64_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT64, (uint8_t*)v, sizeof(int64_t));
			int64_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			uint64_t* v = new uint64_t;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT64, (uint8_t*)v, sizeof(uint64_t));
			uint64_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, float>) {
			float* v = new float;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_FLOAT, (uint8_t*)v, sizeof(float));
			float value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, double>) {
			double* v = new double;
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_DOUBLE, (uint8_t*)v, sizeof(double));
			double value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, string>) {
			char str2[1024];
			ret = OpcUa_Client_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_STRING, (uint8_t*)str2, 1024);
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return str2;
		}
		else {
			throw invalid_argument("不支持的类型");
		}
	}

	void Disconnect();
	virtual ~OpcUaClient();
private:
	OpcClientHandle _handle;
	OpcClientHandle Opc_CreateClient(const char* endpointUrl);
	void Opc_CloseClient();
	int OpcUa_Client_WriteVariable(const char* nodeIdStr, OpcDataType type, void* data);
	int OpcUa_Client_ReadVariable(const char* nodeIdStr, OpcDataType type, uint8_t* outBuf, int buffSize);
};

OpcUaClient.cpp

#include "OpcUaClient.h"

#include <open62541/client_config_default.h>

OpcUaClient::OpcUaClient() {
	// TODO Auto-generated constructor stub
}

OpcUaClient::~OpcUaClient() {
	Disconnect();
}

void OpcUaClient::Connect(string endPointUrl) {
	_handle = Opc_CreateClient(endPointUrl.c_str());
}

void OpcUaClient::Disconnect() {
	Opc_CloseClient();
}

OpcClientHandle OpcUaClient::Opc_CreateClient(const char* endpointUrl) {
	UA_Client* client = UA_Client_new();
	UA_ClientConfig_setDefault(UA_Client_getConfig(client));

	UA_StatusCode status = UA_Client_connect(client, endpointUrl);
	if (status != UA_STATUSCODE_GOOD) {
		UA_Client_delete(client);
		return nullptr;
	}
	ClientContext* ctx = new ClientContext();
	ctx->client = client;
	return (OpcClientHandle)ctx;
}
void OpcUaClient::Opc_CloseClient() {
	if (!_handle)
		return;
	ClientContext* ctx = (ClientContext*)_handle;
	UA_Client_disconnect(ctx->client);
	UA_Client_delete(ctx->client);
	delete ctx;
	_handle = nullptr;
}
int OpcUaClient::OpcUa_Client_WriteVariable(const char* nodeIdStr, OpcDataType type, void* data) {
	if (!_handle)
		return -1;
	ClientContext* ctx = (ClientContext*)_handle;
	// 1. 解析 NodeId (假设是字符串格式,如 "ns=2;s=MyVar")
	// 实际生产中可能需要更复杂的解析,这里简化处理
	UA_NodeId nodeId;
	// 使用字符串解析,支持 "ns=2;s=MyNode" 格式
	UA_StatusCode status = UA_NodeId_parse(&nodeId,
		UA_STRING((char*)nodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		return -2;
	}
	UA_Variant variant;
	UA_Variant_init(&variant);
	UA_String strVal;

	char* str;
	switch (type) {
	case NODE_TYPE_BOOL:
	case NODE_TYPE_SBYTE:
	case NODE_TYPE_BYTE:
	case NODE_TYPE_INT16:
	case NODE_TYPE_UINT16:
	case NODE_TYPE_INT32:
	case NODE_TYPE_UINT32:
	case NODE_TYPE_INT64:
	case NODE_TYPE_UINT64:
	case NODE_TYPE_FLOAT:
	case NODE_TYPE_DOUBLE:
		UA_Variant_setScalarCopy(&variant, (void*)data, &UA_TYPES[type]);
		break;
	case NODE_TYPE_STRING:
		str = (char*)data;
		strVal = UA_STRING_ALLOC(str); // 使用 ALLOC 宏分配并复制内存
		UA_Variant_setScalarCopy(&variant, &strVal, &UA_TYPES[type]);
		break;
	default:
		return -2; // 不支持的类型
	}
	UA_StatusCode ret = UA_Client_writeValueAttribute(ctx->client, nodeId, &variant);
	UA_NodeId_clear(&nodeId);
	UA_Variant_clear(&variant);
	return (int)ret;
}
int OpcUaClient::OpcUa_Client_ReadVariable(const char* nodeIdStr, OpcDataType type, uint8_t* outBuf, int buffSize) {
	if (!_handle)
		return -1;
	ClientContext* ctx = (ClientContext*)_handle;
	UA_NodeId nodeId;
	UA_StatusCode status = UA_NodeId_parse(&nodeId, UA_STRING((char*)nodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		return -2;
	}
	UA_Variant val;
	UA_Variant_init(&val);
	UA_StatusCode ret = UA_Client_readValueAttribute(ctx->client, nodeId, &val);
	UA_NodeId_clear(&nodeId);
	if (ret != UA_STATUSCODE_GOOD) {
		UA_Variant_clear(&val);
		return (int)ret;
	}
	uint32_t outTypeId = (uint32_t)(val.type - UA_TYPES);
	if (outTypeId != (uint32_t)type) {
		UA_Variant_clear(&val);
		return -3;
	}
	if (val.type == &UA_TYPES[UA_TYPES_STRING]) {
		UA_String* uaStr = (UA_String*)val.data;
		int lenToCopy =
			(uaStr->length < (unsigned int)(buffSize - 1)) ?
			uaStr->length : (buffSize - 1);
		if (lenToCopy > 0) {
			memcpy(outBuf, uaStr->data, lenToCopy);
		}
		outBuf[lenToCopy] = '\0';
	}
	else {
		// Int32 / Double / Bool 等值类型
		size_t sz = val.type->memSize;
		memcpy(outBuf, val.data, sz);
	}
	UA_Variant_clear(&val);
	return (int)ret;
}

OpcUaServer.h

#pragma once


#include "TypeDef.h"
#include <open62541/server.h>
#include <string>
#include <thread>
#include <atomic>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
class OpcUaServer
{
public:
	uint16_t parentId = 0;
	OpcUaServer();
	void Start(int port);

	bool AddRootFolder(string nodeName);
	bool AddFolder(string parentNodeId, string nodeName);

	template<typename T>
	bool AddNode_Description(string parentNodeIdStr, string dispalyName, string description, T value) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_BOOL, &value);
		}
		else if constexpr (is_same_v<T, int8_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_SBYTE, &value);
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_BYTE, &value);
		}
		else if constexpr (is_same_v<T, int16_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_INT16, &value);
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_UINT16, &value);
		}
		else if constexpr (is_same_v<T, int32_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_INT32, &value);
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_UINT32, &value);
		}
		else if constexpr (is_same_v<T, int64_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_INT64, &value);
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_UINT64, &value);
		}
		else if constexpr (is_same_v<T, float>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_FLOAT, &value);
		}
		else if constexpr (is_same_v<T, double>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_DOUBLE, &value);
		}
		else if constexpr (is_same_v<T, string>) {
			ret = Opc_AddNode_Description(parentNodeIdStr.c_str(), dispalyName.c_str(), description.c_str(), NODE_TYPE_STRING,
				(void*)value.c_str());
		}
		else {
			//不支持的类型
		}
		return ret == 0;
	}

	/**
	 * 泛型,添加节点 返回添加成功还是失败,模板实现必须放头文件
	 */
	template<typename T>
	bool AddNode(string parentId, string displayName, T value) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_BOOL, &value);
		}
		else if constexpr (is_same_v<T, int8_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_SBYTE, &value);
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_BYTE, &value);
		}
		else if constexpr (is_same_v<T, int16_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_INT16, &value);
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_UINT16, &value);
		}
		else if constexpr (is_same_v<T, int32_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_INT32, &value);
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_UINT32, &value);
		}
		else if constexpr (is_same_v<T, int64_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_INT64, &value);
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_UINT64, &value);
		}
		else if constexpr (is_same_v<T, float>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_FLOAT, &value);
		}
		else if constexpr (is_same_v<T, double>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_DOUBLE, &value);
		}
		else if constexpr (is_same_v<T, string>) {
			ret = Opc_AddNode(parentId.c_str(), displayName.c_str(), NODE_TYPE_STRING, (void*)value.c_str());
		}
		else {
			//不支持的类型
		}
		return ret == 0;
	}

	/**
	 * 泛型,添加节点 返回添加成功还是失败,模板实现必须放头文件
	 */
	template<typename T>
	bool Write(string nodeIdStr, T value) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_BOOL, &value);
		}
		else if constexpr (is_same_v<T, int8_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_SBYTE, &value);
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_BYTE, &value);
		}
		else if constexpr (is_same_v<T, int16_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT16, &value);
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT16, &value);
		}
		else if constexpr (is_same_v<T, int32_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT32, &value);
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT32, &value);
		}
		else if constexpr (is_same_v<T, int64_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_INT64, &value);
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_UINT64, &value);
		}
		else if constexpr (is_same_v<T, float>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_FLOAT, &value);
		}
		else if constexpr (is_same_v<T, double>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_DOUBLE, &value);
		}
		else if constexpr (is_same_v<T, string>) {
			ret = OpcUa_Server_WriteVariable(nodeIdStr.c_str(), NODE_TYPE_STRING, (void*)value.c_str());
		}
		else {
			//不支持的类型
		}
		return ret == 0;
	}

	/**
	 * 泛型,添加节点 返回添加成功还是失败,模板实现必须放头文件
	 */
	template<typename T>
	T Read(string nodeIdStr) {
		int ret = -1;
		if constexpr (is_same_v<T, bool>) {
			bool* v = new bool;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_BOOL, (uint8_t*)v, sizeof(bool));
			bool value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int8_t>) {
			int8_t* v = new int8_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_SBYTE, (uint8_t*)v, sizeof(int8_t));
			int8_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint8_t>) {
			uint8_t* v = new uint8_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_BYTE, (uint8_t*)v, sizeof(uint8_t));
			uint8_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int16_t>) {
			int16_t* v = new int16_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT16, (uint8_t*)v, sizeof(int16_t));
			int16_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint16_t>) {
			uint16_t* v = new uint16_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT16, (uint8_t*)v, sizeof(uint16_t));
			uint16_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int32_t>) {
			int32_t* v = new int32_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT32, (uint8_t*)v, sizeof(int32_t));
			int32_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint32_t>) {
			uint32_t* v = new uint32_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT32, (uint8_t*)v, sizeof(uint32_t));
			uint32_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, int64_t>) {
			int64_t* v = new int64_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_INT64, (uint8_t*)v, sizeof(int64_t));
			int64_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, uint64_t>) {
			uint64_t* v = new uint64_t;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_UINT64, (uint8_t*)v, sizeof(uint64_t));
			uint64_t value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, float>) {
			float* v = new float;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_FLOAT, (uint8_t*)v, sizeof(float));
			float value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, double>) {
			double* v = new double;
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_DOUBLE, (uint8_t*)v, sizeof(double));
			double value = *v;
			delete v;
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return value;
		}
		else if constexpr (is_same_v<T, string>) {
			char str2[1024];
			ret = OpcUa_Server_ReadVariable(nodeIdStr.c_str(), NODE_TYPE_STRING, (uint8_t*)str2, 1024);
			if (ret != 0)
				throw invalid_argument("不支持的类型");
			return str2;
		}
		else {
			throw invalid_argument("不支持的类型");
		}
	}

	void Stop();
	virtual ~OpcUaServer();
	static void ThreadRun(OpcUaServer* ptr) {
		// UA_Server_run 是阻塞的,它会一直运行直到 UA_Server_run_shutdown 被调用
		// 第二个参数 &running 用于控制循环,但 UA_Server_run 内部处理得比较好
		// 我们这里简单地让它运行,直到 shutdown 被调用
		UA_StatusCode status = UA_Server_run(ptr->g_server, &ptr->ua_running);

		if (status != UA_STATUSCODE_GOOD) {
			// 可以在这里添加日志
		}
		// 循环结束后,重置状态
		ptr->g_running = false;
	}

private:
	thread g_serverThread;       // 服务器运行线程
	bool g_running = false; // 运行状态标志
	bool ua_running = false;
	UA_Server* g_server = nullptr;
	/* ========== 服务端  ========== */
	int Opc_StartServer(int port);
	void Opc_StopServer();

	int Opc_AddNode(const char* parentNodeIdStr, const char* nodeIdStr, const char* displayName, OpcDataType type, void* data);
	int Opc_AddNode(const char* parentNodeIdStr, const char* displayName, OpcDataType type, void* data);
	int Opc_AddNode_Description(const char* parentNodeIdStr, const char* nodeIdStr, const char* displayName, const char* description, OpcDataType type, void* data);
	int Opc_AddNode_Description(const char* parentNodeIdStr, const char* displayName, const char* description, OpcDataType type, void* data);
	int OpcUa_Server_WriteVariable(const char* nodeIdStr, OpcDataType type, void* data);
	int OpcUa_Server_ReadVariable(const char* nodeIdStr, OpcDataType type, uint8_t* outBuf, int buffSize);

};

OpcUaServer.cpp

#include "OpcUaServer.h"
#include <open62541/types.h>
#include <open62541/server_config_default.h>
OpcUaServer::OpcUaServer() {
	// TODO Auto-generated constructor stub
}

OpcUaServer::~OpcUaServer() {
	Stop();
}

void OpcUaServer::Start(int port = 4840) {
	Opc_StartServer(port);
}

int OpcUaServer::Opc_StartServer(int port) {
	// 1. 检查是否已经在运行
	if (g_running) {
		return -1; // 服务器已在运行
	}


	g_server = UA_Server_new();
	if (!g_server) {
		return -2; // 创建失败
	}
	UA_ServerConfig* config = UA_Server_getConfig(g_server);
	UA_StatusCode status = UA_ServerConfig_setMinimal(config, port, NULL);
	if (status != UA_STATUSCODE_GOOD) {
		UA_Server_delete(g_server);
		g_server = nullptr;
		return -2;
	}
	g_running = true;      // 设置 C++ 逻辑状态
	ua_running = true;     // 设置 open62541 运行状态
	g_serverThread = thread(OpcUaServer::ThreadRun, this);
	// 5. 分离线程
	g_serverThread.detach();
	return 0; // 启动成功
}

void OpcUaServer::Opc_StopServer() {
	if (!g_running) {
		return;
	}
	// 1. 通知停止 (同时更新两个标志位)
	g_running = false;  // C++ 逻辑停止
	ua_running = false; // open62541 停止循环
	// 2. 调用 shutdown 唤醒主循环
	if (g_server) {
		UA_Server_run_shutdown(g_server);
	}
	// 3. 等待线程结束
	if (g_serverThread.joinable()) {
		g_serverThread.join();
	}
	// 4. 删除服务器实例
	if (g_server) {
		UA_Server_delete(g_server);
		g_server = nullptr;
	}
}

bool OpcUaServer::AddRootFolder(string nodeName) {
	string parentNodeId = "ns=0;i=85";
	string newNode = "ns=1;s=" + nodeName;
	int ret = Opc_AddNode(parentNodeId.c_str(), newNode.c_str(), nodeName.c_str(), Node_TYPE_FOLDER, NULL);
	return ret == 0;
}
bool OpcUaServer::AddFolder(string parentNodeId, string nodeName) {
	string newNode = parentNodeId + "." + nodeName;
	int ret = Opc_AddNode(parentNodeId.c_str(), newNode.c_str(), nodeName.c_str(), Node_TYPE_FOLDER, NULL);
	return ret == 0;
}

int OpcUaServer::Opc_AddNode_Description(const char* parentNodeIdStr, const char* nodeIdStr, const char* displayName, const char* description, OpcDataType type,
	void* data) {
	if (!g_server) {
		return -1; // 服务器未启动
	}
	UA_NodeId parentNodeId;
	UA_StatusCode status = UA_NodeId_parse(&parentNodeId, UA_STRING((char*)parentNodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		UA_NodeId_clear(&parentNodeId);
		return -2;
	}
	UA_NodeId varNodeId;
	status = UA_NodeId_parse(&varNodeId, UA_STRING((char*)nodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		UA_NodeId_clear(&varNodeId);
		UA_NodeId_clear(&parentNodeId);
		return -3;
	}
	UA_NodeId refTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
	if (type == Node_TYPE_FOLDER) {
		UA_ObjectAttributes attr1 = UA_ObjectAttributes_default;
		attr1.displayName = UA_LOCALIZEDTEXT_ALLOC("zh-CN", displayName);
		attr1.description = UA_LOCALIZEDTEXT_ALLOC("zh-CN", displayName);
		UA_QualifiedName qn = UA_QUALIFIEDNAME(varNodeId.namespaceIndex, (char*)nodeIdStr);
		UA_StatusCode ret = UA_Server_addObjectNode(g_server, varNodeId,
			parentNodeId, refTypeId, qn, UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), attr1,
			NULL,
			NULL);
		//UA_QualifiedName_clear(&qn);
//		UA_ObjectAttributes_clear(&objAttr);
		UA_NodeId_clear(&varNodeId);
		UA_NodeId_clear(&parentNodeId);

		return (int)ret;
	}
	else {
		UA_Variant variant;
		UA_Variant_init(&variant);

		// 2. 根据类型设置初始值和 DataType
		UA_String strVal;
		char* str;
		switch (type) {
		case NODE_TYPE_BOOL:
		case NODE_TYPE_SBYTE:
		case NODE_TYPE_BYTE:
		case NODE_TYPE_INT16:
		case NODE_TYPE_UINT16:
		case NODE_TYPE_INT32:
		case NODE_TYPE_UINT32:
		case NODE_TYPE_INT64:
		case NODE_TYPE_UINT64:
		case NODE_TYPE_FLOAT:
		case NODE_TYPE_DOUBLE:
			UA_Variant_setScalarCopy(&variant, (void*)data, &UA_TYPES[type]);
			break;
		case NODE_TYPE_STRING:
			str = (char*)data;
			strVal = UA_String_fromChars(str);
			UA_Variant_setScalarCopy(&variant, &strVal, &UA_TYPES[type]);
			break;
		default:
			return -2; // 不支持的类型
		}
		UA_VariableAttributes attr = UA_VariableAttributes_default;
		attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", displayName);
		attr.value = variant;
		attr.description = UA_LOCALIZEDTEXT_ALLOC("zh-CN", description);
		attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE; // 可读写
		attr.userAccessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE; // 可读写
		UA_QualifiedName qn = UA_QUALIFIEDNAME(varNodeId.namespaceIndex, (char*)nodeIdStr);
		UA_StatusCode ret = UA_Server_addVariableNode(g_server, varNodeId,
			parentNodeId, refTypeId, qn, UA_NODEID_NULL, attr,
			NULL,
			NULL);

		//UA_QualifiedName_clear(&qn);
		UA_NodeId_clear(&varNodeId);
		UA_NodeId_clear(&parentNodeId);
		return (int)ret;
	}
}

int OpcUaServer::Opc_AddNode_Description(const char* parentNodeIdStr, const char* displayName, const char* description, OpcDataType type, void* data) {
	string p = parentNodeIdStr;
	string n = displayName;
	string nodeIdStr = p + "." + n;
	return Opc_AddNode_Description(parentNodeIdStr, nodeIdStr.c_str(), displayName, description, type, data);
}

int OpcUaServer::Opc_AddNode(const char* parentNodeIdStr, const char* nodeIdStr, const char* displayName, OpcDataType type, void* data) {
	return Opc_AddNode_Description(parentNodeIdStr, nodeIdStr, displayName, "", type, data);
}

int OpcUaServer::Opc_AddNode(const char* parentNodeIdStr, const char* displayName, OpcDataType type, void* data) {
	string p = parentNodeIdStr;
	string n = displayName;
	string nodeIdStr = p + "." + n;
	return Opc_AddNode(parentNodeIdStr, nodeIdStr.c_str(), displayName, type, data);
}
int OpcUaServer::OpcUa_Server_WriteVariable(const char* nodeIdStr,
	OpcDataType type, void* data) {
	if (!g_server) {
		return -1; // 服务器未启动
	}
	UA_NodeId nodeId;
	UA_StatusCode status = UA_NodeId_parse(&nodeId,
		UA_STRING((char*)nodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		return -2;
	}
	UA_Variant variant;
	UA_Variant_init(&variant);

	UA_String strVal;

	char* str;

	switch (type) {
	case NODE_TYPE_BOOL:
	case NODE_TYPE_SBYTE:
	case NODE_TYPE_BYTE:
	case NODE_TYPE_INT16:
	case NODE_TYPE_UINT16:
	case NODE_TYPE_INT32:
	case NODE_TYPE_UINT32:
	case NODE_TYPE_INT64:
	case NODE_TYPE_UINT64:
	case NODE_TYPE_FLOAT:
	case NODE_TYPE_DOUBLE:
		UA_Variant_setScalarCopy(&variant, (void*)data, &UA_TYPES[type]);
		break;
	case NODE_TYPE_STRING:
		str = (char*)data;
		strVal = UA_STRING_ALLOC(str); // 使用 ALLOC 宏分配并复制内存
		UA_Variant_setScalarCopy(&variant, &strVal, &UA_TYPES[type]);
		break;
	default:
		return -2; // 不支持的类型
	}
	UA_StatusCode ret = UA_Server_writeValue(g_server, nodeId, variant);
	UA_NodeId_clear(&nodeId);
	UA_Variant_clear(&variant);

	return (int)ret;
}
int OpcUaServer::OpcUa_Server_ReadVariable(const char* nodeIdStr,
	OpcDataType type, uint8_t* outBuf, int buffSize) {
	if (!g_server) {
		return -1; // 服务器未启动
	}
	UA_NodeId nodeId;
	UA_StatusCode status = UA_NodeId_parse(&nodeId,
		UA_STRING((char*)nodeIdStr));
	if (status != UA_STATUSCODE_GOOD) {
		return -2;
	}
	UA_Variant val;
	UA_Variant_init(&val);
	UA_StatusCode ret = UA_Server_readValue(g_server, nodeId, &val);
	UA_NodeId_clear(&nodeId);
	if (ret != UA_STATUSCODE_GOOD) {
		UA_Variant_clear(&val);
		return (int)ret;
	}
	uint32_t outTypeId = (uint32_t)(val.type - UA_TYPES);
	if (outTypeId != (uint32_t)type) {
		UA_Variant_clear(&val);
		return -3;
	}
	if (val.type == &UA_TYPES[UA_TYPES_STRING]) {
		UA_String* uaStr = (UA_String*)val.data;
		int lenToCopy =
			(uaStr->length < (unsigned int)(buffSize - 1)) ?
			uaStr->length : (buffSize - 1);
		if (lenToCopy > 0) {
			memcpy(outBuf, uaStr->data, lenToCopy);
		}
		outBuf[lenToCopy] = '\0';
	}
	else {
		// Int32 / Double / Bool 等值类型
		size_t sz = val.type->memSize;
		memcpy(outBuf, val.data, sz);
	}
	UA_Variant_clear(&val);
	return (int)ret;
}
void OpcUaServer::Stop() {
	Opc_StopServer();
}

OpcUaTest.h

#pragma once
#include"OpcUaServer.h"
#include "OpcUaClient.h"
class OpcUaTest
{
public:
	void TestStart() {
		ServerTest();
		Sleep(1000);
		ClientTest();
	}
	void TestStop() {
		ClientStop();
		Sleep(2000);
		ServerStop();
	}
private:
	void ServerTest();
	void Sleep(int ms);
	void ClientTest();
	void ServerStop();
	void ClientStop();
	bool stop = false;
	OpcUaServer server;
	OpcUaClient client;
	static  void ServerTestFunc(OpcUaTest* ptr);
	static void ClientTestFunc(OpcUaTest* ptr);
};

OpcUaTest.cpp

#include "OpcUaTest.h"
#include<string>
#include <thread>
using namespace std;
void OpcUaTest::ServerTest() {
	//开启线程启动
	thread thread = std::thread(OpcUaTest::ServerTestFunc, this);
	// 5. 分离线程
	thread.detach();

}
void OpcUaTest::ServerTestFunc(OpcUaTest* ptr) {
	cout << "=== OPC UA Wrapper Test ===" << endl;
	//开启服务
	ptr->server.Start(4840);
	ptr->server.AddRootFolder("CuttingHMI");
	ptr->server.AddFolder("ns=1;s=CuttingHMI", "TestFolder1");
	ptr->server.AddNode_Description("ns=1;s=CuttingHMI.TestFolder1", "TestBool11", "测试bool", (bool)true);
	//1.TestBool
	ptr->server.AddNode_Description("ns=1;s=CuttingHMI", "TestBool", "测试bool", (bool)true);
	//2.TestSByte
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestSbyte", (int8_t)-128);
	//3.TestByte
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestByte", (uint8_t)127);
	//4.TestInt16
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestInt16", (int16_t)-8010);
	//5.TestUInt16
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestUInt16", (uint16_t)-8011);
	//6.TestInt32
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestInt32", (int32_t)-10086);
	//7.TestUInt32
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestUInt32", (uint32_t)10010);
	//8.TestInt64
	ptr->server.AddNode_Description("ns=1;s=CuttingHMI", "TestInt64", "测试Int64", (int64_t)-3333333);
	//9.TestUInt64
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestUInt64", (uint64_t)4444444);
	//10.TestFloat
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestFloat", (float)3.14);
	//11.TestDouble
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestDouble", (double)3.141592654);
	//12.TestString
	ptr->server.AddNode("ns=1;s=CuttingHMI", "TestString", (string)"HelloWorld");

	//服务器写入测试
	cout << "服务端写入数据" << endl;
	//1.Bool
	ptr->server.Write("ns=1;s=CuttingHMI.TestBool", (bool)false);
	//2.Sbyte
	ptr->server.Write("ns=1;s=CuttingHMI.TestSbyte", (int8_t)-126);
	//3.byte
	ptr->server.Write("ns=1;s=CuttingHMI.TestByte", (uint8_t)125);
	//4.TestInt16
	ptr->server.Write("ns=1;s=CuttingHMI.TestInt16", (int16_t)111);
	//5.TestUInt16
	ptr->server.Write("ns=1;s=CuttingHMI.TestUInt16", (uint16_t)222);
	//6.TestInt32
	ptr->server.Write("ns=1;s=CuttingHMI.TestInt32", (int32_t)333);
	//7.TestUInt32
	ptr->server.Write("ns=1;s=CuttingHMI.TestUInt32", (uint32_t)444);
	//8.TestInt64
	ptr->server.Write("ns=1;s=CuttingHMI.TestInt64", (int64_t)555);
	//9.TestUInt64
	ptr->server.Write("ns=1;s=CuttingHMI.TestUInt64", (uint64_t)666);
	//10.TestFloat
	ptr->server.Write("ns=1;s=CuttingHMI.TestFloat", (float)333.1415);
	//11.TestDouble
	ptr->server.Write("ns=1;s=CuttingHMI.TestDouble", (double)444.151692654);
	//12.TestString
	ptr->server.Write("ns=1;s=CuttingHMI.TestString", "ABCDEFGGGGG");

	cout << "服务端读取数据" << endl;
	//1.Bool
	bool boolValue2 = ptr->server.Read<bool>("ns=1;s=CuttingHMI.TestBool");
	cout << "服务端读取到的bool值:" << (boolValue2 ? "True" : "False") << endl;
	//2.Sbyte
	int8_t sbyteValue2 = ptr->server.Read<int8_t>("ns=1;s=CuttingHMI.TestSbyte");
	cout << "服务端读取到的sbyte值:" << (int)sbyteValue2 << endl;
	//3.byte
	uint8_t byteValue2 = ptr->server.Read<uint8_t>("ns=1;s=CuttingHMI.TestByte");
	cout << "服务端读取到的byte值:" << (int)byteValue2 << endl;
	//4.TestInt16
	int16_t int16Value2 = ptr->server.Read<int16_t>("ns=1;s=CuttingHMI.TestInt16");
	cout << "服务端读取到的int16值:" << int16Value2 << endl;
	//5.TestUInt16
	uint16_t uint16Value2 = ptr->server.Read<uint16_t>("ns=1;s=CuttingHMI.TestUInt16");
	cout << "服务端读取到的uint16值:" << uint16Value2 << endl;
	//6.TestInt32
	int32_t int32Value2 = ptr->server.Read<int32_t>("ns=1;s=CuttingHMI.TestInt32");
	cout << "服务端读取到的int32值:" << int32Value2 << endl;
	//7.TestUInt32
	uint32_t uint32Value2 = ptr->server.Read<uint32_t>(
		"ns=1;s=CuttingHMI.TestUInt32");
	cout << "服务端读取到的uint32值:" << uint32Value2 << endl;
	//8.TestInt64
	int64_t int64Value2 = ptr->server.Read<int64_t>("ns=1;s=CuttingHMI.TestInt64");
	cout << "服务端读取到的int64值:" << int64Value2 << endl;
	//9.TestUInt64
	uint64_t uint64Value2 = ptr->server.Read<uint64_t>(
		"ns=1;s=CuttingHMI.TestUInt64");
	cout << "服务端读取到的uint64值:" << uint64Value2 << endl;
	//10.TestFloat
	float floatValue2 = ptr->server.Read<float>("ns=1;s=CuttingHMI.TestFloat");
	cout << "服务端读取到的float值:" << floatValue2 << endl;
	//11.TestDouble
	double doubleValue2 = ptr->server.Read<double>("ns=1;s=CuttingHMI.TestDouble");
	cout << "服务端读取到的double值:" << doubleValue2 << endl;
	//12.TestString ;
	string str2 = ptr->server.Read<string>("ns=1;s=CuttingHMI.TestString");
	cout << "服务端读取到的string值:" << str2 << endl;
}
void OpcUaTest::Sleep(int ms) {
	this_thread::sleep_for(chrono::milliseconds(ms));
}

void OpcUaTest::ServerStop() {
	cout << "服务端关闭" << endl;
	server.Stop();
}

void OpcUaTest::ClientTest() {
	//开启线程启动
	thread thread = std::thread(OpcUaTest::ClientTestFunc, this);
	// 5. 分离线程
	thread.detach();
}
void OpcUaTest::ClientTestFunc(OpcUaTest* ptr) {
	ptr->client.Connect("opc.tcp://127.0.0.1:4840");
	int index = 0;
	while (!ptr->stop) {
		index++;
		if (index > 65536) {
			index = 1;
		}
		bool boolValue2 = ptr->client.Read<bool>("ns=1;s=CuttingHMI.TestBool");
		cout << "客户端读取到的bool值:" << ((boolValue2) ? "True" : "False") << endl;
		boolValue2 = !boolValue2;
		ptr->client.Write("ns=1;s=CuttingHMI.TestBool", boolValue2);
		//2.Sbyte
		int8_t sbyteValue2 = ptr->client.Read<int8_t>("ns=1;s=CuttingHMI.TestSbyte");
		cout << "客户端读取到的sbyte值:" << (int)(sbyteValue2) << endl;
		sbyteValue2 = sbyteValue2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestSbyte", sbyteValue2);
		//3.byte
		uint8_t byteValue2 = ptr->client.Read<uint8_t>("ns=1;s=CuttingHMI.TestByte");
		cout << "客户端读取到的byte值:" << (int)(byteValue2) << endl;
		byteValue2 = byteValue2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestByte", byteValue2);
		//4.TestInt16
		int16_t int16Value2 = ptr->client.Read<int16_t>(
			"ns=1;s=CuttingHMI.TestInt16");
		cout << "客户端读取到的int16值:" << int16Value2 << endl;
		int16Value2 = (int16Value2)+1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestInt16", int16Value2);
		//5.TestUInt16
		uint16_t uint16Value2 = ptr->client.Read<uint16_t>(
			"ns=1;s=CuttingHMI.TestUInt16");
		cout << "客户端读取到的uint16值:" << uint16Value2 << endl;
		uint16Value2 = uint16Value2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestUInt16", uint16Value2);
		//6.TestInt32
		int32_t int32Value2 = ptr->client.Read<int32_t>(
			"ns=1;s=CuttingHMI.TestInt32");
		cout << "客户端读取到的int32值:" << int32Value2 << endl;
		int32Value2 = int32Value2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestInt32", int32Value2);
		//7.TestUInt32
		uint32_t uint32Value2 = ptr->client.Read<uint32_t>(
			"ns=1;s=CuttingHMI.TestUInt32");
		cout << "客户端读取到的uint32值:" << uint32Value2 << endl;
		uint32Value2 = uint32Value2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestUInt32", uint32Value2);
		//8.TestInt64
		int64_t int64Value2 = ptr->client.Read<int64_t>(
			"ns=1;s=CuttingHMI.TestInt64");
		cout << "客户端读取到的int64值:" << int64Value2 << endl;
		int64Value2 = int64Value2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestInt64", int64Value2);
		//9.TestUInt64
		uint64_t uint64Value2 = ptr->client.Read<uint64_t>(
			"ns=1;s=CuttingHMI.TestUInt64");
		cout << "客户端读取到的uint64值:" << uint64Value2 << endl;
		uint64Value2 = uint64Value2 + 1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestUInt64", uint64Value2);

		//10.TestFloat
		float floatValue2 = ptr->client.Read<float>("ns=1;s=CuttingHMI.TestFloat");
		cout << "客户端读取到的float值:" << floatValue2 << endl;
		floatValue2 = floatValue2 + 0.1;
		ptr->client.Write("ns=1;s=CuttingHMI.TestFloat", floatValue2);
		//11.TestDouble
		double doubleValue2 = ptr->client.Read<double>("ns=1;s=CuttingHMI.TestDouble");
		cout << "客户端读取到的double值:" << doubleValue2 << endl;
		doubleValue2 = doubleValue2 + 0.00011;
		ptr->client.Write("ns=1;s=CuttingHMI.TestDouble", doubleValue2);

		//12.TestString ;
		string str2 = ptr->client.Read<string>("ns=1;s=CuttingHMI.TestString");
		cout << "客户端读取到的string值:" << str2 << endl;
		string str3 = "HelloWorld" + to_string(index);
		ptr->client.Write("ns=1;s=CuttingHMI.TestString", str3);
		this_thread::sleep_for(chrono::milliseconds(100));
	}
	ptr->client.Disconnect();
	cout << "断开客户端连接" << endl;
}
void OpcUaTest::ClientStop() {
	stop = true;
}

main.cpp

#include<iostream>
#include"OpcUaTest.h"
using namespace std;
int main()
{
	OpcUaTest opcTest;
	opcTest.TestStart();
	int a;
	cin >> a;
	opcTest.TestStop();
	cout << "Hello World!" << endl;
	return 0;
}
分类: C++库

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注