tutorial_client_firststeps.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Building a Simple Client
  5. * ------------------------
  6. * You should already have a basic server from the previous tutorials. open62541
  7. * provides both a server- and clientside API, so creating a client is as easy as
  8. * creating a server. Copy the following into a file `myClient.c`: */
  9. #include <ua_client_highlevel.h>
  10. #include <ua_config_default.h>
  11. #include <ua_log_stdout.h>
  12. #include <stdlib.h>
  13. int main(void) {
  14. UA_Client *client = UA_Client_new();
  15. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  16. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  17. if(retval != UA_STATUSCODE_GOOD) {
  18. UA_Client_delete(client);
  19. return (int)retval;
  20. }
  21. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  22. * wrapper for the raw read service available as UA_Client_Service_read. */
  23. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  24. UA_Variant_init(&value);
  25. /* NodeId of the variable holding the current time */
  26. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  27. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  28. if(retval == UA_STATUSCODE_GOOD &&
  29. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  30. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  31. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  32. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  33. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  34. }
  35. /* Clean up */
  36. UA_Variant_clear(&value);
  37. UA_Client_delete(client); /* Disconnects the client internally */
  38. return EXIT_SUCCESS;
  39. }
  40. /**
  41. * Compilation is similar to the server example.
  42. *
  43. * .. code-block:: bash
  44. *
  45. * $ gcc -std=c99 open62541.c myClient.c -o myClient
  46. *
  47. * In a MinGW environment, the Winsock library must be added.
  48. *
  49. * .. code-block:: bash
  50. *
  51. * $ gcc -std=c99 open62541.c myClient.c -lws2_32 -o myClient.exe
  52. *
  53. * Further tasks
  54. * ^^^^^^^^^^^^^
  55. *
  56. * - Try to connect to some other OPC UA server by changing
  57. * ``opc.tcp://localhost:4840`` to an appropriate address (remember that the
  58. * queried node is contained in any OPC UA server).
  59. *
  60. * - Try to set the value of the variable node (ns=1,i="the.answer") containing
  61. * an ``Int32`` from the example server (which is built in
  62. * :doc:`tutorial_server_firststeps`) using "UA_Client_write" function. The
  63. * example server needs some more modifications, i.e., changing request types.
  64. * The answer can be found in "examples/exampleClient.c". */