open62541 中 opc ua服务器创建 使用

注:

// opcua 基于 open62541 基础使用

#include 
#include "open62541/open62541.h"
#include 
#include 
#include 
#pragma comment(lib,"ws2_32.lib")    //加载ws2_32.dll的语句,必须要有,否则报错

using namespace std;

UA_Boolean running = true;
UA_Logger logger = UA_Log_Stdout;

static void stopHandler(int sign) {
	UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
	running = false;
}

1.最简单基础服务器

使用 open62541 创建服务器

int main() {
	signal(SIGINT, stopHandler);
	signal(SIGTERM, stopHandler);
	UA_ServerConfig* config = UA_ServerConfig_new_default();
	UA_Server* server = UA_Server_new(config);
	UA_StatusCode retval = UA_Server_run(server, &running);
	UA_Server_delete(server);
	UA_ServerConfig_delete(config);
	return (int)retval;
}

2. 创建节点变量

// 添加 变量到 服务器
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("name", "dev"); // 描述
	attr.displayName = UA_LOCALIZEDTEXT("dev", "data");
	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, "name"); // Id
	UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "name"); // 认证名称
	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);
}

3.修改节点变量

void opc_ua_server::writeVariable(char* str_nodeid, char* str_name, void* var_data, int data_byte) {
	UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, str_nodeid);
	UA_Variant myVar;
	UA_Variant_init(&myVar);
	UA_Variant_setScalar(&myVar, var_data, &UA_TYPES[data_byte]);
	UA_Server_writeValue(server, myIntegerNodeId, myVar);

}

官方例程

const int len_hostname = size("name-opc-ua");
unsigned char ce_name[ 11 ] = "name-opc-ua";
UA_Byte* name_name = ce_name ;
UA_String name_hostname = { 10 , name_name };
UA_ServerConfig_set_customHostname( config, name_hostname );

UA_Server* server = UA_Server_new(config);

// add a variable node to the adresspace
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = 15;
UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT_ALLOC("name", "dev");  // 描述
attr.displayName = UA_LOCALIZEDTEXT_ALLOC("dev", "data");
UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "nameid");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "name"); // 认证名称
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_deleteMembers(&attr);
UA_NodeId_deleteMembers(&myIntegerNodeId);
UA_QualifiedName_deleteMembers(&myIntegerName);

UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
UA_ServerConfig_delete(config);

return retval;

二. opc ua 服务器创建回调

附带服务器回调简单测试代码,
opc ua server 回调代码

你可能感兴趣的:(C++,VS)