当我开始使用 opc ua 时,我只是想知道在 opc ua 通信层的底层发生了什么。
让我们举一个非常简单的服务器实现的例子,它在地址空间中有 3 个节点。这些节点提供可由 opc-UA 客户端写入和读取的数据。
通过阅读 open62541 附带的部分代码,我了解到通信是通过 TCP 发生的。这意味着服务器启动一个客户端可以连接的套接字,并使客户端能够在节点上执行各种操作。
我的问题是,客户端如何知道可用的服务器节点?我知道它浏览地址空间,但它究竟在哪里浏览以查找可用节点?opc-UA 使用什么暴露机制向客户端呈现可用节点?服务器是否在某个 xml 文件或其他任何地方写入可用信息和节点,因此当客户端连接时,它会尝试读取文件的内容以了解 addressSpace 结构?
open62541 的示例服务器实现
#include <stdio.h>
#include <open62541.h>
#include <signal.h>
static void
addVariable(UA_Server *server) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = 43;
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_NULL, attr, NULL, NULL);
}
static void
addThirdVariable(UA_Server *server) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_String myInteger = UA_STRING("My name is variable 3"); // variable name
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_STRING]);
attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Add the variable node to the information model */
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "third.variable");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "third varaible");
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);
}
void addSecondVariable(UA_Server * server) {
//variable attributes
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_String machine_name = UA_STRING("My name is a machine"); // variable name
UA_Variant_setScalar(&attr.value, &machine_name, &UA_TYPES[UA_TYPES_STRING]);
attr.description = UA_LOCALIZEDTEXT("en-US", "machine name");
attr.displayName = UA_LOCALIZEDTEXT("en-US", "machine name");
attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
//setting access level not important
//add the variable to the information model
UA_NodeId myStringNodeID = UA_NODEID_STRING(1, "the.machine");
UA_QualifiedName myStringName = UA_QUALIFIEDNAME(1, "the machine");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myStringNodeID, parentNodeId,
parentReferenceNodeId, myStringName,
UA_NODEID_NULL, attr, NULL, NULL);
}
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_ServerConfig *config = UA_ServerConfig_new_default();
UA_Server *server = UA_Server_new(config);
addVariable(server);
addSecondVariable(server);
addThirdVariable(server);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
UA_ServerConfig_delete(config);
return (int)retval;
}