client_firstSteps.c 1.5 KB

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