server.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_Variant *myIntegerVariant = UA_Variant_new();
  28. UA_Int32 myInteger = 42;
  29. UA_Variant_setScalarCopy(myIntegerVariant, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  30. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  31. UA_NodeId myIntegerNodeId = UA_NODEID_NULL; /* assign a random free nodeid */
  32. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  33. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  34. UA_Server_addVariableNode(server, myIntegerVariant, myIntegerName,
  35. myIntegerNodeId, parentNodeId, parentReferenceNodeId);
  36. UA_Boolean running = UA_TRUE;
  37. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  38. UA_Server_delete(server);
  39. return retval;
  40. }