client.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifdef NOT_AMALGATED
  2. #include "ua_types.h"
  3. #include "ua_client.h"
  4. #include "ua_nodeids.h"
  5. #include "networklayer_tcp.h"
  6. #include "logger_stdout.h"
  7. #else
  8. #include "open62541.h"
  9. #endif
  10. #include <stdio.h>
  11. int main(int argc, char *argv[]) {
  12. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  13. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect,
  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. printf("Browsing nodes in objects folder:\n");
  21. UA_BrowseRequest bReq;
  22. UA_BrowseRequest_init(&bReq);
  23. bReq.requestedMaxReferencesPerNode = 0;
  24. bReq.nodesToBrowse = UA_BrowseDescription_new();
  25. bReq.nodesToBrowseSize = 1;
  26. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder
  27. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything
  28. UA_BrowseResponse bResp = UA_Client_browse(client, &bReq);
  29. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  30. for (int i = 0; i < bResp.resultsSize; ++i) {
  31. for (int j = 0; j < bResp.results[i].referencesSize; ++j) {
  32. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  33. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  34. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  35. ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length,
  36. ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  37. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  38. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  39. ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data,
  40. ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length,
  41. ref->displayName.text.data);
  42. }
  43. //TODO: distinguish further types
  44. }
  45. }
  46. UA_BrowseRequest_deleteMembers(&bReq);
  47. UA_BrowseResponse_deleteMembers(&bResp);
  48. UA_Int32 value = 0;
  49. // Read node's value
  50. printf("\nReading the value of node (1, \"the.answer\"):\n");
  51. UA_ReadRequest rReq;
  52. UA_ReadRequest_init(&rReq);
  53. rReq.nodesToRead = UA_ReadValueId_new();
  54. rReq.nodesToReadSize = 1;
  55. rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  56. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  57. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  58. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  59. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  60. UA_Variant_isScalar(&rResp.results[0].value) &&
  61. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
  62. value = *(UA_Int32*)rResp.results[0].value.data;
  63. printf("the value is: %i\n", value);
  64. }
  65. UA_ReadRequest_deleteMembers(&rReq);
  66. UA_ReadResponse_deleteMembers(&rResp);
  67. value++;
  68. // Write node's value
  69. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  70. UA_WriteRequest wReq;
  71. UA_WriteRequest_init(&wReq);
  72. wReq.nodesToWrite = UA_WriteValue_new();
  73. wReq.nodesToWriteSize = 1;
  74. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  75. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  76. wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
  77. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  78. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion
  79. wReq.nodesToWrite[0].value.value.data = &value;
  80. UA_WriteResponse wResp = UA_Client_write(client, &wReq);
  81. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  82. printf("the new value is: %i\n", value);
  83. UA_WriteRequest_deleteMembers(&wReq);
  84. UA_WriteResponse_deleteMembers(&wResp);
  85. UA_Client_disconnect(client);
  86. UA_Client_delete(client);
  87. return UA_STATUSCODE_GOOD;
  88. }