open62541 windows VS client/server 实例

Posted 姚家湾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了open62541 windows VS client/server 实例相关的知识,希望对你有一定的参考价值。

过程

在Linux 上使用CMAKE 配置。平台选择为win32.生成open62541.h和open62541.c 文件。

在VS 建立一个C++ Console 程序,将open62541.h 考入头文件。open62541.c 考入源文件

在项目属性中加入这两个文件的文件夹

源代码 

server 端

#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include <open62541.h>
using namespace std;
UA_Boolean running = true;
static void
stopHandler(int sig) 
    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
    running = false;


int
main(void) 
  
  
    cout << "OPCUA Server" << endl;
    UA_Server *server = UA_Server_new();
   // UA_ServerConfig_setDefault(UA_Server_getConfig(server));
    UA_ServerConfig_setMinimal(UA_Server_getConfig(server),48410, NULL);
    // add a variable node to the adresspace
    UA_VariableAttributes attr = UA_VariableAttributes_default;
    UA_Int32 myInteger = 42;
    UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
    attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "the answer");
    attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "the answer");
    UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
    UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(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);

    /* allocations on the heap need to be freed */
    UA_VariableAttributes_clear(&attr);
    UA_NodeId_clear(&myIntegerNodeId);
    UA_QualifiedName_clear(&myIntegerName);

    UA_StatusCode retval = UA_Server_run(server, &running);

    UA_Server_delete(server);
    return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;

Client 端

#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
#include "open62541.h"
using namespace std;
int main()

    UA_Client *client = UA_Client_new();
    UA_ClientConfig_setDefault(UA_Client_getConfig(client));
    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:48410");
    if(retval != UA_STATUSCODE_GOOD) 
        UA_Client_delete(client);
        return (int)retval;
    

  
    UA_Variant value; /* Variants can hold scalar values and arrays of any type */
    UA_Variant_init(&value);

    retval =UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, (char *)"the.answer"), &value);

    if(retval == UA_STATUSCODE_GOOD &&
       UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) 
        UA_Int32 rawVal = *(UA_Int32 *)value.data;
        cout << "the.answer Val=" << rawVal << endl;
    

    /* Clean up */
    UA_Client_delete(client); 
    return UA_STATUSCODE_GOOD;


以上是关于open62541 windows VS client/server 实例的主要内容,如果未能解决你的问题,请参考以下文章

在 Windows 中使用 open62541 构建一个简单的服务器

在 Windows 上使用 CMake 构建 open62541 失败

使用 Visual Studio 2017 构建 open62541 DLL

open62541:发布温度值

基于open62541的OPC UA服务器建立

open62541:构建 PubSub 示例时出错