client_firstSteps.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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_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. //variables to store data
  18. UA_DateTime raw_date = 0;
  19. UA_String string_date;
  20. UA_ReadRequest rReq;
  21. UA_ReadRequest_init(&rReq);
  22. rReq.nodesToRead = UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
  23. rReq.nodesToReadSize = 1;
  24. rReq.nodesToRead[0].nodeId = UA_NODEID_NUMERIC(0, 2258);
  25. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  26. UA_ReadResponse rResp = UA_Client_Service_read(client, rReq);
  27. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD && rResp.resultsSize > 0 &&
  28. rResp.results[0].hasValue && UA_Variant_isScalar(&rResp.results[0].value) &&
  29. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_DATETIME]) {
  30. raw_date = *(UA_DateTime*)rResp.results[0].value.data;
  31. printf("raw date is: %" PRId64 "\n", raw_date);
  32. string_date = UA_DateTime_toString(raw_date);
  33. printf("string date is: %.*s\n", (int)string_date.length, string_date.data);
  34. }
  35. UA_ReadRequest_deleteMembers(&rReq);
  36. UA_ReadResponse_deleteMembers(&rResp);
  37. UA_String_deleteMembers(&string_date);
  38. UA_Client_disconnect(client);
  39. UA_Client_delete(client);
  40. return (int) UA_STATUSCODE_GOOD;
  41. }