tutorial_client_firststeps.c 2.7 KB

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