client.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include "ua_types.h"
  3. #include "ua_server.h"
  4. #include "logger_stdout.h"
  5. #include "networklayer_tcp.h"
  6. int main(void) {
  7. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  8. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect, "opc.tcp://localhost:16664");
  9. if(retval != UA_STATUSCODE_GOOD) {
  10. UA_Client_delete(client);
  11. return retval;
  12. }
  13. UA_DateTime raw_date = 0;
  14. UA_String* string_date = UA_String_new();
  15. // Read node's value
  16. UA_ReadRequest rReq;
  17. UA_ReadRequest_init(&rReq);
  18. rReq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], 1);
  19. rReq.nodesToReadSize = 1;
  20. rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(0, 2258);
  21. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  22. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  23. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  24. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  25. UA_Variant_isScalar(&rResp.results[0].value) &&
  26. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DATETIME]) {
  27. raw_date = *(UA_DateTime*)rResp.results[0].value.data;
  28. printf("raw date is: %llu\n", raw_date);
  29. UA_DateTime_toString(raw_date, string_date);
  30. printf("string date is: %.*s\n", string_date->length, string_date->data);
  31. }
  32. UA_ReadRequest_deleteMembers(&rReq);
  33. UA_ReadResponse_deleteMembers(&rResp);
  34. UA_String_delete(string_date);
  35. UA_Client_disconnect(client);
  36. UA_Client_delete(client);
  37. return 0;
  38. }