OPC UA JAVA開發筆記(六):milo的數組數據寫入

今天研究了一下OPC UA milo中有關數組數據的寫入,算是對之前的數據寫入的一個補充。

首先我們先定義一個數組的節點
image-20200518171910290
這裏是通過opcua-modeler來建立節點對象。主要是設置ValueRank爲OneDimension,然後AccessLevel的讀寫都要圈上。

啓動Server。至於結點解析可以看我上一篇。

我們在官方的WriteExample中修改一點:

public class WriteExample implements ClientExample {

    public static void main(String[] args) throws Exception {
        WriteExample example = new WriteExample();
		// 這裏改成false表示用本地Server運行
        new ClientExampleRunner(example, false).run();
    }

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
        // synchronous connect
        client.connect().get();
		//這個是節點的NodeId
        List<NodeId> nodeIds = ImmutableList.of(new NodeId(2, 139));

        for (int i = 0; i < 10; i++) {
            // 這裏是需要寫入的數據,注意要把集合轉換成對應類型的數組
            String[] array = ImmutableList.of(String.valueOf(i), String.valueOf(i + 1)).toArray(new String[0]);
			
            //milo的所有類型數據都用Variant包裝
            Variant v = new Variant(array);
            
			// 再轉換成DataValue
            // don't write status or timestamps
            DataValue dv = new DataValue(v, null, null);

            // write asynchronously....
            CompletableFuture<List<StatusCode>> f =
                    client.writeValues(nodeIds, ImmutableList.of(dv));

            // ...but block for the results so we write in order
            List<StatusCode> statusCodes = f.get();
            StatusCode status = statusCodes.get(0);

            if (status.isGood()) {
                logger.info("Wrote '{}' to nodeId={}", v, nodeIds.get(0));
            }
        }

        future.complete(client);
    }

}

總結:

  1. 最主要的是要設置正確數據的數組範圍,即ValueRank
  2. 傳入的數據必須是數組,然後類型要與OPC 結點的數據類型匹配
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章