From 88da1e13a073e8b5656387a246d827593fbd6163 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 04 八月 2023 18:25:59 +0800
Subject: [PATCH] 添加设备查询,修改, 采集数据上报

---
 collector/plc4x.go |   41 +++++++++++++++++++++++++++++++++++++++--
 1 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/collector/plc4x.go b/collector/plc4x.go
index 5e7966d..8d5cb1b 100644
--- a/collector/plc4x.go
+++ b/collector/plc4x.go
@@ -35,8 +35,45 @@
 	return connectionResult.GetConnection(), nil
 }
 
-func ReadHoldingRegister(connection plc4go.PlcConnection, address int) ([]byte, error) {
-	tagAddress := fmt.Sprintf("holding-register:%d:DINT", address)
+func ReadHoldingRegister(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
+	if length > 1 {
+		return ReadHoldingRegisterList(connection, address, length)
+	}
+
+	return ReadHoldingRegisterSingle(connection, address)
+}
+
+func ReadHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
+	tagAddress := fmt.Sprintf("holding-register:%d:UINT", address)
+
+	// 璇绘ā寮�
+	readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()
+	if err != nil {
+		fmt.Printf("preparing read-request:%s\n", err.Error())
+		return nil, err
+	}
+
+	// 鎵ц
+	readResult := <-readRequest.Execute()
+	if err := readResult.GetErr(); err != nil {
+		fmt.Printf("execting read-request:%s\n", err.Error())
+		return nil, err
+	}
+
+	// 鍒ゆ柇鍝嶅簲鐮佹槸鍚︽纭�
+	if readResult.GetResponse().GetResponseCode("tag") != apiModel.PlcResponseCode_OK {
+		fmt.Printf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode("tag").GetName())
+		return nil, nil
+	}
+
+	value := readResult.GetResponse().GetValue("tag")
+
+	return value.GetRaw(), err
+
+}
+
+func ReadHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
+	tagAddress := fmt.Sprintf("holding-register:%d:UINT[%d]", address, length)
 
 	// 璇绘ā寮�
 	readRequest, err := connection.ReadRequestBuilder().AddTagAddress("tag", tagAddress).Build()

--
Gitblit v1.8.0