opcua 0.13 → 0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,142 +0,0 @@
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
- /**
5
- * Adding Variables to a Server
6
- * ----------------------------
7
- *
8
- * This tutorial shows how to work with data types and how to add variable nodes
9
- * to a server. First, we add a new variable to the server. Take a look at the
10
- * definition of the ``UA_VariableAttributes`` structure to see the list of all
11
- * attributes defined for VariableNodes.
12
- *
13
- * Note that the default settings have the AccessLevel of the variable value as
14
- * read only. See below for making the variable writable.
15
- */
16
-
17
- #include <open62541.h>
18
-
19
- #include <signal.h>
20
- #include <stdlib.h>
21
-
22
- static void
23
- addVariable(UA_Server *server) {
24
- /* Define the attribute of the myInteger variable node */
25
- UA_VariableAttributes attr = UA_VariableAttributes_default;
26
- UA_Int32 myInteger = 42;
27
- UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
28
- attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
29
- attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
30
- attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
31
- attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
32
-
33
- /* Add the variable node to the information model */
34
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
35
- UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
36
- UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
37
- UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
38
- UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
39
- parentReferenceNodeId, myIntegerName,
40
- UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
41
- }
42
-
43
- static UA_NodeId pointTypeId;
44
- static void addVariableType2DPoint(UA_Server *server) {
45
- UA_VariableAttributes vtAttr = UA_VariableAttributes_default;
46
- vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
47
- vtAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
48
- UA_UInt32 arrayDims[1] = {5};
49
- vtAttr.arrayDimensions = arrayDims;
50
- vtAttr.arrayDimensionsSize = 1;
51
- vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
52
- vtAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
53
- UA_NodeId myArrayNodeId = UA_NODEID_STRING(1, "MyArray");
54
- /* a matching default value is required */
55
- UA_Double zero[2] = {0.0, 0.0};
56
- UA_Variant_setArray(&vtAttr.value, zero, 5, &UA_TYPES[UA_TYPES_DOUBLE]);
57
-
58
- UA_Server_addVariableNode(server, myArrayNodeId,
59
- UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
60
- UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
61
- UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
62
- vtAttr, NULL, &pointTypeId);
63
- }
64
- /**
65
- * Now we change the value with the write service. This uses the same service
66
- * implementation that can also be reached over the network by an OPC UA client.
67
- */
68
-
69
- static void writeVariable(UA_Server *server) {
70
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
71
-
72
- /* Write a different integer value */
73
- UA_Int32 myInteger = 43;
74
- UA_Variant myVar;
75
- UA_Variant_init(&myVar);
76
- UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
77
- UA_Server_writeValue(server, myIntegerNodeId, myVar);
78
-
79
- /* Set the status code of the value to an error code. The function
80
- * UA_Server_write provides access to the raw service. The above
81
- * UA_Server_writeValue is syntactic sugar for writing a specific node
82
- * attribute with the write service. */
83
- UA_WriteValue wv;
84
- UA_WriteValue_init(&wv);
85
- wv.nodeId = myIntegerNodeId;
86
- wv.attributeId = UA_ATTRIBUTEID_VALUE;
87
- wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
88
- wv.value.hasStatus = true;
89
- UA_Server_write(server, &wv);
90
-
91
- /* Reset the variable to a good statuscode with a value */
92
- wv.value.hasStatus = false;
93
- wv.value.value = myVar;
94
- wv.value.hasValue = true;
95
- UA_Server_write(server, &wv);
96
- }
97
-
98
- /**
99
- * Note how we initially set the DataType attribute of the variable node to the
100
- * NodeId of the Int32 data type. This forbids writing values that are not an
101
- * Int32. The following code shows how this consistency check is performed for
102
- * every write.
103
- */
104
-
105
- static void
106
- writeWrongVariable(UA_Server *server) {
107
- UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
108
-
109
- /* Write a string */
110
- UA_String myString = UA_STRING("test");
111
- UA_Variant myVar;
112
- UA_Variant_init(&myVar);
113
- UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
114
- UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
115
- printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
116
- }
117
-
118
- /** It follows the main server code, making use of the above definitions. */
119
-
120
- UA_Boolean running = true;
121
- static void stopHandler(int sign) {
122
- UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
123
- running = false;
124
- }
125
-
126
- int main(void) {
127
- signal(SIGINT, stopHandler);
128
- signal(SIGTERM, stopHandler);
129
- UA_ServerConfig *config = UA_ServerConfig_new_default();
130
- UA_Server *server = UA_Server_new(config);
131
-
132
-
133
- addVariable(server);
134
- writeVariable(server);
135
- writeWrongVariable(server);
136
- addVariableType2DPoint(server);
137
-
138
- UA_StatusCode retval = UA_Server_run(server, &running);
139
-
140
- UA_Server_delete(server);
141
- return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
142
- }