client.c 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_Client_setConfig(client, (UA_ClientConfig){.timeout=500});
  13. UA_ClientNetworkLayer nl = ClientNetworkLayerTCP_new(UA_ConnectionConfig_standard);
  14. UA_StatusCode retval = UA_Client_connect(client, UA_ConnectionConfig_standard, nl,
  15. "opc.tcp://localhost:16664");
  16. if(retval != UA_STATUSCODE_GOOD) {
  17. UA_Client_delete(client);
  18. return retval;
  19. }
  20. // Browse some objects
  21. printf("Browsing nodes in objects folder:\n");
  22. UA_BrowseRequest bReq;
  23. UA_BrowseRequest_init(&bReq);
  24. bReq.requestedMaxReferencesPerNode = 0;
  25. bReq.nodesToBrowse = UA_BrowseDescription_new();
  26. bReq.nodesToBrowseSize = 1;
  27. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder
  28. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything
  29. UA_BrowseResponse bResp = UA_Client_browse(client, &bReq);
  30. printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME");
  31. for (int i = 0; i < bResp.resultsSize; ++i) {
  32. for (int j = 0; j < bResp.results[i].referencesSize; ++j) {
  33. UA_ReferenceDescription *ref = &(bResp.results[i].references[j]);
  34. if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC){
  35. printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  36. }else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING){
  37. printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data);
  38. }
  39. //TODO: distinguish further types
  40. }
  41. }
  42. UA_BrowseRequest_deleteMembers(&bReq);
  43. UA_BrowseResponse_deleteMembers(&bResp);
  44. UA_Int32 value = 0;
  45. // Read node's value
  46. printf("\nReading the value of node (1, \"the.answer\"):\n");
  47. UA_ReadRequest rReq;
  48. UA_ReadRequest_init(&rReq);
  49. rReq.nodesToRead = UA_ReadValueId_new();
  50. rReq.nodesToReadSize = 1;
  51. rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  52. rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  53. UA_ReadResponse rResp = UA_Client_read(client, &rReq);
  54. if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  55. rResp.resultsSize > 0 && rResp.results[0].hasValue &&
  56. rResp.results[0].value.data /* an empty array returns a null-ptr */ &&
  57. rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]){
  58. value = *(UA_Int32*)rResp.results[0].value.data;
  59. printf("the value is: %i\n", value);
  60. }
  61. UA_ReadRequest_deleteMembers(&rReq);
  62. UA_ReadResponse_deleteMembers(&rResp);
  63. value++;
  64. // Write node's value
  65. printf("\nWriting a value of node (1, \"the.answer\"):\n");
  66. UA_WriteRequest wReq;
  67. UA_WriteRequest_init(&wReq);
  68. wReq.nodesToWrite = UA_WriteValue_new();
  69. wReq.nodesToWriteSize = 1;
  70. wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
  71. wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE;
  72. wReq.nodesToWrite[0].value.hasValue = UA_TRUE;
  73. wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32];
  74. wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion
  75. wReq.nodesToWrite[0].value.value.data = &value;
  76. UA_WriteResponse wResp = UA_Client_write(client, &wReq);
  77. if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  78. printf("the new value is: %i\n", value);
  79. UA_WriteRequest_deleteMembers(&wReq);
  80. UA_WriteResponse_deleteMembers(&wResp);
  81. UA_Client_disconnect(client);
  82. UA_Client_delete(client);
  83. return UA_STATUSCODE_GOOD;
  84. }