Merge pull request #147 from cmaglie/remove_windows_timeout_loop

Remove unnecessary code, originally for checking for disconnects when using non-overlapped I/O
This commit is contained in:
Cristian Maglie
2022-11-29 11:12:13 +01:00
committed by GitHub

View File

@@ -26,7 +26,6 @@ import (
type windowsPort struct { type windowsPort struct {
mu sync.Mutex mu sync.Mutex
handle syscall.Handle handle syscall.Handle
readTimeoutCycles int64
} }
func nativeGetPortsList() ([]string, error) { func nativeGetPortsList() ([]string, error) {
@@ -83,9 +82,7 @@ func (port *windowsPort) Read(p []byte) (int, error) {
} }
defer syscall.CloseHandle(ev.HEvent) defer syscall.CloseHandle(ev.HEvent)
cycles := int64(0) err = syscall.ReadFile(port.handle, p, &readed, ev)
for {
err := syscall.ReadFile(port.handle, p, &readed, ev)
if err == syscall.ERROR_IO_PENDING { if err == syscall.ERROR_IO_PENDING {
err = getOverlappedResult(port.handle, ev, &readed, true) err = getOverlappedResult(port.handle, ev, &readed, true)
} }
@@ -99,34 +96,13 @@ func (port *windowsPort) Read(p []byte) (int, error) {
// error happened // error happened
return int(readed), err return int(readed), err
} }
if readed > 0 { if readed > 0 {
return int(readed), nil return int(readed), nil
} }
if err := resetEvent(ev.HEvent); err != nil {
return 0, err
}
if port.readTimeoutCycles != -1 {
cycles++
if cycles == port.readTimeoutCycles {
// Timeout // Timeout
return 0, nil return 0, nil
} }
}
// At the moment it seems that the only reliable way to check if
// a serial port is alive in Windows is to check if the SetCommState
// function fails.
params := &dcb{}
getCommState(port.handle, params)
if err := setCommState(port.handle, params); err != nil {
port.Close()
return 0, err
}
}
}
func (port *windowsPort) Write(p []byte) (int, error) { func (port *windowsPort) Write(p []byte) (int, error) {
var writed uint32 var writed uint32
@@ -383,26 +359,24 @@ func (port *windowsPort) GetModemStatusBits() (*ModemStatusBits, error) {
} }
func (port *windowsPort) SetReadTimeout(timeout time.Duration) error { func (port *windowsPort) SetReadTimeout(timeout time.Duration) error {
var cycles, cycleDuration int64 commTimeouts := &commTimeouts{
if timeout == NoTimeout {
cycles = -1
cycleDuration = 1000
} else {
cycles = timeout.Milliseconds()/1000 + 1
cycleDuration = timeout.Milliseconds() / cycles
}
err := setCommTimeouts(port.handle, &commTimeouts{
ReadIntervalTimeout: 0xFFFFFFFF, ReadIntervalTimeout: 0xFFFFFFFF,
ReadTotalTimeoutMultiplier: 0xFFFFFFFF, ReadTotalTimeoutMultiplier: 0xFFFFFFFF,
ReadTotalTimeoutConstant: uint32(cycleDuration), ReadTotalTimeoutConstant: 0xFFFFFFFE,
WriteTotalTimeoutConstant: 0, WriteTotalTimeoutConstant: 0,
WriteTotalTimeoutMultiplier: 0, WriteTotalTimeoutMultiplier: 0,
}) }
if err != nil { if timeout != NoTimeout {
ms := timeout.Milliseconds()
if ms > 0xFFFFFFFE || ms < 0 {
return &PortError{code: InvalidTimeoutValue}
}
commTimeouts.ReadTotalTimeoutConstant = uint32(ms)
}
if err := setCommTimeouts(port.handle, commTimeouts); err != nil {
return &PortError{code: InvalidTimeoutValue, causedBy: err} return &PortError{code: InvalidTimeoutValue, causedBy: err}
} }
port.readTimeoutCycles = cycles
return nil return nil
} }