server_udp.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 <stdio.h>
  6. #include <signal.h>
  7. #include "open62541.h"
  8. UA_Logger logger = UA_Log_Stdout;
  9. UA_Boolean running = 1;
  10. static void stopHandler(int sign) {
  11. printf("Received Ctrl-C\n");
  12. running = 0;
  13. }
  14. int main(int argc, char** argv) {
  15. signal(SIGINT, stopHandler);
  16. signal(SIGTERM, stopHandler);
  17. UA_ServerConfig config = UA_ServerConfig_standard;
  18. UA_ServerNetworkLayer nl;
  19. nl = UA_ServerNetworkLayerUDP(UA_ConnectionConfig_standard, 16664);
  20. config.networkLayers = &nl;
  21. config.networkLayersSize = 1;
  22. UA_Server *server = UA_Server_new(config);
  23. // add a variable node to the adresspace
  24. UA_VariableAttributes attr;
  25. UA_VariableAttributes_init(&attr);
  26. UA_Int32 myInteger = 42;
  27. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  28. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  29. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  30. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  31. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  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, myIntegerNodeId, parentNodeId,
  35. parentReferenceNodeId, myIntegerName,
  36. UA_NODEID_NULL, attr, NULL, NULL);
  37. UA_StatusCode retval = UA_Server_run(server, &running);
  38. UA_Server_delete(server);
  39. return (int) retval;
  40. }