]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Fix ERROR_SEM_TIMEOUT issue on Windows.
[libserialport.git] / serialport.c
index c4a0e1b96e5dbc50e6754fcc55c48fa8c26822e7..d271478250f8588e0bfe03651b25f492a3d8523e 100644 (file)
@@ -764,8 +764,14 @@ SP_API enum sp_return sp_blocking_write(struct sp_port *port, const void *buf,
                RETURN_INT(count);
        } else if (GetLastError() == ERROR_IO_PENDING) {
                DEBUG("Waiting for write to complete");
-               if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0)
-                       RETURN_FAIL("GetOverlappedResult() failed");
+               if (GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE) == 0) {
+                       if (GetLastError() == ERROR_SEM_TIMEOUT) {
+                               DEBUG("Write timed out");
+                               RETURN_INT(0);
+                       } else {
+                               RETURN_FAIL("GetOverlappedResult() failed");
+                       }
+               }
                DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
                RETURN_INT(bytes_written);
        } else {
@@ -1112,9 +1118,8 @@ SP_API enum sp_return sp_blocking_read_next(struct sp_port *port, void *buf,
        /* Loop until we have at least one byte, or timeout is reached. */
        while (bytes_read == 0) {
                /* Start read. */
-               if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) {
+               if (ReadFile(port->hdl, buf, count, &bytes_read, &port->read_ovl)) {
                        DEBUG("Read completed immediately");
-                       bytes_read = count;
                } else if (GetLastError() == ERROR_IO_PENDING) {
                        DEBUG("Waiting for read to complete");
                        if (GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE) == 0)