client_firstSteps.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. //variables to store data
  20. UA_DateTime raw_date = 0;
  21. UA_String string_date;
  22. /* create a readrequest with one entry */
  23. UA_ReadRequest rReq;
  24. UA_ReadRequest_init(&rReq);
  25. rReq.nodesToRead = UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
  26. rReq.nodesToReadSize = 1;
  27. /* define the node and attribute to be read */
  28. #define NS0_CURRENT_TIME 2258
  29. rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(0, NS0_CURRENT_TIME);
  30. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  31. /* call the service and print the result */
  32. UA_ReadResponse rResp = UA_Client_Service_read(client, rReq);
  33. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD && rResp.resultsSize > 0 &&
  34. rResp.results[0].hasValue && UA_Variant_isScalar(&rResp.results[0].value) &&
  35. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DATETIME]) {
  36. raw_date = *(UA_DateTime*)rResp.results[0].value.data;
  37. printf("raw date is: %" PRId64 "\n", raw_date);
  38. string_date = UA_DateTime_toString(raw_date);
  39. printf("string date is: %.*s\n", (int)string_date.length, string_date.data);
  40. }
  41. UA_ReadRequest_deleteMembers(&rReq);
  42. UA_ReadResponse_deleteMembers(&rResp);
  43. UA_String_deleteMembers(&string_date);
  44. UA_Client_disconnect(client);
  45. UA_Client_delete(client);
  46. return (int) UA_STATUSCODE_GOOD;
  47. }