Skip to content

Commit

Permalink
Windows: return immediately when there are bytes available instead of…
Browse files Browse the repository at this point in the history
… waiting for the read timeout when doing a read with a timeout. Fixes #31
  • Loading branch information
Tarmigan Casebolt committed Nov 11, 2015
1 parent d381340 commit 0b54e01
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions serial_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,17 @@ func setCommTimeouts(h syscall.Handle, readTimeout time.Duration) error {
var timeouts structTimeouts
const MAXDWORD = 1<<32 - 1

// blocking read by default
var timeoutMs int64 = MAXDWORD

if readTimeout > 0 {
// non-blocking read
timeoutMs := readTimeout.Nanoseconds() / 1e6
if timeoutMs < 1 {
timeoutMs = 1
} else if timeoutMs > MAXDWORD {
timeoutMs = MAXDWORD
} else if timeoutMs > MAXDWORD-1 {
timeoutMs = MAXDWORD - 1
}
timeouts.ReadIntervalTimeout = 0
timeouts.ReadTotalTimeoutMultiplier = 0
timeouts.ReadTotalTimeoutConstant = uint32(timeoutMs)
} else {
// blocking read
timeouts.ReadIntervalTimeout = MAXDWORD
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD
timeouts.ReadTotalTimeoutConstant = MAXDWORD - 1
}

/* From http://msdn.microsoft.com/en-us/library/aa363190(v=VS.85).aspx
Expand All @@ -232,6 +227,10 @@ func setCommTimeouts(h syscall.Handle, readTimeout time.Duration) error {
ReadTotalTimeoutConstant, ReadFile times out.
*/

timeouts.ReadIntervalTimeout = MAXDWORD
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD
timeouts.ReadTotalTimeoutConstant = uint32(timeoutMs)

r, _, err := syscall.Syscall(nSetCommTimeouts, 2, uintptr(h), uintptr(unsafe.Pointer(&timeouts)), 0)
if r == 0 {
return err
Expand Down

0 comments on commit 0b54e01

Please sign in to comment.