From 83b652cd2f1be2375d33a67366a7c359cf6e22c0 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 18 九月 2023 14:56:09 +0800
Subject: [PATCH] 支持几个配置,支持按配置选择plc驱动包

---
 pkg/plc/apacheplc4x/modbus.go |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/pkg/plc/apacheplc4x/modbus.go b/pkg/plc/apacheplc4x/modbus.go
index d9ef045..7555866 100644
--- a/pkg/plc/apacheplc4x/modbus.go
+++ b/pkg/plc/apacheplc4x/modbus.go
@@ -1,6 +1,7 @@
 package apacheplc4x
 
 import (
+	"apsClient/conf"
 	"apsClient/pkg/logx"
 	"context"
 	"errors"
@@ -52,7 +53,7 @@
 
 func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) {
 	tag := fmt.Sprintf("tag:%v", address)
-	tagAddress := fmt.Sprintf("holding-register:%d:DINT", address)
+	tagAddress := getTagAddress(address)
 
 	// 璇绘ā寮�
 	readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
@@ -82,7 +83,7 @@
 
 func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) {
 	tag := fmt.Sprintf("tag:%v:%v", address, length)
-	tagAddress := fmt.Sprintf("holding-register:%d:DINT[%d]", address, length)
+	tagAddress := getTagAddress(address)
 
 	// 璇绘ā寮�
 	readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build()
@@ -129,6 +130,14 @@
 	return readHoldingRegisterSingle(connection, address)
 }
 
+func getTagAddress(address int) string {
+	intType := conf.Conf.PLC.ModbusIntType
+	if intType == "" {
+		intType = "DINT"
+	}
+	return fmt.Sprintf("holding-register:%d:%v", address, intType)
+}
+
 func WriteHoldingRegister(ipAddr string, address int, value any) (string, error) {
 	connection, err := GetModbusConnection(ipAddr)
 	dealErr(err, ipAddr)
@@ -137,7 +146,7 @@
 	}
 	defer connection.Close()
 	tag := fmt.Sprintf("tag:%v:w", address)
-	tagAddress := fmt.Sprintf("holding-register:%d:DINT", address)
+	tagAddress := getTagAddress(address)
 
 	// 鍐欐ā寮�
 	writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build()

--
Gitblit v1.8.0