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/modbusrtu.go | 165 +++++++++++++++++++++++++++--------------------------- 1 files changed, 83 insertions(+), 82 deletions(-) diff --git a/pkg/plc/apacheplc4x/modbusrtu.go b/pkg/plc/apacheplc4x/modbusrtu.go index 32a6169..eed90c6 100644 --- a/pkg/plc/apacheplc4x/modbusrtu.go +++ b/pkg/plc/apacheplc4x/modbusrtu.go @@ -1,84 +1,85 @@ package apacheplc4x -import ( - "apsClient/model/common" - "apsClient/pkg/logx" - "context" - "errors" - "fmt" - plc4go "github.com/apache/plc4x/plc4go/pkg/api" - apiModel "github.com/apache/plc4x/plc4go/pkg/api/model" - "github.com/spf13/cast" - "time" -) - -func newModbusRTUConnection(c *common.RTUConfig) (plc4go.PlcConnection, error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) - // 鍒涘缓涓�涓柊鐨� PLC 杩炴帴 - connectionString := fmt.Sprintf("modbus-rtu:serial://%s?baudrate=%d&databits=%d&stopbits=%d&parity=%s", c.SerialName, c.BaudRate, c.DataBit, c.StopBit, c.Parity.ToString()) - connectionRequestChanel := driverManager.GetConnection(connectionString) - // 绛夊緟杩炴帴鍝嶅簲锛屽悓鏃惰�冭檻涓婁笅鏂囩殑瓒呮椂 - select { - case connectionResult := <-connectionRequestChanel: - cancel() - if err := connectionResult.GetErr(); err != nil { - return nil, err - } - return connectionResult.GetConnection(), nil - case <-ctx.Done(): - cancel() - return nil, errors.New("connect plc by modbusRTU timeout") - } -} - -func ReadHoldingRegisterByRTU(c *common.RTUConfig, address, length int) ([]byte, error) { - connection, err := newModbusRTUConnection(c) - if err != nil { - return nil, err - } - defer connection.Close() - if length > 1 { - return readHoldingRegisterList(connection, address, length) - } - - return readHoldingRegisterSingle(connection, address) -} - -func WriteHoldingRegisterByRTU(c *common.RTUConfig, address int, value any) (string, error) { - connection, err := newModbusRTUConnection(c) - if err != nil { - return "", err - } - 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) - } - - // 鍐欐ā寮� - writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build() - if err != nil { - logx.Errorf("plc4x preparing read-request:%s\n", err.Error()) - return "", err - } - - // 鎵ц - writeResult := <-writeRequest.Execute() - if err := writeResult.GetErr(); err != nil { - logx.Errorf("plc4x execute write-request:%s\n", err.Error()) - return "", err - } - - // 鍒ゆ柇鍝嶅簲鐮佹槸鍚︽纭� - if writeResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK { - logx.Errorf("plc4x response error code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName()) - return "", errors.New("error code: " + writeResult.GetResponse().GetResponseCode(tag).GetName()) - } - - result := writeResult.GetResponse().String() - - return result, nil -} +// +//import ( +// "apsClient/model/common" +// "apsClient/pkg/logx" +// "context" +// "errors" +// "fmt" +// plc4go "github.com/apache/plc4x/plc4go/pkg/api" +// apiModel "github.com/apache/plc4x/plc4go/pkg/api/model" +// "github.com/spf13/cast" +// "time" +//) +// +//func newModbusRTUConnection(c *common.RTUConfig) (plc4go.PlcConnection, error) { +// ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) +// // 鍒涘缓涓�涓柊鐨� PLC 杩炴帴 +// connectionString := fmt.Sprintf("modbus-rtu:serial://%s?baudrate=%d&databits=%d&stopbits=%d&parity=%s", c.SerialName, c.BaudRate, c.DataBit, c.StopBit, c.Parity.ToString()) +// connectionRequestChanel := driverManager.GetConnection(connectionString) +// // 绛夊緟杩炴帴鍝嶅簲锛屽悓鏃惰�冭檻涓婁笅鏂囩殑瓒呮椂 +// select { +// case connectionResult := <-connectionRequestChanel: +// cancel() +// if err := connectionResult.GetErr(); err != nil { +// return nil, err +// } +// return connectionResult.GetConnection(), nil +// case <-ctx.Done(): +// cancel() +// return nil, errors.New("connect plc by modbusRTU timeout") +// } +//} +// +//func ReadHoldingRegisterByRTU(c *common.RTUConfig, address, length int) ([]byte, error) { +// connection, err := newModbusRTUConnection(c) +// if err != nil { +// return nil, err +// } +// defer connection.Close() +// if length > 1 { +// return readHoldingRegisterList(connection, address, length) +// } +// +// return readHoldingRegisterSingle(connection, address) +//} +// +//func WriteHoldingRegisterByRTU(c *common.RTUConfig, address int, value any) (string, error) { +// connection, err := newModbusRTUConnection(c) +// if err != nil { +// return "", err +// } +// 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) +// } +// +// // 鍐欐ā寮� +// writeRequest, err := connection.WriteRequestBuilder().AddTagAddress(tag, tagAddress, value).Build() +// if err != nil { +// logx.Errorf("plc4x preparing read-request:%s\n", err.Error()) +// return "", err +// } +// +// // 鎵ц +// writeResult := <-writeRequest.Execute() +// if err := writeResult.GetErr(); err != nil { +// logx.Errorf("plc4x execute write-request:%s\n", err.Error()) +// return "", err +// } +// +// // 鍒ゆ柇鍝嶅簲鐮佹槸鍚︽纭� +// if writeResult.GetResponse().GetResponseCode(tag) != apiModel.PlcResponseCode_OK { +// logx.Errorf("plc4x response error code: %s", writeResult.GetResponse().GetResponseCode(tag).GetName()) +// return "", errors.New("error code: " + writeResult.GetResponse().GetResponseCode(tag).GetName()) +// } +// +// result := writeResult.GetResponse().String() +// +// return result, nil +//} -- Gitblit v1.8.0