From 4939a18b2d0feaabb5a4ae5d8dac74c708299b96 Mon Sep 17 00:00:00 2001 From: hedgecrw85 Date: Thu, 19 Nov 2015 13:32:16 -0600 Subject: [PATCH] Allow Windows serial port to return after several failed attempts at closing. --- src/main/c/Windows/SerialPort_Windows.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/c/Windows/SerialPort_Windows.c b/src/main/c/Windows/SerialPort_Windows.c index 5e851ea..13c16f7 100644 --- a/src/main/c/Windows/SerialPort_Windows.c +++ b/src/main/c/Windows/SerialPort_Windows.c @@ -500,7 +500,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); // Close port - while (!CloseHandle(serialPortHandle)); + int numRetries = 10; + while (!CloseHandle(serialPortHandle) && (numRetries-- > 0)); serialPortHandle = INVALID_HANDLE_VALUE; env->SetLongField(obj, serialPortHandleField, -1l); env->SetBooleanField(obj, isOpenedField, JNI_FALSE); @@ -518,7 +519,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(J if (!ClearCommError(serialPortHandle, NULL, &commInfo)) return -1; DWORD numBytesAvailable = commInfo.cbInQue; - + return (jint)numBytesAvailable; }