Browse Source

Merge remote-tracking branch 'origin/0.2'

Julius Pfrommer 8 years ago
parent
commit
464a00deef
5 changed files with 43 additions and 68 deletions
  1. 22 39
      README.md
  2. 2 2
      doc/building.rst
  3. 8 16
      examples/client.c
  4. 2 2
      plugins/ua_network_tcp.c
  5. 9 9
      src/client/ua_client_highlevel.c

+ 22 - 39
README.md

@@ -14,7 +14,7 @@ open62541 is licensed under the LGPL with static linking exception. That means *
 ### Using open62541
 A general introduction to OPC UA and the open62541 documentation can be found at http://open62541.org/doc/current.
 Past releases of the library can be downloaded at https://github.com/open62541/open62541/releases.
-To use the latest improvements, download a recent build of the *single-file distributions* (the entire library merged into a single source and header file) from http://open62541.org/releases.
+To use the latest improvements, download a recent build of the *single-file distribution* (the entire library merged into a single source and header file) from http://open62541.org/releases.
 Recent MSVC binaries of the library are available [here](https://ci.appveyor.com/project/Stasik0/open62541/build/artifacts).
 
 
@@ -30,14 +30,12 @@ As an open source project, we invite new contributors to help improving open6254
 - Work on issues marked as "[easy hacks](https://github.com/open62541/open62541/labels/easy%20hack)"
 
 ### Example Server Implementation
-Compile the examples with the single file distribution `open62541.*` header and source files.
-With the GCC compiler, just run ```gcc -std=c99 <server.c> open62541.c -o server```.
+Compile the examples with the single-file distribution `open62541.h/.c` header and source file.
+Using the GCC compiler, just run ```gcc -std=c99 <server.c> open62541.c -o server```.
 ```c
 #include <signal.h>
 #include "open62541.h"
 
-#define PORT 16664
-
 UA_Boolean running = true;
 void signalHandler(int sig) {
     running = false;
@@ -45,12 +43,11 @@ void signalHandler(int sig) {
 
 int main(int argc, char** argv)
 {
-    /* catch ctrl-c */
-    signal(SIGINT, signalHandler);
+    signal(SIGINT, signalHandler); /* catch ctrl-c */
 
-    /* init the server */
-    UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, PORT);
+    /* create a server with one networklayer listening on port 4840 */
     UA_ServerConfig config = UA_ServerConfig_standard;
+    UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 4840);
     config.networkLayers = &nl;
     config.networkLayersSize = 1;
     UA_Server *server = UA_Server_new(config);
@@ -75,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;
 }
 ```
 
@@ -91,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:16664");
-    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;
+    }
+
+    /* 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);
     }
 
-    /* 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;
+    UA_Variant_delete(val);
+    UA_Client_delete(client); /* disconnects the client first */
+    return status;
 }
 ```

+ 2 - 2
doc/building.rst

@@ -38,8 +38,8 @@ Building with CMake on Ubuntu or Debian
 Building with CMake on Windows
 ------------------------------
 
-Here we explain the build process for Visual Studio. To build with MinGW, just
-replace the compiler selection in the call to CMake.
+Here we explain the build process for Visual Studio (2013 or newer). To build
+with MinGW, just replace the compiler selection in the call to CMake.
 
 - Download and install
 

+ 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++;

+ 2 - 2
plugins/ua_network_tcp.c

@@ -600,7 +600,7 @@ UA_ClientConnectionTCP(UA_ConnectionConfig localConf, const char *endpointUrl, U
     }
 
     /* where does the port begin? */
-    size_t portpos = 9;
+    size_t portpos = 10;
     for(; portpos < urlLength-1; portpos++) {
         if(endpointUrl[portpos] == ':')
             break;
@@ -610,7 +610,7 @@ UA_ClientConnectionTCP(UA_ConnectionConfig localConf, const char *endpointUrl, U
     memcpy(hostname, &endpointUrl[10], portpos - 10);
     hostname[portpos-10] = 0;
 
-    const char *port = "4842";
+    const char *port = "4840";
     if(portpos < urlLength - 1)
         port = &endpointUrl[portpos + 1];
     else

+ 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