client.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifdef NOT_AMALGATED
  2. #include "ua_types.h"
  3. #include "ua_client.h"
  4. #else
  5. #include "open62541.h"
  6. #endif
  7. #include <stdio.h>
  8. #include "networklayer_tcp.h"
  9. int main(int argc, char *argv[]) {
  10. UA_Client *client = UA_Client_new();
  11. UA_ClientNetworkLayer nl = ClientNetworkLayerTCP_new(UA_ConnectionConfig_standard);
  12. UA_StatusCode retval = UA_Client_connect(client, UA_ConnectionConfig_standard, nl,
  13. "opc.tcp://localhost:16664");
  14. if(retval != UA_STATUSCODE_GOOD) {
  15. UA_Client_delete(client);
  16. return retval;
  17. }
  18. UA_ReadRequest req;
  19. UA_ReadRequest_init(&req);
  20. req.nodesToRead = UA_ReadValueId_new();
  21. req.nodesToReadSize = 1;
  22. req.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  23. req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  24. UA_ReadResponse resp = UA_Client_read(client, &req);
  25. if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  26. resp.resultsSize > 0 && resp.results[0].hasValue &&
  27. resp.results[0].value.data /* an empty array returns a null-ptr */ &&
  28. resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32])
  29. printf("the answer is: %i\n", *(UA_Int32*)resp.results[0].value.data);
  30. UA_ReadRequest_deleteMembers(&req);
  31. UA_ReadResponse_deleteMembers(&resp);
  32. UA_Client_disconnect(client);
  33. UA_Client_delete(client);
  34. return UA_STATUSCODE_GOOD;
  35. }