client_connect_loop.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Client disconnect handling
  5. * --------------------------
  6. * This example shows you how to handle a client disconnect, e.g., if the server
  7. * is shut down while the client is connected. You just need to call connect
  8. * again and the client will automatically reconnect.
  9. *
  10. * This example is very similar to the tutorial_client_firststeps.c. */
  11. #include <ua_client_highlevel.h>
  12. #include <ua_config_default.h>
  13. #include <ua_log_stdout.h>
  14. #include <signal.h>
  15. #include <stdlib.h>
  16. UA_Boolean running = true;
  17. static void stopHandler(int sign) {
  18. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Received Ctrl-C");
  19. running = 0;
  20. }
  21. int main(void) {
  22. signal(SIGINT, stopHandler); /* catches ctrl-c */
  23. UA_Client *client = UA_Client_new();
  24. UA_ClientConfig *cc = UA_Client_getConfig(client);
  25. UA_ClientConfig_setDefault(cc);
  26. /* default timeout is 5 seconds. Set it to 1 second here for demo */
  27. cc->timeout = 1000;
  28. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  29. * wrapper for the raw read service available as UA_Client_Service_read. */
  30. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  31. UA_Variant_init(&value);
  32. /* Endless loop reading the server time */
  33. while(running) {
  34. /* if already connected, this will return GOOD and do nothing */
  35. /* if the connection is closed/errored, the connection will be reset and then reconnected */
  36. /* Alternatively you can also use UA_Client_getState to get the current state */
  37. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  38. if(retval != UA_STATUSCODE_GOOD) {
  39. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Not connected. Retrying to connect in 1 second");
  40. /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */
  41. /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */
  42. UA_sleep_ms(1000);
  43. continue;
  44. }
  45. /* NodeId of the variable holding the current time */
  46. const UA_NodeId nodeId =
  47. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  48. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  49. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
  50. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
  51. "Connection was closed. Reconnecting ...");
  52. continue;
  53. }
  54. if(retval == UA_STATUSCODE_GOOD &&
  55. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  56. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  57. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  58. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  59. "date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
  60. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  61. }
  62. UA_Variant_clear(&value);
  63. UA_sleep_ms(1000);
  64. };
  65. /* Clean up */
  66. UA_Variant_clear(&value);
  67. UA_Client_delete(client); /* Disconnects the client internally */
  68. return EXIT_SUCCESS;
  69. }