}
/* Start write. */
- if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl) == 0) {
- if (GetLastError() == ERROR_IO_PENDING) {
- DEBUG("Waiting for write to complete");
- GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
- DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
- RETURN_INT(bytes_written);
- } else {
- RETURN_FAIL("WriteFile() failed");
- }
- } else {
+ if (WriteFile(port->hdl, buf, count, NULL, &port->write_ovl)) {
DEBUG("Write completed immediately");
RETURN_INT(count);
+ } else if (GetLastError() == ERROR_IO_PENDING) {
+ DEBUG("Waiting for write to complete");
+ GetOverlappedResult(port->hdl, &port->write_ovl, &bytes_written, TRUE);
+ DEBUG_FMT("Write completed, %d/%d bytes written", bytes_written, count);
+ RETURN_INT(bytes_written);
+ } else {
+ RETURN_FAIL("WriteFile() failed");
}
#else
size_t bytes_written = 0;
/* Restart wait operation if buffer was emptied. */
static enum sp_return restart_wait_if_needed(struct sp_port *port, unsigned int bytes_read)
{
- int ret, bytes_remaining;
-
- ret = sp_input_waiting(port);
+ DWORD errors;
+ COMSTAT comstat;
- if (ret < 0)
- RETURN_CODEVAL(ret);
+ if (bytes_read == 0)
+ RETURN_OK();
- bytes_remaining = ret;
+ if (ClearCommError(port->hdl, &errors, &comstat) == 0)
+ RETURN_FAIL("ClearCommError() failed");
- if (bytes_read > 0 && bytes_remaining == 0)
+ if (comstat.cbInQue == 0)
TRY(restart_wait(port));
RETURN_OK();
}
/* Start read. */
- if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl) == 0) {
- if (GetLastError() == ERROR_IO_PENDING) {
- DEBUG("Waiting for read to complete");
- GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
- DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
- } else {
- RETURN_FAIL("ReadFile() failed");
- }
- } else {
+ if (ReadFile(port->hdl, buf, count, NULL, &port->read_ovl)) {
DEBUG("Read completed immediately");
bytes_read = count;
+ } else if (GetLastError() == ERROR_IO_PENDING) {
+ DEBUG("Waiting for read to complete");
+ GetOverlappedResult(port->hdl, &port->read_ovl, &bytes_read, TRUE);
+ DEBUG_FMT("Read completed, %d/%d bytes read", bytes_read, count);
+ } else {
+ RETURN_FAIL("ReadFile() failed");
}
TRY(restart_wait_if_needed(port, bytes_read));