From 0b54e010e77bc9d755ef7f3dfe0e98f3c14f5b3d Mon Sep 17 00:00:00 2001 From: Tarmigan Casebolt Date: Tue, 10 Nov 2015 22:11:44 -0800 Subject: [PATCH] Windows: return immediately when there are bytes available instead of waiting for the read timeout when doing a read with a timeout. Fixes #31 --- serial_windows.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/serial_windows.go b/serial_windows.go index 3c747bc..126e1e6 100644 --- a/serial_windows.go +++ b/serial_windows.go @@ -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 @@ -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