帧间间隔时间10ms->100ms #11

Merged
Fuyao merged 2 commits from cfy-feat-fix into main 2025-12-29 11:49:49 +08:00

View File

@@ -1,6 +1,6 @@
package modbus package modbus
import ( import (
"time" "time"
"github.com/goburrow/serial" "github.com/goburrow/serial"
@@ -10,43 +10,46 @@ import (
// 1) satisfy the rtuLink interface and // 1) satisfy the rtuLink interface and
// 2) add Read() deadline/timeout support. // 2) add Read() deadline/timeout support.
type serialPortWrapper struct { type serialPortWrapper struct {
conf *serialPortConfig conf *serialPortConfig
port serial.Port port serial.Port
deadline time.Time deadline time.Time
} }
type serialPortConfig struct { type serialPortConfig struct {
Device string Device string
Speed uint Speed uint
DataBits uint DataBits uint
Parity uint Parity uint
StopBits uint StopBits uint
} }
func newSerialPortWrapper(conf *serialPortConfig) (spw *serialPortWrapper) { func newSerialPortWrapper(conf *serialPortConfig) (spw *serialPortWrapper) {
spw = &serialPortWrapper{ spw = &serialPortWrapper{
conf: conf, conf: conf,
} }
return return
} }
func (spw *serialPortWrapper) Open() (err error) { func (spw *serialPortWrapper) Open() (err error) {
var parity string var parity string
switch spw.conf.Parity { switch spw.conf.Parity {
case PARITY_NONE: parity = "N" case PARITY_NONE:
case PARITY_EVEN: parity = "E" parity = "N"
case PARITY_ODD: parity = "O" case PARITY_EVEN:
parity = "E"
case PARITY_ODD:
parity = "O"
} }
spw.port, err = serial.Open(&serial.Config{ spw.port, err = serial.Open(&serial.Config{
Address: spw.conf.Device, Address: spw.conf.Device,
BaudRate: int(spw.conf.Speed), BaudRate: int(spw.conf.Speed),
DataBits: int(spw.conf.DataBits), DataBits: int(spw.conf.DataBits),
Parity: parity, Parity: parity,
StopBits: int(spw.conf.StopBits), StopBits: int(spw.conf.StopBits),
Timeout: 10 * time.Millisecond, Timeout: 100 * time.Millisecond,
}) })
return return
@@ -64,11 +67,12 @@ func (spw *serialPortWrapper) Close() (err error) {
// attempting to read from the serial port. // attempting to read from the serial port.
// If Read() is called before the deadline, a read attempt to the serial port // If Read() is called before the deadline, a read attempt to the serial port
// is made. At this point, one of two things can happen: // is made. At this point, one of two things can happen:
// - the serial port's receive buffer has one or more bytes and port.Read() // - the serial port's receive buffer has one or more bytes and port.Read()
// returns immediately (partial or full read), // returns immediately (partial or full read),
// - the serial port's receive buffer is empty: port.Read() blocks for // - the serial port's receive buffer is empty: port.Read() blocks for
// up to 10ms and returns serial.ErrTimeout. The serial timeout error is // up to 10ms and returns serial.ErrTimeout. The serial timeout error is
// masked and Read() returns with no data. // masked and Read() returns with no data.
//
// As the higher-level methods use io.ReadFull(), Read() will be called // As the higher-level methods use io.ReadFull(), Read() will be called
// as many times as necessary until either enough bytes have been read or an // as many times as necessary until either enough bytes have been read or an
// error is returned (ErrRequestTimedOut or any other i/o error). // error is returned (ErrRequestTimedOut or any other i/o error).