当前位置:   article > 正文

open62541 windows VS client/server 实例_open62541 vxworks

open62541 vxworks

过程

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

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

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

源代码 

server 端

  1. #pragma comment(lib, "wsock32.lib")
  2. #pragma comment(lib, "ws2_32.lib")
  3. #include <iostream>
  4. #include <open62541.h>
  5. using namespace std;
  6. UA_Boolean running = true;
  7. static void
  8. stopHandler(int sig) {
  9. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
  10. running = false;
  11. }
  12. int
  13. main(void) {
  14. cout << "OPCUA Server" << endl;
  15. UA_Server *server = UA_Server_new();
  16. // UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  17. UA_ServerConfig_setMinimal(UA_Server_getConfig(server),48410, NULL);
  18. // add a variable node to the adresspace
  19. UA_VariableAttributes attr = UA_VariableAttributes_default;
  20. UA_Int32 myInteger = 42;
  21. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  22. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "the answer");
  23. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "the answer");
  24. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  25. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  26. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  27. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  28. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  29. parentReferenceNodeId, myIntegerName, UA_NODEID_NULL, attr,
  30. NULL, NULL);
  31. /* allocations on the heap need to be freed */
  32. UA_VariableAttributes_clear(&attr);
  33. UA_NodeId_clear(&myIntegerNodeId);
  34. UA_QualifiedName_clear(&myIntegerName);
  35. UA_StatusCode retval = UA_Server_run(server, &running);
  36. UA_Server_delete(server);
  37. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  38. }

Client 端

  1. #pragma comment(lib, "wsock32.lib")
  2. #pragma comment(lib, "ws2_32.lib")
  3. #include <iostream>
  4. #include "open62541.h"
  5. using namespace std;
  6. int main()
  7. {
  8. UA_Client *client = UA_Client_new();
  9. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  10. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:48410");
  11. if(retval != UA_STATUSCODE_GOOD) {
  12. UA_Client_delete(client);
  13. return (int)retval;
  14. }
  15. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  16. UA_Variant_init(&value);
  17. retval =UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, (char *)"the.answer"), &value);
  18. if(retval == UA_STATUSCODE_GOOD &&
  19. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_INT32])) {
  20. UA_Int32 rawVal = *(UA_Int32 *)value.data;
  21. cout << "the.answer Val=" << rawVal << endl;
  22. }
  23. /* Clean up */
  24. UA_Client_delete(client);
  25. return UA_STATUSCODE_GOOD;
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/982192
推荐阅读
相关标签
  

闽ICP备14008679号