tutorial_client_firstSteps.rst 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 5. Building a simple client
  2. ===========================
  3. You should already have a basic server from the previous tutorials. open62541
  4. provides both a server- and clientside API, so creating a client is as easy as
  5. creating a server. Copy the following into a file `myClient.c`:
  6. .. code-block:: c
  7. #include "open62541.h"
  8. int main(void) {
  9. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  10. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  11. if(retval != UA_STATUSCODE_GOOD) {
  12. UA_Client_delete(client);
  13. return retval;
  14. }
  15. UA_Client_disconnect(client);
  16. UA_Client_delete(client);
  17. return 0;
  18. }
  19. Compilation is very much similar to the server example.
  20. .. code-block:: bash
  21. $ gcc -std=c99 open6251.c myClient.c -o myClient
  22. Reading a node attibute
  23. -----------------------
  24. In this example we are going to connect to the server from the second tutorial
  25. and read the value-attribute of the added variable node.
  26. .. code-block:: c
  27. #include <stdio.h>
  28. #include "open62541.h"
  29. int main(int argc, char *argv[])
  30. {
  31. /* create a client and connect */
  32. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  33. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  34. if(retval != UA_STATUSCODE_GOOD) {
  35. UA_Client_delete(client);
  36. return retval;
  37. }
  38. /* create a readrequest with one entry */
  39. UA_ReadRequest req;
  40. UA_ReadRequest_init(&req);
  41. req.nodesToRead = UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
  42. req.nodesToReadSize = 1;
  43. /* define the node and attribute to be read */
  44. req.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  45. req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
  46. /* call the service and print the result */
  47. UA_ReadResponse resp = UA_Client_Service_read(client, req);
  48. if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
  49. resp.resultsSize > 0 && resp.results[0].hasValue &&
  50. UA_Variant_isScalar(&resp.results[0].value) &&
  51. resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
  52. UA_Int32 *value = (UA_Int32*)resp.results[0].value.data;
  53. printf("the value is: %i\n", *value);
  54. }
  55. UA_ReadRequest_deleteMembers(&req);
  56. UA_ReadResponse_deleteMembers(&resp);
  57. UA_Client_disconnect(client);
  58. UA_Client_delete(client);
  59. return UA_STATUSCODE_GOOD;
  60. }
  61. Further tasks
  62. -------------
  63. * Try to connect to some other OPC UA server by changing
  64. "opc.tcp://localhost:16664" to an appropriate address (remember that the
  65. queried node is contained in any OPC UA server).
  66. * Try to set the value of the variable node (ns=1,i="the.answer") containing an
  67. "Int32" from the example server (which is built in
  68. :doc:`tutorial_server_firstSteps`) using "UA_Client_write" function. The
  69. example server needs some more modifications, i.e., changing request types.
  70. The answer can be found in "examples/exampleClient.c".