client_types_custom.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include <open62541/client_config_default.h>
  4. #include <open62541/client_highlevel.h>
  5. #include <stdlib.h>
  6. #include "custom_datatype.h"
  7. int main(void) {
  8. /* Make your custom datatype known to the stack */
  9. UA_DataType types[1];
  10. types[0] = PointType;
  11. /* Attention! Here the custom datatypes are allocated on the stack. So they
  12. * cannot be accessed from parallel (worker) threads. */
  13. UA_DataTypeArray customDataTypes = {NULL, 1, types};
  14. UA_Client *client = UA_Client_new();
  15. UA_ClientConfig *cc = UA_Client_getConfig(client);
  16. UA_ClientConfig_setDefault(cc);
  17. cc->customDataTypes = &customDataTypes;
  18. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  19. if(retval != UA_STATUSCODE_GOOD) {
  20. UA_Client_delete(client);
  21. return (int)retval;
  22. }
  23. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  24. UA_Variant_init(&value);
  25. UA_NodeId nodeId =
  26. UA_NODEID_STRING(1, "3D.Point");
  27. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  28. if(retval == UA_STATUSCODE_GOOD) {
  29. Point *p = (Point *)value.data;
  30. printf("Point = %f, %f, %f \n", p->x, p->y, p->z);
  31. }
  32. /* Clean up */
  33. UA_Variant_clear(&value);
  34. UA_Client_delete(client); /* Disconnects the client internally */
  35. return EXIT_SUCCESS;
  36. }