tutorial_datatypes.c 4.2 KB

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