client_firstSteps.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //This file contains source-code that is discussed in a tutorial located here:
  2. //http://open62541.org/doc/sphinx/tutorial_firstStepsClient.html
  3. #include <stdio.h>
  4. #ifdef UA_NO_AMALGAMATION
  5. # include "ua_types.h"
  6. # include "ua_server.h"
  7. # include "logger_stdout.h"
  8. # include "networklayer_tcp.h"
  9. #else
  10. # include "open62541.h"
  11. #endif
  12. int main(void) {
  13. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  14. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect, "opc.tcp://localhost:16664");
  15. if(retval != UA_STATUSCODE_GOOD) {
  16. UA_Client_delete(client);
  17. return retval;
  18. }
  19. //variables to store data
  20. UA_DateTime raw_date = 0;
  21. UA_String* string_date = UA_String_new();
  22. UA_ReadRequest rReq;
  23. UA_ReadRequest_init(&rReq);
  24. rReq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], 1);
  25. rReq.nodesToReadSize = 1;
  26. rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(0, 2258);
  27. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  28. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  29. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  30. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  31. UA_Variant_isScalar(&rResp.results[0].value) &&
  32. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DATETIME]) {
  33. raw_date = *(UA_DateTime*)rResp.results[0].value.data;
  34. #ifdef _WIN32
  35. printf("raw date is: %I64u\n", raw_date);
  36. #else
  37. printf("raw date is: %llu\n", raw_date);
  38. #endif
  39. UA_DateTime_toString(raw_date, string_date);
  40. printf("string date is: %.*s\n", string_date->length, string_date->data);
  41. }
  42. UA_ReadRequest_deleteMembers(&rReq);
  43. UA_ReadResponse_deleteMembers(&rResp);
  44. UA_String_delete(string_date);
  45. UA_Client_disconnect(client);
  46. UA_Client_delete(client);
  47. return 0;
  48. }