client.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifdef NOT_AMALGATED
  2. #include "ua_types.h"
  3. #include "ua_client.h"
  4. #include "ua_nodeids.h"
  5. #else
  6. #include "open62541.h"
  7. #endif
  8. #include <stdio.h>
  9. #include "networklayer_tcp.h"
  10. int main(int argc, char *argv[]) {
  11. UA_Client *client = UA_Client_new();
  12. UA_ClientNetworkLayer nl = ClientNetworkLayerTCP_new(UA_ConnectionConfig_standard);
  13. UA_StatusCode retval = UA_Client_connect(client, UA_ConnectionConfig_standard, nl,
  14. "opc.tcp://localhost:16664");
  15. if(retval != UA_STATUSCODE_GOOD) {
  16. UA_Client_delete(client);
  17. return retval;
  18. }
  19. // Browse some objects
  20. UA_BrowseDescription bd;
  21. UA_BrowseDescription_init(&bd);
  22. bd.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  23. bd.resultMask = UA_BROWSERESULTMASK_ALL; // everything
  24. UA_BrowseRequest breq;
  25. UA_BrowseRequest_init(&breq);
  26. breq.requestedMaxReferencesPerNode = 0;
  27. breq.nodesToBrowse = &bd;
  28. breq.nodesToBrowseSize = 1;
  29. UA_BrowseResponse bresp = UA_Client_browse(client, &breq);
  30. printf("result code: %d, results: %d, diagnostics: %d\n", bresp.responseHeader.serviceResult, bresp.resultsSize, bresp.diagnosticInfosSize);
  31. puts("id, namespace, browse name, display name");
  32. if (bresp.resultsSize > 0) for (int i = 0; i < bresp.resultsSize; ++i) {
  33. if (bresp.results[i].referencesSize > 0) for (int j = 0; j < bresp.results[i].referencesSize; ++j) {
  34. UA_ReferenceDescription *ref = &(bresp.results[i].references[j]);
  35. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC){
  36. printf("%d, %d, %.*s, %.*s\n", ref->nodeId.nodeId.identifier.numeric, ref->browseName.namespaceIndex, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  37. }else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING){
  38. printf("%.*s, %d, %.*s, %.*s\n", ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data, ref->browseName.namespaceIndex, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  39. }else{
  40. printf("--, %d, %.*s, %.*s\n", ref->browseName.namespaceIndex, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  41. }
  42. }
  43. }
  44. UA_BrowseResponse_deleteMembers(&bresp);
  45. // Read a node
  46. UA_ReadRequest req;
  47. UA_ReadRequest_init(&req);
  48. req.nodesToRead = UA_ReadValueId_new();
  49. req.nodesToReadSize = 1;
  50. req.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  51. req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  52. UA_ReadResponse resp = UA_Client_read(client, &req);
  53. if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  54. resp.resultsSize > 0 && resp.results[0].hasValue &&
  55. resp.results[0].value.data /* an empty array returns a null-ptr */ &&
  56. resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32])
  57. printf("the answer is: %i\n", *(UA_Int32*)resp.results[0].value.data);
  58. UA_ReadRequest_deleteMembers(&req);
  59. UA_ReadResponse_deleteMembers(&resp);
  60. UA_Client_disconnect(client);
  61. UA_Client_delete(client);
  62. return UA_STATUSCODE_GOOD;
  63. }