From 91d264501159a08766566e2b2029b2f99dabfc83 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Wed, 12 Sep 2018 07:52:13 +0100 Subject: [PATCH] Add the serial timeout to the Windows serial port handler. --- Common/SerialDataController.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Common/SerialDataController.cpp b/Common/SerialDataController.cpp index e47bbf9..ad11a4e 100644 --- a/Common/SerialDataController.cpp +++ b/Common/SerialDataController.cpp @@ -205,15 +205,21 @@ int CSerialDataController::readNonblock(unsigned char* buffer, unsigned int leng m_readPending = true; } - BOOL res = HasOverlappedIoCompleted(&m_readOverlapped); - if (!res) - return 0; - - DWORD bytes = 0UL; - res = ::GetOverlappedResult(m_handle, &m_readOverlapped, &bytes, TRUE); + DWORD bytes = 0UL; + DWORD millis = timeout; + res = ::GetOverlappedResultEx(m_handle, &m_readOverlapped, &bytes, millis, FALSE); if (!res) { - wxLogError(wxT("Error from GetOverlappedResult (ReadFile): %04lx"), ::GetLastError()); - return -1; + DWORD error = ::GetLastError(); + if (timeout == 0U && error == ERROR_IO_INCOMPLETE) { + return 0; + } else if (timeout > 0U && error == WAIT_TIMEOUT) { + return 0; + } else if (timeout > 0U && error == WAIT_IO_COMPLETION) { + return 0; + } else { + wxLogError(wxT("Error from GetOverlappedResultEx (ReadFile): %04lx"), error); + return -1; + } } ::memcpy(buffer, m_readBuffer, bytes);