From 2d6875c93b25d0b7336c7fa11e066d213259fe2e Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 25 四月 2024 10:02:01 +0800 Subject: [PATCH] 更改定时任务时区设置 --- pkg/plc/modbusx/modbusrtu.go | 57 ++++++++++++--------------------------------------------- 1 files changed, 12 insertions(+), 45 deletions(-) diff --git a/pkg/plc/modbusx/modbusrtu.go b/pkg/plc/modbusx/modbusrtu.go index 46348cf..62dac2e 100644 --- a/pkg/plc/modbusx/modbusrtu.go +++ b/pkg/plc/modbusx/modbusrtu.go @@ -1,72 +1,39 @@ package modbusx import ( - "apsClient/constvar" "apsClient/model/common" - "github.com/goburrow/modbus" - "time" ) func ReadByRTU(c *common.RTUConfig, address uint16, quantity uint16) (data []byte, err error) { address-- - cli, err := getModbusRTUConnection(c) - if err != nil { - return nil, err - } + cli := getModbusRTUConnection(c) data, err = cli.ReadHoldingRegisters(address, quantity) + dealRTUErr(err, c.SerialName) if err != nil { - cli, err = getModbusRTUConnection(c) - if err != nil { - return nil, err - } + cli = getModbusRTUConnection(c) data, err = cli.ReadHoldingRegisters(address, quantity) + dealRTUErr(err, c.SerialName) } return } -func WriteByRTU(c *common.RTUConfig, address uint16, value int) (err error) { +func WriteByRTU(c *common.RTUConfig, address uint16, value int, endian string, length int) (err error) { address-- var bytesVal []byte - bytesVal = intToBytes(value) - - cli, err := getModbusRTUConnection(c) - if err != nil { - return err - } + bytesVal = intToBytes(value, endian, length) + cli := getModbusRTUConnection(c) _, err = cli.WriteMultipleRegisters(address, uint16(len(bytesVal)), bytesVal) + dealRTUErr(err, c.SerialName) if err != nil { - cli, err = getModbusRTUConnection(c) - if err != nil { - return err - } + cli = getModbusRTUConnection(c) _, err = cli.WriteMultipleRegisters(address, uint16(len(bytesVal)), bytesVal) + dealRTUErr(err, c.SerialName) } return err } -func getModbusRTUConnection(c *common.RTUConfig) (cli modbus.Client, err error) { - // Modbus RTU/ASCII - h := modbus.NewRTUClientHandler(c.SerialName) - h.BaudRate = c.BaudRate - h.DataBits = c.DataBit - switch c.Parity { - case constvar.ParityEven: - h.Parity = "E" - case constvar.ParityOdd: - h.Parity = "O" - case constvar.ParityNull: - h.Parity = "N" - } - - h.StopBits = c.StopBit - h.SlaveId = 1 - h.Timeout = 5 * time.Second - - err = h.Connect() +func dealRTUErr(err error, serialName string) { if err != nil { - return nil, err + unsetModbusRTUConnection(serialName) //澶辫触鍒欏垹闄ょ紦瀛樼殑杩炴帴 } - defer h.Close() - cli = modbus.NewClient(h) - return } -- Gitblit v1.8.0