Skip to content

Commit

Permalink
Tinker how to enhance retval of writeBytes (_nix-only)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenalpha committed Oct 3, 2022
1 parent 6886b44 commit 5e94748
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -525,13 +526,16 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR
/*
* Writing data to the port
*/
JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes
JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_write
(JNIEnv *env, jobject, jlong portHandle, jbyteArray buffer){
jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE);
jint bufferSize = env->GetArrayLength(buffer);
jint result = write(portHandle, jBuffer, (size_t)bufferSize);
env->ReleaseByteArrayElements(buffer, jBuffer, 0);
return result == bufferSize ? JNI_TRUE : JNI_FALSE;
if( result < 0 ){ /* aka error */
return env->ThrowNew(env->FindClass("jssc/SerialPortException"), strerror(errno));
}
return result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jssc/SerialNativeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static String getLibraryVersion() {
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*/
public native boolean writeBytes(long handle, byte[] buffer);
public native int write(long handle, byte[] buffer) throws SerialPortException;

/**
* Get bytes count in buffers of port
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,28 @@ public boolean setDTR(boolean enabled) throws SerialPortException {
*/
public boolean writeBytes(byte[] buffer) throws SerialPortException {
checkPortOpened("writeBytes()");
return serialInterface.writeBytes(portHandle, buffer);
// Delegate to new method and translate result to what original method
// did return.
try{
int result = write(buffer);
return result == buffer.length;
}catch (SerialPortException ex) {
return false;
}
}

/**
* Write byte array to port
*
* @param buffer <code>byte[]</code> array to write
*
* @return If the operation is successfully completed, the method returns true, otherwise false
*
* @throws SerialPortException if exception occurred
*/
public int write(byte[] buffer) throws SerialPortException {
checkPortOpened("writeBytes()");
return serialInterface.write(portHandle, buffer);
}

/**
Expand Down Expand Up @@ -1392,4 +1413,5 @@ public void run() {
}
}
}

}

0 comments on commit 5e94748

Please sign in to comment.