From be40c6bbad18cee0444c50069d98967cd26b6c3b Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 19 十二月 2023 20:10:24 +0800 Subject: [PATCH] plc读写参数调整,读写方式调整 --- pkg/plc/apacheplc4x/modbus.go | 27 ++++++++++----------------- 1 files changed, 10 insertions(+), 17 deletions(-) diff --git a/pkg/plc/apacheplc4x/modbus.go b/pkg/plc/apacheplc4x/modbus.go index e828403..a6138eb 100644 --- a/pkg/plc/apacheplc4x/modbus.go +++ b/pkg/plc/apacheplc4x/modbus.go @@ -1,7 +1,6 @@ package apacheplc4x import ( - "apsClient/conf" "apsClient/pkg/logx" "context" "errors" @@ -10,7 +9,6 @@ "github.com/apache/plc4x/plc4go/pkg/api/drivers" apiModel "github.com/apache/plc4x/plc4go/pkg/api/model" "github.com/apache/plc4x/plc4go/pkg/api/transports" - "github.com/spf13/cast" "sync/atomic" "time" ) @@ -56,9 +54,9 @@ } } -func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) { +func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int, intType string) ([]byte, error) { tag := fmt.Sprintf("tag:%v", address) - tagAddress := getTagAddress(address, 1) + tagAddress := getTagAddress(address, 1, intType) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -86,9 +84,9 @@ } -func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) { +func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int, intType string) ([]byte, error) { tag := fmt.Sprintf("tag:%v:%v", address, length) - tagAddress := getTagAddress(address, length) + tagAddress := getTagAddress(address, length, intType) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -121,7 +119,7 @@ return result, nil } -func ReadHoldingRegister(ipAddr string, address, length int) ([]byte, error) { +func ReadHoldingRegister(ipAddr string, address, length int, intType string) ([]byte, error) { connection, err := GetModbusConnection(ipAddr) dealErr(err, ipAddr) if err != nil { @@ -129,14 +127,13 @@ } defer connection.Close() if length > 1 { - return readHoldingRegisterList(connection, address, length) + return readHoldingRegisterList(connection, address, length, intType) } - return readHoldingRegisterSingle(connection, address) + return readHoldingRegisterSingle(connection, address, intType) } -func getTagAddress(address int, length int) string { - intType := conf.Conf.PLC.ModbusIntType +func getTagAddress(address int, length int, intType string) string { if intType == "" { intType = "DINT" } @@ -147,7 +144,7 @@ } } -func WriteHoldingRegister(ipAddr string, address int, value any) (string, error) { +func WriteHoldingRegister(ipAddr string, address int, value any, intType string) (string, error) { connection, err := GetModbusConnection(ipAddr) dealErr(err, ipAddr) if err != nil { @@ -156,11 +153,7 @@ defer connection.Close() tag := fmt.Sprintf("tag:%v:w", address) var tagAddress string - if cast.ToInt32(value) > 2<<16 { - tagAddress = getTagAddress(address, 2) - } else { - tagAddress = getTagAddress(address, 1) - } + tagAddress = getTagAddress(address, 1, intType) // 鍐欐ā寮� writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build() -- Gitblit v1.8.0