From a98176f513941b77bb652002e4865ff65909cfe2 Mon Sep 17 00:00:00 2001 From: peri4 Date: Sun, 18 Dec 2022 14:23:19 +0300 Subject: [PATCH] PISerial Windows read fix --- libs/main/io_devices/piserial.cpp | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/libs/main/io_devices/piserial.cpp b/libs/main/io_devices/piserial.cpp index 415b4dea..13d66f64 100644 --- a/libs/main/io_devices/piserial.cpp +++ b/libs/main/io_devices/piserial.cpp @@ -830,9 +830,15 @@ void PISerial::applySettings() { void PISerial::setTimeouts() { #ifdef WINDOWS COMMTIMEOUTS times; - times.ReadIntervalTimeout = isOptionSet(BlockingRead) ? vtime : MAXDWORD; - times.ReadTotalTimeoutConstant = isOptionSet(BlockingRead) ? 0 : 1; - times.ReadTotalTimeoutMultiplier = isOptionSet(BlockingRead) ? 0 : MAXDWORD; + if (isOptionSet(BlockingRead)) { + times.ReadIntervalTimeout = MAXDWORD; + times.ReadTotalTimeoutConstant = vtime; + times.ReadTotalTimeoutMultiplier = MAXDWORD; + } else { + times.ReadIntervalTimeout = MAXDWORD; + times.ReadTotalTimeoutConstant = 0; + times.ReadTotalTimeoutMultiplier = 0; + } times.WriteTotalTimeoutConstant = isOptionSet(BlockingWrite) ? 0 : 1; times.WriteTotalTimeoutMultiplier = 0; if (SetCommTimeouts(PRIVATE->hCom, ×) == -1) piCoutObj << "Unable to set timeouts for \"" << path() << "\""; @@ -860,25 +866,26 @@ ssize_t PISerial::readDevice(void * read_to, ssize_t max_size) { #ifdef WINDOWS if (!canRead()) return -1; if (sending) return -1; - // piCoutObj << "read ..." << PRIVATE->hCom; + // piCoutObj << "read ..." << PRIVATE->hCom << max_size; memset(&(PRIVATE->overlap), 0, sizeof(PRIVATE->overlap)); PRIVATE->overlap.hEvent = PRIVATE->event.getEvent(); + PRIVATE->readed = 0; ReadFile(PRIVATE->hCom, read_to, max_size, NULL, &(PRIVATE->overlap)); - PRIVATE->readed = 0; + DWORD err = GetLastError(); + // piCoutObj << "read" << err; + if (err == ERROR_BAD_COMMAND || err == ERROR_ACCESS_DENIED) { + piCoutObj << "read error" << errorString(); + stop(); + close(); + return 0; + } + // piCout << "wait ..."; if (PRIVATE->event.wait()) { GetOverlappedResult(PRIVATE->hCom, &(PRIVATE->overlap), &(PRIVATE->readed), FALSE); } else return -1; // piCoutObj << "read done" << PRIVATE->readed; - DWORD err = GetLastError(); - if (err == ERROR_TIMEOUT && PRIVATE->readed == 0) return 0; - if (err == ERROR_BAD_COMMAND || err == ERROR_ACCESS_DENIED) { - piCoutObj << "read error" << (PRIVATE->readed) << errorString(); - stop(); - close(); - return 0; - } - // piCoutObj << "read" << (PRIVATE->readed) << errorString(); + // piCoutObj << "read" << (PRIVATE->readed) << errorString(); return PRIVATE->readed; #else if (!canRead()) return -1;