From 4b5cc5816ff9199dfaf2357c8972aad7d3ddcb7a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sat, 13 Dec 2014 20:28:51 +0430 Subject: [PATCH] Windows: Fixed Read, with USB disconnect detection --- serial/native_windows.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/serial/native_windows.go b/serial/native_windows.go index dc09e46..361b200 100644 --- a/serial/native_windows.go +++ b/serial/native_windows.go @@ -86,8 +86,27 @@ func (port windowsSerialPort) Close() error { return syscall.CloseHandle(port.Handle) } -func (port *windowsSerialPort) Read(p []byte) (n int, err error) { - return syscall.Read(port.Handle, p) +func (port windowsSerialPort) Read(p []byte) (int, error) { + var readed uint32 + params := &DCB{} + for { + if err := syscall.ReadFile(port.Handle, p, &readed, nil); err != nil { + return int(readed), err + } + if readed > 0 { + return int(readed), 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. + + GetCommState(port.Handle, params) + if err := SetCommState(port.Handle, params); err != nil { + port.Close() + return 0, err + } + } } func (port windowsSerialPort) Write(p []byte) (int, error) {