Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/cpp/_nix_based/jssc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <time.h>
Expand Down Expand Up @@ -641,12 +642,22 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes
*/
JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getBuffersBytesCount
(JNIEnv *env, jobject, jlong portHandle){
int err;
jint returnValues[2];
returnValues[0] = -1; //Input buffer
returnValues[1] = -1; //Output buffer

err = ioctl(portHandle, FIONREAD, &returnValues[0]) == -1
|| ioctl(portHandle, TIOCOUTQ, &returnValues[1]) == -1;
if( err ){
err = errno;
jclass exClz = env->FindClass("java/io/IOException");
if( exClz != NULL ) env->ThrowNew(exClz, strerror(err));
return NULL;
}

jintArray returnArray = env->NewIntArray(2);
ioctl(portHandle, FIONREAD, &returnValues[0]);
ioctl(portHandle, TIOCOUTQ, &returnValues[1]);
if( returnArray == NULL ) return NULL;
env->SetIntArrayRegion(returnArray, 0, 2, returnValues);
return returnArray;
}
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 @@ -290,7 +290,7 @@ public static String getLibraryVersion() {
*
* @since 0.8
*/
public native int[] getBuffersBytesCount(long handle);
public native int[] getBuffersBytesCount(long handle) throws IOException;

/**
* Set flow control mode
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/jssc/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,11 @@ public int[] readIntArray() throws SerialPortException {
*/
public int getInputBufferBytesCount() throws SerialPortException {
checkPortOpened("getInputBufferBytesCount()");
return serialInterface.getBuffersBytesCount(portHandle)[0];
try{
return serialInterface.getBuffersBytesCount(portHandle)[0];
}catch( IOException ex ){
throw SerialPortException.wrapNativeException(ex, this, "getInputBufferBytesCount");
}
}

/**
Expand All @@ -890,7 +894,11 @@ public int getInputBufferBytesCount() throws SerialPortException {
*/
public int getOutputBufferBytesCount() throws SerialPortException {
checkPortOpened("getOutputBufferBytesCount()");
return serialInterface.getBuffersBytesCount(portHandle)[1];
try{
return serialInterface.getBuffersBytesCount(portHandle)[1];
}catch( IOException ex ){
throw SerialPortException.wrapNativeException(ex, this, "getOutputBufferBytesCount");
}
}

/**
Expand Down