tutorial_client_firstSteps.rst 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 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. .. literalinclude:: ${PROJECT_SOURCE_DIR}/examples/client_firstSteps.c
  27. :language: c
  28. :linenos:
  29. :lines: 4,5,12,14-
  30. Further tasks
  31. ^^^^^^^^^^^^^
  32. * Try to connect to some other OPC UA server by changing
  33. ``opc.tcp://localhost:16664`` to an appropriate address (remember that the
  34. queried node is contained in any OPC UA server).
  35. * Try to set the value of the variable node (ns=1,i="the.answer") containing an
  36. ``Int32`` from the example server (which is built in
  37. :doc:`tutorial_server_firstSteps`) using "UA_Client_write" function. The
  38. example server needs some more modifications, i.e., changing request types.
  39. The answer can be found in "examples/exampleClient.c".