client.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. return retval;
  16. UA_ReadRequest req;
  17. UA_ReadRequest_init(&req);
  18. req.nodesToRead = UA_ReadValueId_new();
  19. req.nodesToReadSize = 1;
  20. req.nodesToRead[0].nodeId = UA_NODEID_STATIC(1, 442); /* assume this node exists */
  21. req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  22. UA_ReadResponse resp = UA_Client_read(client, &req);
  23. if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  24. resp.resultsSize > 0 && resp.results[0].hasValue &&
  25. resp.results[0].value.data /* an empty array returns a null-ptr */ &&
  26. resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32])
  27. printf("the answer is: %i\n", *(UA_Int32*)resp.results[0].value.data);
  28. UA_ReadRequest_deleteMembers(&req);
  29. UA_ReadResponse_deleteMembers(&resp);
  30. UA_Client_disconnect(client);
  31. UA_Client_delete(client);
  32. return UA_STATUSCODE_GOOD;
  33. }