瀏覽代碼

add client example to the readme

Julius Pfrommer 10 年之前
父節點
當前提交
162b134cfb
共有 1 個文件被更改,包括 37 次插入0 次删除
  1. 37 0
      README.md

+ 37 - 0
README.md

@@ -71,3 +71,40 @@ int main(int argc, char** argv) {
 	return retval;
 }
 ```
+
+### Example Client Implementation
+```c
+#include <stdio.h>
+
+#include "ua_client.h"
+#include "networklayer_tcp.h"
+
+int main(int argc, char *argv[]) {
+	UA_Client *client = UA_Client_new();
+	UA_ClientNetworkLayer nl = ClientNetworkLayerTCP_new(UA_ConnectionConfig_standard);
+    UA_StatusCode retval = UA_Client_connect(client, UA_ConnectionConfig_standard, nl,
+                                             "opc.tcp://localhost:16664");
+	if(retval != UA_STATUSCODE_GOOD)
+    	return retval;
+
+    UA_ReadRequest req;
+    UA_ReadRequest_init(&req);
+    req.nodesToRead = UA_ReadValueId_new();
+    req.nodesToReadSize = 1;
+    req.nodesToRead[0].nodeId = UA_NODEID_STATIC(1, 442); /* assume this node exists */
+    req.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE;
+
+    UA_ReadResponse resp = UA_Client_read(client, &req);
+    if(resp.responseHeader.serviceResult == UA_STATUSCODE_GOOD &&
+       resp.resultsSize > 0 && resp.results[0].hasValue &&
+       resp.results[0].value.data /* an empty array returns a null-ptr */ &&
+       resp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32])
+        printf("the answer is: %i\n", *(UA_Int32*)resp.results[0].value.data);
+
+    UA_ReadRequest_deleteMembers(&req);
+    UA_ReadResponse_deleteMembers(&resp);
+    UA_Client_disconnect(client);
+    UA_Client_delete(client);
+    return UA_STATUSCODE_GOOD;
+}
+```