client_firstSteps.c 1.5 KB

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