]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Fix ERROR_SEM_TIMEOUT issue on Windows.
[libserialport.git] / serialport.c
index 520d27e8531a4014e6a9746f9bd25b5d2b91103b..d271478250f8588e0bfe03651b25f492a3d8523e 100644 (file)
@@ -389,12 +389,12 @@ SP_API void sp_free_port_list(struct sp_port **list)
 #ifdef _WIN32
 #define CHECK_PORT_HANDLE() do { \
        if (port->hdl == INVALID_HANDLE_VALUE) \
-               RETURN_ERROR(SP_ERR_ARG, "Invalid port handle"); \
+               RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
 } while (0)
 #else
 #define CHECK_PORT_HANDLE() do { \
        if (port->fd < 0) \
-               RETURN_ERROR(SP_ERR_ARG, "Invalid port fd"); \
+               RETURN_ERROR(SP_ERR_ARG, "Port not open"); \
 } while (0)
 #endif
 #define CHECK_OPEN_PORT() do { \
@@ -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)