client.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. void handler_TheAnswerChanged(UA_UInt32 handle, UA_DataValue *value);
  12. void handler_TheAnswerChanged(UA_UInt32 handle, UA_DataValue *value) {
  13. printf("The Answer has changed!\n");
  14. return;
  15. }
  16. int main(int argc, char *argv[]) {
  17. UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new());
  18. UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect,
  19. "opc.tcp://localhost:16664");
  20. if(retval != UA_STATUSCODE_GOOD) {
  21. UA_Client_delete(client);
  22. return retval;
  23. }
  24. // Browse some objects
  25. printf("Browsing nodes in objects folder:\n");
  26. UA_BrowseRequest bReq;
  27. UA_BrowseRequest_init(&bReq);
  28. bReq.requestedMaxReferencesPerNode = 0;
  29. bReq.nodesToBrowse = UA_BrowseDescription_new();
  30. bReq.nodesToBrowseSize = 1;
  31. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder
  32. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything
  33. UA_BrowseResponse bResp = UA_Client_browse(client, &bReq);
  34. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  35. for (int i = 0; i < bResp.resultsSize; ++i) {
  36. for (int j = 0; j < bResp.results[i].referencesSize; ++j) {
  37. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  38. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) {
  39. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  40. ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length,
  41. ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  42. } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) {
  43. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex,
  44. ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data,
  45. ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length,
  46. ref->displayName.text.data);
  47. }
  48. //TODO: distinguish further types
  49. }
  50. }
  51. UA_BrowseRequest_deleteMembers(&bReq);
  52. UA_BrowseResponse_deleteMembers(&bResp);
  53. #ifdef ENABLE_SUBSCRIPTIONS
  54. // Create a subscription with interval 0 (immediate)...
  55. UA_Int32 subId = UA_Client_newSubscription(client, 0);
  56. if (subId)
  57. printf("Create subscription succeeded, id %u\n", subId);
  58. // .. and monitor TheAnswer
  59. UA_NodeId monitorThis;
  60. monitorThis = UA_NODEID_STRING_ALLOC(1, "the.answer");
  61. UA_UInt32 monId = UA_Client_monitorItemChanges(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE, &handler_TheAnswerChanged );
  62. if (monId)
  63. printf("Monitoring 'the.answer', id %u\n", subId);
  64. UA_NodeId_deleteMembers(&monitorThis);
  65. // First Publish always generates data (current value) and call out handler.
  66. UA_Client_doPublish(client);
  67. // This should not generate anything
  68. UA_Client_doPublish(client);
  69. #endif
  70. UA_Int32 value = 0;
  71. // Read node's value
  72. printf("\nReading the value of node (1, \"the.answer\"):\n");
  73. UA_ReadRequest rReq;
  74. UA_ReadRequest_init(&rReq);
  75. rReq.nodesToRead = UA_ReadValueId_new();
  76. rReq.nodesToReadSize = 1;
  77. rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  78. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  79. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  80. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  81. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  82. UA_Variant_isScalar(&rResp.results[0].value) &&
  83. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
  84. value = *(UA_Int32*)rResp.results[0].value.data;
  85. printf("the value is: %i\n", value);
  86. }
  87. UA_ReadRequest_deleteMembers(&rReq);
  88. UA_ReadResponse_deleteMembers(&rResp);
  89. value++;
  90. // Write node's value
  91. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  92. UA_WriteRequest wReq;
  93. UA_WriteRequest_init(&wReq);
  94. wReq.nodesToWrite = UA_WriteValue_new();
  95. wReq.nodesToWriteSize = 1;
  96. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  97. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  98. wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
  99. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  100. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion
  101. wReq.nodesToWrite[0].value.value.data = &value;
  102. UA_WriteResponse wResp = UA_Client_write(client, &wReq);
  103. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  104. printf("the new value is: %i\n", value);
  105. UA_WriteRequest_deleteMembers(&wReq);
  106. UA_WriteResponse_deleteMembers(&wResp);
  107. #ifdef ENABLE_SUBSCRIPTIONS
  108. // Take another look at the.answer... this should call the handler.
  109. UA_Client_doPublish(client);
  110. // Delete our subscription (which also unmonitors all items)
  111. if(!UA_Client_removeSubscription(client, subId))
  112. printf("Subscription removed\n");
  113. #endif
  114. UA_Client_disconnect(client);
  115. UA_Client_delete(client);
  116. return UA_STATUSCODE_GOOD;
  117. }