tutorial_client_firststeps.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. int main(void) {
  13. UA_Client *client = UA_Client_new();
  14. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  15. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  16. if(retval != UA_STATUSCODE_GOOD) {
  17. UA_Client_delete(client);
  18. return (int)retval;
  19. }
  20. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  21. * wrapper for the raw read service available as UA_Client_Service_read. */
  22. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  23. UA_Variant_init(&value);
  24. /* NodeId of the variable holding the current time */
  25. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  26. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  27. if(retval == UA_STATUSCODE_GOOD &&
  28. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  29. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  30. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  31. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  32. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  33. }
  34. /* Clean up */
  35. UA_Variant_clear(&value);
  36. UA_Client_delete(client); /* Disconnects the client internally */
  37. return UA_STATUSCODE_GOOD;
  38. }
  39. /**
  40. * Compilation is similar to the server example.
  41. *
  42. * .. code-block:: bash
  43. *
  44. * $ gcc -std=c99 open62541.c myClient.c -o myClient
  45. *
  46. * In a MinGW environment, the Winsock library must be added.
  47. *
  48. * .. code-block:: bash
  49. *
  50. * $ gcc -std=c99 open62541.c myClient.c -lws2_32 -o myClient.exe
  51. *
  52. * Further tasks
  53. * ^^^^^^^^^^^^^
  54. *
  55. * - Try to connect to some other OPC UA server by changing
  56. * ``opc.tcp://localhost:4840`` to an appropriate address (remember that the
  57. * queried node is contained in any OPC UA server).
  58. *
  59. * - Try to set the value of the variable node (ns=1,i="the.answer") containing
  60. * an ``Int32`` from the example server (which is built in
  61. * :doc:`tutorial_server_firststeps`) using "UA_Client_write" function. The
  62. * example server needs some more modifications, i.e., changing request types.
  63. * The answer can be found in "examples/exampleClient.c". */