123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
- * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
- /**
- * Adding Variables to a Server
- * ----------------------------
- *
- * This tutorial shows how to work with data types and how to add variable nodes
- * to a server. First, we add a new variable to the server. Take a look at the
- * definition of the ``UA_VariableAttributes`` structure to see the list of all
- * attributes defined for VariableNodes.
- *
- * Note that the default settings have the AccessLevel of the variable value as
- * read only. See below for making the variable writable.
- */
- #include <ua_server.h>
- #include <ua_config_default.h>
- #include <ua_log_stdout.h>
- #include <signal.h>
- #include <stdlib.h>
- static void
- addVariable(UA_Server *server) {
- /* Define the attribute of the myInteger variable node */
- UA_VariableAttributes attr = UA_VariableAttributes_default;
- UA_Int32 myInteger = 42;
- UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
- attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
- attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
- attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
- /* Add the variable node to the information model */
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
- UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
- UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
- parentReferenceNodeId, myIntegerName,
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
- }
- /**
- * Now we change the value with the write service. This uses the same service
- * implementation that can also be reached over the network by an OPC UA client.
- */
- static void
- writeVariable(UA_Server *server) {
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
- /* Write a different integer value */
- UA_Int32 myInteger = 43;
- UA_Variant myVar;
- UA_Variant_init(&myVar);
- UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
- UA_Server_writeValue(server, myIntegerNodeId, myVar);
- /* Set the status code of the value to an error code. The function
- * UA_Server_write provides access to the raw service. The above
- * UA_Server_writeValue is syntactic sugar for writing a specific node
- * attribute with the write service. */
- UA_WriteValue wv;
- UA_WriteValue_init(&wv);
- wv.nodeId = myIntegerNodeId;
- wv.attributeId = UA_ATTRIBUTEID_VALUE;
- wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
- wv.value.hasStatus = true;
- UA_Server_write(server, &wv);
- /* Reset the variable to a good statuscode with a value */
- wv.value.hasStatus = false;
- wv.value.value = myVar;
- wv.value.hasValue = true;
- UA_Server_write(server, &wv);
- }
- /**
- * Note how we initially set the DataType attribute of the variable node to the
- * NodeId of the Int32 data type. This forbids writing values that are not an
- * Int32. The following code shows how this consistency check is performed for
- * every write.
- */
- static void
- writeWrongVariable(UA_Server *server) {
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
- /* Write a string */
- UA_String myString = UA_STRING("test");
- UA_Variant myVar;
- UA_Variant_init(&myVar);
- UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
- UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
- printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
- }
- /** It follows the main server code, making use of the above definitions. */
- UA_Boolean running = true;
- static void stopHandler(int sign) {
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
- running = false;
- }
- int main(void) {
- signal(SIGINT, stopHandler);
- signal(SIGTERM, stopHandler);
- UA_ServerConfig *config = UA_ServerConfig_new_default();
- UA_Server *server = UA_Server_new(config);
- addVariable(server);
- writeVariable(server);
- writeWrongVariable(server);
- UA_StatusCode retval = UA_Server_run(server, &running);
- UA_Server_delete(server);
- UA_ServerConfig_delete(config);
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
- }
|