tutorial_client_firststeps.c 2.7 KB

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