From 36ee5d8d47da4bad96069f4e868b9ce392d15fc9 Mon Sep 17 00:00:00 2001 From: "Andreas Fankhauser hiddenalpha.ch" <23085769+hiddenalpha@users.noreply.github.com> Date: Mon, 3 Oct 2022 23:24:15 +0200 Subject: [PATCH] Also adapt windows impl --- src/main/cpp/windows/jssc.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/cpp/windows/jssc.cpp b/src/main/cpp/windows/jssc.cpp index bc9cfc685..db4fbab23 100644 --- a/src/main/cpp/windows/jssc.cpp +++ b/src/main/cpp/windows/jssc.cpp @@ -231,28 +231,31 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR * portHandle - port handle * buffer - byte array for sending */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes +JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_write (JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){ HANDLE hComm = (HANDLE)portHandle; DWORD lpNumberOfBytesTransferred; DWORD lpNumberOfBytesWritten; OVERLAPPED *overlapped = new OVERLAPPED(); - jboolean returnValue = JNI_FALSE; + jint returnValue = -1; jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE); overlapped->hEvent = CreateEventA(NULL, true, false, NULL); if(WriteFile(hComm, jBuffer, (DWORD)env->GetArrayLength(buffer), &lpNumberOfBytesWritten, overlapped)){ - returnValue = JNI_TRUE; + returnValue = lpNumberOfBytesWritten; } else if(GetLastError() == ERROR_IO_PENDING){ if(WaitForSingleObject(overlapped->hEvent, INFINITE) == WAIT_OBJECT_0){ if(GetOverlappedResult(hComm, overlapped, &lpNumberOfBytesTransferred, false)){ - returnValue = JNI_TRUE; + returnValue = lpNumberOfBytesTransferred; } } } env->ReleaseByteArrayElements(buffer, jBuffer, 0); CloseHandle(overlapped->hEvent); delete overlapped; + if( returnValue < 0 ){ + return env->ThrowNew(env->FindClass("jssc/SerialPortException"), "WriteFile() failed"); + } return returnValue; }