From 828fd69acb4191b1ec41e3b2f9842f2e08894772 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期五, 27 十月 2023 16:11:30 +0800 Subject: [PATCH] add log --- pkg/plc/apacheplc4x/modbus.go | 34 ++++++++++++++++++++++------------ 1 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/plc/apacheplc4x/modbus.go b/pkg/plc/apacheplc4x/modbus.go index 7555866..dd3621f 100644 --- a/pkg/plc/apacheplc4x/modbus.go +++ b/pkg/plc/apacheplc4x/modbus.go @@ -10,6 +10,7 @@ "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" ) @@ -18,7 +19,7 @@ // 鍒涘缓涓�涓笂涓嬫枃锛屽苟璁剧疆 3 绉掕秴鏃� ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() - conn, err := newGetModbusConnection(ctx, ipAddr) + conn, err := newModbusConnection(ctx, ipAddr) if err != nil { logx.Errorf("new modbus connection err: %v", err.Error()) return nil, err @@ -26,7 +27,7 @@ return conn, nil } -func newGetModbusConnection(ctx context.Context, ipAddr string) (plc4go.PlcConnection, error) { +func newModbusConnection(ctx context.Context, ipAddr string) (plc4go.PlcConnection, error) { // 鍒涘缓椹卞姩绠$悊鍣� driverManager := plc4go.NewPlcDriverManager() // 娉ㄥ唽TCP浼犺緭 @@ -53,7 +54,7 @@ func readHoldingRegisterSingle(connection plc4go.PlcConnection, address int) ([]byte, error) { tag := fmt.Sprintf("tag:%v", address) - tagAddress := getTagAddress(address) + tagAddress := getTagAddress(address, 1) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -83,7 +84,7 @@ func readHoldingRegisterList(connection plc4go.PlcConnection, address, length int) ([]byte, error) { tag := fmt.Sprintf("tag:%v:%v", address, length) - tagAddress := getTagAddress(address) + tagAddress := getTagAddress(address, length) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -95,13 +96,13 @@ // 鎵ц readResult := <-readRequest.Execute() if err := readResult.GetErr(); err != nil { - logx.Errorf("execting read-request:%s\n", err.Error()) + logx.Errorf("plc4x execute read-request:%s\n", err.Error()) return nil, err } // 鍒ゆ柇鍝嶅簲鐮佹槸鍚︽纭� if readResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK { - logx.Errorf("error an non-ok return code: %s", readResult.GetResponse().GetResponseCode(tag).GetName()) + logx.Errorf("plc4x response error code: %s", readResult.GetResponse().GetResponseCode(tag).GetName()) return nil, errors.New("error code: " + readResult.GetResponse().GetResponseCode(tag).GetName()) } @@ -130,12 +131,16 @@ return readHoldingRegisterSingle(connection, address) } -func getTagAddress(address int) string { +func getTagAddress(address int, length int) string { intType := conf.Conf.PLC.ModbusIntType if intType == "" { intType = "DINT" } - return fmt.Sprintf("holding-register:%d:%v", address, intType) + if length == 1 { + return fmt.Sprintf("holding-register:%d:%v", address, intType) + } else { + return fmt.Sprintf("holding-register:%d:%v[%d]", address, intType, length) + } } func WriteHoldingRegister(ipAddr string, address int, value any) (string, error) { @@ -146,25 +151,30 @@ } defer connection.Close() tag := fmt.Sprintf("tag:%v:w", address) - tagAddress := getTagAddress(address) + var tagAddress string + if cast.ToInt32(value) > 2<<16 { + tagAddress = getTagAddress(address, 2) + } else { + tagAddress = getTagAddress(address, 1) + } // 鍐欐ā寮� writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build() if err != nil { - logx.Errorf("preparing read-request:%s\n", err.Error()) + logx.Errorf("plc4x preparing read-request:%s\n", err.Error()) return "", err } // 鎵ц writeResult := <-writeRequest.Execute() if err := writeResult.GetErr(); err != nil { - logx.Errorf("execting read-request:%s\n", err.Error()) + logx.Errorf("plc4x execute write-request:%s\n", err.Error()) return "", err } // 鍒ゆ柇鍝嶅簲鐮佹槸鍚︽纭� if writeResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK { - logx.Errorf("error an non-ok return code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName()) + logx.Errorf("plc4x response error code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName()) return "", errors.New("error code: " + writeResult.GetResponse().GetResponseCode(tag).GetName()) } -- Gitblit v1.8.0