tutorial_datatypes.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * .. _types-tutorial:
  5. *
  6. * Working with Data Types
  7. * -----------------------
  8. *
  9. * OPC UA defines a type system for values that can be encoded in the protocol
  10. * messages. This tutorial shows some examples for available data types and
  11. * their use. See the section on :ref:`types` for the full definitions.
  12. *
  13. * Basic Data Handling
  14. * ^^^^^^^^^^^^^^^^^^^
  15. * This section shows the basic interaction patterns for data types. Make
  16. * sure to compare with the type definitions in ``ua_types.h``. */
  17. #include "open62541.h"
  18. static void
  19. variables_basic(void) {
  20. /* Int32 */
  21. UA_Int32 i = 5;
  22. UA_Int32 j;
  23. UA_Int32_copy(&i, &j);
  24. UA_Int32 *ip = UA_Int32_new();
  25. UA_Int32_copy(&i, ip);
  26. UA_Int32_delete(ip);
  27. /* String */
  28. UA_String s;
  29. UA_String_init(&s); /* _init zeroes out the entire memory of the datatype */
  30. char *test = "test";
  31. s.length = strlen(test);
  32. s.data = (UA_Byte*)test;
  33. UA_String s2;
  34. UA_String_copy(&s, &s2);
  35. UA_String_deleteMembers(&s2); /* Copying heap-allocated the dynamic content */
  36. UA_String s3 = UA_STRING("test2");
  37. UA_String s4 = UA_STRING_ALLOC("test2"); /* Copies the content to the heap */
  38. UA_Boolean eq = UA_String_equal(&s3, &s4);
  39. UA_String_deleteMembers(&s4);
  40. if(!eq)
  41. return;
  42. /* Structured Type */
  43. UA_ReadRequest rr;
  44. UA_init(&rr, &UA_TYPES[UA_TYPES_READREQUEST]); /* Generic method */
  45. UA_ReadRequest_init(&rr); /* Shorthand for the previous line */
  46. rr.requestHeader.timestamp = UA_DateTime_now(); /* Members of a structure */
  47. rr.nodesToRead = (UA_ReadValueId *)UA_Array_new(5, &UA_TYPES[UA_TYPES_READVALUEID]);
  48. rr.nodesToReadSize = 5; /* Array size needs to be made known */
  49. UA_ReadRequest *rr2 = UA_ReadRequest_new();
  50. UA_copy(&rr, rr2, &UA_TYPES[UA_TYPES_READREQUEST]);
  51. UA_ReadRequest_deleteMembers(&rr);
  52. UA_ReadRequest_delete(rr2);
  53. }
  54. /**
  55. * NodeIds
  56. * ^^^^^^^
  57. * An OPC UA information model is made up of nodes and references between nodes.
  58. * Every node has a unique :ref:`nodeid`. NodeIds refer to a namespace with an
  59. * additional identifier value that can be an integer, a string, a guid or a
  60. * bytestring. */
  61. static void
  62. variables_nodeids(void) {
  63. UA_NodeId id1 = UA_NODEID_NUMERIC(1, 1234);
  64. id1.namespaceIndex = 3;
  65. UA_NodeId id2 = UA_NODEID_STRING(1, "testid"); /* the string is static */
  66. UA_Boolean eq = UA_NodeId_equal(&id1, &id2);
  67. if(eq)
  68. return;
  69. UA_NodeId id3;
  70. UA_NodeId_copy(&id2, &id3);
  71. UA_NodeId_deleteMembers(&id3);
  72. UA_NodeId id4 = UA_NODEID_STRING_ALLOC(1, "testid"); /* the string is copied
  73. to the heap */
  74. UA_NodeId_deleteMembers(&id4);
  75. }
  76. /**
  77. * Variants
  78. * ^^^^^^^^
  79. * The datatype :ref:`variant` belongs to the built-in datatypes of OPC UA and
  80. * is used as a container type. A variant can hold any other datatype as a
  81. * scalar (except variant) or as an array. Array variants can additionally
  82. * denote the dimensionality of the data (e.g. a 2x3 matrix) in an additional
  83. * integer array. */
  84. static void
  85. variables_variants(void) {
  86. /* Set a scalar value */
  87. UA_Variant v;
  88. UA_Int32 i = 42;
  89. UA_Variant_setScalar(&v, &i, &UA_TYPES[UA_TYPES_INT32]);
  90. /* Make a copy */
  91. UA_Variant v2;
  92. UA_Variant_copy(&v, &v2);
  93. UA_Variant_deleteMembers(&v2);
  94. /* Set an array value */
  95. UA_Variant v3;
  96. UA_Double d[9] = {1.0, 2.0, 3.0,
  97. 4.0, 5.0, 6.0,
  98. 7.0, 8.0, 9.0};
  99. UA_Variant_setArrayCopy(&v3, d, 9, &UA_TYPES[UA_TYPES_DOUBLE]);
  100. /* Set array dimensions */
  101. v3.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_UINT32]);
  102. v3.arrayDimensionsSize = 2;
  103. v3.arrayDimensions[0] = 3;
  104. v3.arrayDimensions[1] = 3;
  105. UA_Variant_deleteMembers(&v3);
  106. }
  107. /** It follows the main function, making use of the above definitions. */
  108. int main(void) {
  109. variables_basic();
  110. variables_nodeids();
  111. variables_variants();
  112. return 0;
  113. }