From 07db2029d66a028a2d2256d2c158693c3a42b5f8 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 26 十月 2023 16:43:21 +0800 Subject: [PATCH] update --- service/plc.go | 6 ++ pkg/plc/plc4x.go | 74 +++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/pkg/plc/plc4x.go b/pkg/plc/plc4x.go index 68562a6..a7f9d25 100644 --- a/pkg/plc/plc4x.go +++ b/pkg/plc/plc4x.go @@ -1,12 +1,15 @@ package plc import ( + "apsClient/conf" "apsClient/pkg/logx" "context" "errors" "fmt" apiModel "github.com/apache/plc4x/plc4go/pkg/api/model" + "github.com/spf13/cast" "sync" + "sync/atomic" "time" plc4go "github.com/apache/plc4x/plc4go/pkg/api" @@ -107,7 +110,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, 1) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -137,7 +140,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, length) // 璇绘ā寮� readRequest, err := connection.ReadRequestBuilder().AddTagAddress(tag, tagAddress).Build() @@ -149,13 +152,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()) } @@ -178,27 +181,44 @@ return readHoldingRegisterSingle(connection, address) } +func getTagAddress(address int, length int) string { + intType := conf.Conf.PLC.ModbusIntType + if intType == "" { + intType = "DINT" + } + 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(connection plc4go.PlcConnection, address int, value any) (string, error) { tag := fmt.Sprintf("tag:%v:w", address) - tagAddress := fmt.Sprintf("holding-register:%d:UINT", 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()) } @@ -206,3 +226,41 @@ return result, nil } + +func dealErr(err error, ipAddr string) { + if err != nil { + FailureRemainingOpportunitiesDecr() //鍑忓皯澶辫触鍓╀綑鏈轰細 + } else { + FailureRemainingOpportunitiesReset() //閲嶇疆澶辫触鍓╀綑鏈轰細 + } +} + +var connectionStatus atomic.Bool + +var failureRemainingOpportunities atomic.Int64 + +const ( + defaultFailureRemainingOpportunities = 20 +) + +func IsConnect() bool { + return connectionStatus.Load() +} + +func FailureRemainingOpportunitiesDecr() { + newValue := failureRemainingOpportunities.Add(-1) + if newValue <= 0 { + connectionStatus.Store(false) + } + return +} + +func FailureRemainingOpportunitiesReset() { + if failureRemainingOpportunities.Load() < defaultFailureRemainingOpportunities { + failureRemainingOpportunities.Store(defaultFailureRemainingOpportunities) + } + if connectionStatus.Load() == false { + connectionStatus.Store(true) + } + return +} diff --git a/service/plc.go b/service/plc.go index 1a674b2..183cb00 100644 --- a/service/plc.go +++ b/service/plc.go @@ -130,7 +130,11 @@ if err != nil { return nil, err } - return plc.ReadHoldingRegister(conn, address, length) + newLength := length / 2 + if newLength == 0 { + newLength = 1 + } + return plc.ReadHoldingRegister(conn, address, newLength) } else { return modbusx.Read(ipAddr, uint16(address), uint16(length)) } -- Gitblit v1.8.0