tutorial_server_variable.c 4.4 KB

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