server_udp.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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, 4840);
  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 = UA_VariableAttributes_default;
  25. UA_Int32 myInteger = 42;
  26. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  27. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  28. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  29. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  30. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  31. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  32. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  33. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  34. parentReferenceNodeId, myIntegerName,
  35. UA_NODEID_NULL, attr, NULL, NULL);
  36. UA_StatusCode retval = UA_Server_run(server, &running);
  37. UA_Server_delete(server);
  38. return (int) retval;
  39. }