tutorial_server_variable.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * Adding Variables to a Server
  5. * ----------------------------
  6. *
  7. * This tutorial shows how to work with data types and how to add variable nodes
  8. * to a server. First, we add a new variable to the server. Take a look at the
  9. * definition of the ``UA_VariableAttrbitues`` structure to see the list of all
  10. * attributes defined for VariableNodes.
  11. */
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include "open62541.h"
  15. static void
  16. addVariable(UA_Server *server) {
  17. /* Define the attribute of the myInteger variable node */
  18. UA_VariableAttributes attr;
  19. UA_VariableAttributes_init(&attr);
  20. UA_Int32 myInteger = 42;
  21. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  22. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  23. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  24. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  25. /* Add the variable node to the information model */
  26. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  27. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  28. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  29. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  30. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  31. parentReferenceNodeId, myIntegerName,
  32. UA_NODEID_NULL, attr, NULL, NULL);
  33. }
  34. /**
  35. * Now we change the value with the write service. This uses the same service
  36. * implementation that can also be reached over the network by an OPC UA client.
  37. */
  38. static void
  39. writeVariable(UA_Server *server) {
  40. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  41. /* Write a different integer value */
  42. UA_Int32 myInteger = 43;
  43. UA_Variant myVar;
  44. UA_Variant_init(&myVar);
  45. UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  46. UA_Server_writeValue(server, myIntegerNodeId, myVar);
  47. /* Set the status code of the value to an error code. The function
  48. * UA_Server_write provides access to the raw service. The above
  49. * UA_Server_writeValue is syntactic sugar for writing a specific node
  50. * attribute with the write service. */
  51. UA_WriteValue wv;
  52. UA_WriteValue_init(&wv);
  53. wv.nodeId = myIntegerNodeId;
  54. wv.attributeId = UA_ATTRIBUTEID_VALUE;
  55. wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
  56. wv.value.hasStatus = true;
  57. UA_Server_write(server, &wv);
  58. /* Reset the variable to a good statuscode with a value */
  59. wv.value.hasStatus = false;
  60. wv.value.value = myVar;
  61. wv.value.hasValue = true;
  62. UA_Server_write(server, &wv);
  63. }
  64. /**
  65. * Note how we initially set the DataType attribute of the variable node to the
  66. * NodeId of the Int32 data type. This forbids writing values that are not an
  67. * Int32. The following code shows how this consistency check is performed for
  68. * every write.
  69. */
  70. static void
  71. writeWrongVariable(UA_Server *server) {
  72. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  73. /* Write a string */
  74. UA_String myString = UA_STRING("test");
  75. UA_Variant myVar;
  76. UA_Variant_init(&myVar);
  77. UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
  78. UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
  79. printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
  80. }
  81. /** It follows the main server code, making use of the above definitions. */
  82. UA_Boolean running = true;
  83. static void stopHandler(int sign) {
  84. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  85. running = false;
  86. }
  87. int main(void) {
  88. signal(SIGINT, stopHandler);
  89. signal(SIGTERM, stopHandler);
  90. UA_ServerConfig config = UA_ServerConfig_standard;
  91. UA_ServerNetworkLayer nl =
  92. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  93. config.networkLayers = &nl;
  94. config.networkLayersSize = 1;
  95. UA_Server *server = UA_Server_new(config);
  96. addVariable(server);
  97. writeVariable(server);
  98. writeWrongVariable(server);
  99. UA_Server_run(server, &running);
  100. UA_Server_delete(server);
  101. nl.deleteMembers(&nl);
  102. return 0;
  103. }