client_types_custom.c 1.5 KB

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