Browse Source

use highlevel API for the readme example

Julius Pfrommer 8 years ago
parent
commit
d51eb62a39
3 changed files with 33 additions and 55 deletions
  1. 16 30
      README.md
  2. 8 16
      examples/client.c
  3. 9 9
      src/client/ua_client_highlevel.c

+ 16 - 30
README.md

@@ -72,10 +72,10 @@ int main(int argc, char** argv)
                               browseName, variableType, attr, NULL, NULL);
 
     /* run the server loop */
-    UA_StatusCode retval = UA_Server_run(server, &running);
+    UA_StatusCode status = UA_Server_run(server, &running);
     UA_Server_delete(server);
     nl.deleteMembers(&nl);
-    return retval;
+    return status;
 }
 ```
 
@@ -88,36 +88,22 @@ int main(int argc, char *argv[])
 {
     /* create a client and connect */
     UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
-    UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
-    if(retval != UA_STATUSCODE_GOOD) {
+    UA_StatusCode status = UA_Client_connect(client, "opc.tcp://localhost:4840");
+    if(status != UA_STATUSCODE_GOOD) {
         UA_Client_delete(client);
-        return retval;
+        return status;
     }
 
-    /* create a readrequest with one entry */
-    UA_ReadRequest req;
-    UA_ReadRequest_init(&req);
-    req.nodesToRead = UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
-    req.nodesToReadSize = 1;
-
-    /* define the node and attribute to be read */
-    req.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
-    req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
-
-    /* call the service and print the result */
-    UA_ReadResponse resp = UA_Client_Service_read(client, req);
-    if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
-       resp.resultsSize > 0 && resp.results[0].hasValue &&
-       UA_Variant_isScalar(&resp.results[0].value) &&
-       resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
-           UA_Int32 *value = (UA_Int32*)resp.results[0].value.data;
-           printf("the value is: %i\n", *value);
-   }
-
-    UA_ReadRequest_deleteMembers(&req);
-    UA_ReadResponse_deleteMembers(&resp);
-    UA_Client_disconnect(client);
-    UA_Client_delete(client);
-    return UA_STATUSCODE_GOOD;
+    /* read the value attribute of the node into the val variable */
+    UA_Variant *val = UA_Variant_new();
+    status = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
+    if(status == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
+       val->type == &UA_TYPES[UA_TYPES_INT32]) {
+            printf("the value is: %i\n", *(UA_Int32*)val->data);
+    }
+
+    UA_Variant_delete(val);
+    UA_Client_delete(client); /* disconnects the client first */
+    return status;
 }
 ```

+ 8 - 16
examples/client.c

@@ -121,24 +121,16 @@ int main(int argc, char *argv[]) {
 #endif
 
     /* Read attribute */
-    printf("\nReading the value of node (1, \"the.answer\"):\n");
     UA_Int32 value = 0;
-    UA_ReadRequest rReq;
-    UA_ReadRequest_init(&rReq);
-    rReq.nodesToRead =  UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]);
-    rReq.nodesToReadSize = 1;
-    rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */
-    rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE; /* return the value attribute */
-    UA_ReadResponse rResp = UA_Client_Service_read(client, rReq);
-    if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
-       rResp.resultsSize > 0 && rResp.results[0].hasValue &&
-       UA_Variant_isScalar(&rResp.results[0].value) &&
-       rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) {
-        value = *(UA_Int32*)rResp.results[0].value.data;
-        printf("the value is: %i\n", value);
+    printf("\nReading the value of node (1, \"the.answer\"):\n");
+    UA_Variant *val = UA_Variant_new();
+    retval = UA_Client_readValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), val);
+    if(retval == UA_STATUSCODE_GOOD && UA_Variant_isScalar(val) &&
+       val->type == &UA_TYPES[UA_TYPES_INT32]) {
+            value = *(UA_Int32*)val->data;
+            printf("the value is: %i\n", value);
     }
-    UA_ReadRequest_deleteMembers(&rReq);
-    UA_ReadResponse_deleteMembers(&rResp);
+    UA_Variant_delete(val);
 
     /* Write node attribute */
     value++;

+ 9 - 9
src/client/ua_client_highlevel.c

@@ -7,17 +7,17 @@
 
 UA_StatusCode
 UA_Client_NamespaceGetIndex(UA_Client *client, UA_String *namespaceUri, UA_UInt16 *namespaceIndex) {
-	UA_ReadRequest request;
-	UA_ReadRequest_init(&request);
+    UA_ReadRequest request;
+    UA_ReadRequest_init(&request);
     UA_ReadValueId id;
-	id.attributeId = UA_ATTRIBUTEID_VALUE;
-	id.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY);
-	request.nodesToRead = &id;
-	request.nodesToReadSize = 1;
+    id.attributeId = UA_ATTRIBUTEID_VALUE;
+    id.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_NAMESPACEARRAY);
+    request.nodesToRead = &id;
+    request.nodesToReadSize = 1;
 
-	UA_ReadResponse response = UA_Client_Service_read(client, request);
+    UA_ReadResponse response = UA_Client_Service_read(client, request);
 
-	UA_StatusCode retval = UA_STATUSCODE_GOOD;
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
     if(response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
         retval = response.responseHeader.serviceResult;
     else if(response.resultsSize != 1 || !response.results[0].hasValue)
@@ -41,7 +41,7 @@ UA_Client_NamespaceGetIndex(UA_Client *client, UA_String *namespaceUri, UA_UInt1
     }
 
     UA_ReadResponse_deleteMembers(&response);
-	return retval;
+    return retval;
 }
 
 UA_StatusCode