X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=serialport.c;h=f74fea7bc82e6e2ef5d3d4e67b48095571c9ca7e;hb=f446cfbf652292f1167544078306cadb5f75ba3b;hp=4f42740a2388fe398034f13722e3aaaa9f4f2121;hpb=ad7498553c7951ae5a559bd25512297be253696c;p=libserialport.git diff --git a/serialport.c b/serialport.c index 4f42740..f74fea7 100644 --- a/serialport.c +++ b/serialport.c @@ -85,6 +85,7 @@ SP_API enum sp_return sp_get_port_by_name(const char *portname, struct sp_port * memcpy(port->name, portname, len); #ifdef _WIN32 + port->usb_path = NULL; port->hdl = INVALID_HANDLE_VALUE; #else port->fd = -1; @@ -887,6 +888,7 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, #ifdef _WIN32 DWORD bytes_read = 0; + DWORD wait_result = 0; /* Set timeout. */ port->timeouts.ReadIntervalTimeout = 0; @@ -908,10 +910,15 @@ SP_API enum sp_return sp_blocking_read(struct sp_port *port, void *buf, bytes_read = count; } - /* Start background operation for subsequent events. */ - if (WaitCommEvent(port->hdl, &port->events, &port->wait_ovl) == 0) { - if (GetLastError() != ERROR_IO_PENDING) - RETURN_FAIL("WaitCommEvent() failed"); + /* Restart wait operation if needed. */ + if (GetOverlappedResult(port->hdl, &port->wait_ovl, &wait_result, FALSE)) { + /* Previous wait completed, start a new one. */ + if (WaitCommEvent(port->hdl, &port->events, &port->wait_ovl) == 0) { + if (GetLastError() != ERROR_IO_PENDING) + RETURN_FAIL("WaitCommEvent() failed"); + } + } else if (GetLastError() != ERROR_IO_INCOMPLETE) { + RETURN_FAIL("GetOverlappedResult() failed"); } RETURN_INT(bytes_read); @@ -993,6 +1000,7 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, #ifdef _WIN32 DWORD bytes_read; + DWORD wait_result; /* Set timeout. */ port->timeouts.ReadIntervalTimeout = MAXDWORD; @@ -1008,12 +1016,15 @@ SP_API enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0) RETURN_FAIL("GetOverlappedResult() failed"); - if (bytes_read > 0) { - /* Start background operation for subsequent events. */ + /* Restart wait operation if needed. */ + if (GetOverlappedResult(port->hdl, &port->wait_ovl, &wait_result, FALSE)) { + /* Previous wait completed, start a new one. */ if (WaitCommEvent(port->hdl, &port->events, &port->wait_ovl) == 0) { if (GetLastError() != ERROR_IO_PENDING) RETURN_FAIL("WaitCommEvent() failed"); } + } else if (GetLastError() != ERROR_IO_INCOMPLETE) { + RETURN_FAIL("GetOverlappedResult() failed"); } RETURN_INT(bytes_read);