From 30c549c7e4d63ab3ea5bd0a7cfd69fa89339df33 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 18 九月 2023 19:46:27 +0800 Subject: [PATCH] 增加重试,地址-1 --- pkg/plc/apacheplc4x/modbus.go | 19 ++++++++++++++----- 1 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/plc/apacheplc4x/modbus.go b/pkg/plc/apacheplc4x/modbus.go index f44a92a..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:UINT", 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:UINT[%d]", address, length) + tagAddress := getTagAddress(address) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -117,8 +118,8 @@ func ReadHoldingRegister(ipAddr string, address, length int) ([]byte, error) { connection, err := GetModbusConnection(ipAddr) + dealErr(err, ipAddr) if err != nil { - dealErr(err, ipAddr) return nil, err } defer connection.Close() @@ -129,15 +130,23 @@ 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) if err != nil { - dealErr(err, ipAddr) return "", err } defer connection.Close() tag := fmt.Sprintf("tag:%v:w", address) - tagAddress := fmt.Sprintf("holding-register:%d:UINT", address) + tagAddress := getTagAddress(address) // 鍐欐ā寮� writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build() -- Gitblit v1.8.0