server.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <iostream>
  6. #include <cstring>
  7. #ifdef NOT_AMALGATED
  8. # include "ua_server.h"
  9. # include "logger_stdout.h"
  10. # include "networklayer_tcp.h"
  11. #else
  12. # include "open62541.h"
  13. #endif
  14. /**
  15. * Build Instructions (Linux)
  16. * - gcc -std=c99 -c open62541.c
  17. * - g++ server.cpp open62541.o -o server
  18. */
  19. using namespace std;
  20. int main()
  21. {
  22. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  23. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  24. UA_Logger logger = Logger_Stdout_new();
  25. UA_Server_setLogger(server, logger);
  26. // add a variable node to the adresspace
  27. UA_VariableAttributes attr;
  28. UA_VariableAttributes_init(&attr);
  29. UA_Int32 myInteger = 42;
  30. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  31. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  32. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  33. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  34. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  35. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  36. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  37. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  38. parentReferenceNodeId, myIntegerName,
  39. UA_NODEID_NULL, attr, NULL);
  40. UA_Boolean running = UA_TRUE;
  41. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  42. UA_Server_delete(server);
  43. return retval;
  44. }