WINDOWS:OPEN62541服务器编程
创建工程
编译环境 windows 10 + Visual Studio 2019 Community
新建工程,将cmake编译生成的open62541.h/.c加载进入工程
添加必要的库
OPEN62541需要额外加载两个库:wsock32.lib ws2_32.lib,在程序开头添加:
#pragma comment(lib, “wsock32.lib”)
#pragma comment(lib, “ws2_32.lib”)
或者在工程属性中添加相应的库
代码
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server* server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
UA_ServerConfig* config = UA_Server_getConfig(server);
config->verifyRequestTimestamp = UA_RULEHANDLING_ACCEPT;
HANDLE handle1;
handle1 = CreateThread(NULL, 0, MyThreadProc1, server, 0, NULL);
printf("in Function %s line %d\n", __FUNCTION__, __LINE__);
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
{
UA_Server* server = (UA_Server*)lpParameter;
int32_t i32data;
string idName,actionName;
printf("MyThreadProc1 begin\n");
while (running) {
cin >> actionName >> idName >> i32data;
if (actionName == "addi") {
cout << "addVariable " << idName << i32data << endl;
addVariable(server, (char *)idName.data(), i32data);
writeWrongVariable(server, (char*)idName.data());
}
else if (actionName == "writei") {
cout << "writeVariable " << idName << i32data << endl;
writeVariable(server, (char*)idName.data(), i32data);
}
else if (actionName == "addm") {
cout << "addMatrixVariable " << idName << i32data << endl;
addMatrixVariable(server);
}
}
printf("MyThreadProc1 end\n");
return 0;
}
DWORD WINAPI MyThreadProc1(LPVOID lpParameter);
static volatile UA_Boolean running = true;
static void stopHandler(int sig) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
running = false;
}
static void addVariable(UA_Server* server, char* name, int32_t data) {
/* Define the attribute of the myInteger variable node */
UA_VariableAttributes attr = UA_VariableAttributes_default;
UA_Int32 myInteger = data;
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT("en-US", name);
attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
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);
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);
}
static void writeVariable(UA_Server* server, char* name, int32_t data) {
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
/* Write a different integer value */
UA_Int32 myInteger = data;
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
UA_Server_writeValue(server, myIntegerNodeId, myVar);
/* Set the status code of the value to an error code. The function
* UA_Server_write provides access to the raw service. The above
* UA_Server_writeValue is syntactic sugar for writing a specific node
* attribute with the write service. */
//UA_WriteValue wv;
//UA_WriteValue_init(&wv);
//wv.nodeId = myIntegerNodeId;
//wv.attributeId = UA_ATTRIBUTEID_VALUE;
//wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
//wv.value.hasStatus = true;
//UA_Server_write(server, &wv);
///* Reset the variable to a good statuscode with a value */
//wv.value.hasStatus = false;
//wv.value.value = myVar;
//wv.value.hasValue = true;
//UA_Server_write(server, &wv);
}
static void addMatrixVariable(UA_Server* server) {
UA_VariableAttributes attr = UA_VariableAttributes_default;
attr.displayName = UA_LOCALIZEDTEXT("en-US", "Double Matrix");
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
/* Set the variable value constraints */
attr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
UA_UInt32 arrayDims[2] = { 2,2 };
attr.arrayDimensions = arrayDims;
attr.arrayDimensionsSize = 2; /* Set the value. The array dimensions need to be the same for the value. */
UA_Double zero[4] = { 0.0, 0.0, 0.0, 0.0 };
UA_Variant_setArray(&attr.value, zero, 4, &UA_TYPES[UA_TYPES_DOUBLE]);
attr.value.arrayDimensions = arrayDims;
attr.value.arrayDimensionsSize = 2;
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "double.matrix");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "double matrix");
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);
}
static void writeWrongVariable(UA_Server* server, char* name) {
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, name);
/* Write a string */
UA_String myString = UA_STRING("test");
UA_Variant myVar;
UA_Variant_init(&myVar);
UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
}
使用Unified Automation UaExpert进行数据观察
第一,运行上述server程序
第二,打开UaExpert,连接server
第三,在控制台输入指令,添加node
第四,观察UaExpert数据变化