client_firstSteps.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include <stdio.h>
  4. #include <inttypes.h>
  5. #ifdef UA_NO_AMALGAMATION
  6. #include "ua_client.h"
  7. #include "ua_config_standard.h"
  8. #include "ua_network_tcp.h"
  9. #else
  10. #include "open62541.h"
  11. #endif
  12. int main(void) {
  13. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  14. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  15. if(retval != UA_STATUSCODE_GOOD) {
  16. UA_Client_delete(client);
  17. return (int)retval;
  18. }
  19. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  20. * wrapper for the raw read service available as UA_Client_Service_read. */
  21. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  22. UA_Variant_init(&value);
  23. #define NS0_CURRENT_TIME 2258
  24. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, NS0_CURRENT_TIME);
  25. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  26. if(retval == UA_STATUSCODE_GOOD &&
  27. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  28. UA_DateTime raw_date = *(UA_DateTime*)value.data;
  29. UA_String string_date = UA_DateTime_toString(raw_date);
  30. printf("string date is: %.*s\n", (int)string_date.length, string_date.data);
  31. UA_String_deleteMembers(&string_date);
  32. }
  33. /* Clean up */
  34. UA_Variant_deleteMembers(&value);
  35. UA_Client_delete(client); /* Disconnects the client internally */
  36. return UA_STATUSCODE_GOOD;
  37. }