From 51d7e2fb01cf6e2ea99095776c064059b1ac76c9 Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Fri, 20 Nov 2015 10:52:50 -0600 Subject: [PATCH] Allow for closing of port during blocking call on Windows. --- src/main/c/Windows/SerialPort_Windows.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/c/Windows/SerialPort_Windows.c b/src/main/c/Windows/SerialPort_Windows.c index 13c16f7..adeac4f 100644 --- a/src/main/c/Windows/SerialPort_Windows.c +++ b/src/main/c/Windows/SerialPort_Windows.c @@ -469,7 +469,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI } // Wait for a serial port event - DWORD eventMask, numBytesRead; + DWORD eventMask, numBytesRead, readResult = WAIT_FAILED; if (WaitCommEvent(serialPortHandle, &eventMask, &overlappedStruct) == FALSE) { if (GetLastError() != ERROR_IO_PENDING) // Problem occurred @@ -481,8 +481,17 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI env->SetLongField(obj, serialPortHandleField, -1l); env->SetBooleanField(obj, isOpenedField, JNI_FALSE); } - else if (GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesRead, TRUE) == FALSE) - numBytesRead = 0; + else + { + BOOL continueWaiting = TRUE; + while (continueWaiting) + { + readResult = waitForSingleObject(overlappedStruct.hEvent, 750); + continueWaiting = ((readResult == WAIT_TIMEOUT) && (env->GetIntField(obj, eventFlagsField) != 0)); + } + if ((readResult != WAIT_OBJECT_0) || (GetOverlappedResult(serialPortHandle, &overlappedStruct, &numBytesRead, TRUE) == FALSE)) + numBytesRead = 0; + } } // Return type of event if successful