server_udp.c 1.6 KB

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