client_types_custom.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. UA_ClientConfig config = UA_ClientConfig_default;
  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 = {config.customDataTypes, 1, types};
  14. config.customDataTypes = &customDataTypes;
  15. UA_Client *client = UA_Client_new(config);
  16. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  17. if(retval != UA_STATUSCODE_GOOD) {
  18. UA_Client_delete(client);
  19. return (int)retval;
  20. }
  21. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  22. UA_Variant_init(&value);
  23. UA_NodeId nodeId =
  24. UA_NODEID_STRING(1, "3D.Point");
  25. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  26. if(retval == UA_STATUSCODE_GOOD) {
  27. Point *p = (Point *)value.data;
  28. printf("Point = %f, %f, %f \n", p->x, p->y, p->z);
  29. }
  30. /* Clean up */
  31. UA_Variant_clear(&value);
  32. UA_Client_delete(client); /* Disconnects the client internally */
  33. return UA_STATUSCODE_GOOD;
  34. }