From 3ce16566c92bdd3a2177a671face50aa17c29d9a Mon Sep 17 00:00:00 2001 From: vertigo Date: Mon, 30 Jun 2014 17:39:17 +0200 Subject: [PATCH] Unix: add SerialPort.read(byte[] buffer) --- src/main/cpp/_nix_based/jssc.cpp | 30 +++++++++++++++++++ src/main/cpp/jssc_SerialNativeInterface.h | 8 +++++ .../java/org/scream3r/jssc/SerialPort.java | 16 +++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/main/cpp/_nix_based/jssc.cpp b/src/main/cpp/_nix_based/jssc.cpp index 634b620187..0d91c2d715 100644 --- a/src/main/cpp/_nix_based/jssc.cpp +++ b/src/main/cpp/_nix_based/jssc.cpp @@ -549,6 +549,36 @@ JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes return returnArray; } +/* + * Read bytes + */ +JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_read +(JNIEnv *env, jobject object, jlong portHandle, jbyteArray buffer) { + + jbyte* jBuffer = env->GetByteArrayElements(buffer, JNI_FALSE); + jint bufferSize = env->GetArrayLength(buffer); + int bytesRead, end; + fd_set read_fd_set; + + do { + end = 1; + FD_ZERO(&read_fd_set); + FD_SET(portHandle, &read_fd_set); + select(portHandle + 1, &read_fd_set, NULL, NULL, NULL); + if ((bytesRead = read(portHandle, jBuffer, bufferSize)) <= 0) { + if ((bytesRead < 0) && (errno == EINTR)) + end = 0; + } + + } while (!end); + + env->ReleaseByteArrayElements(buffer, jBuffer, 0); + return bytesRead; + + +} + + /* OK */ /* * Get bytes count in serial port buffers (Input and Output) diff --git a/src/main/cpp/jssc_SerialNativeInterface.h b/src/main/cpp/jssc_SerialNativeInterface.h index 7029b1bbee..f4eec2b87e 100644 --- a/src/main/cpp/jssc_SerialNativeInterface.h +++ b/src/main/cpp/jssc_SerialNativeInterface.h @@ -139,6 +139,14 @@ JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes (JNIEnv *, jobject, jlong, jint); +/* + * Class: jssc_SerialNativeInterface + * Method: read + * Signature: (J[B)I + */ +JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_read + (JNIEnv *, jobject, jlong, jbyteArray); + /* * Class: jssc_SerialNativeInterface * Method: writeBytes diff --git a/src/main/java/org/scream3r/jssc/SerialPort.java b/src/main/java/org/scream3r/jssc/SerialPort.java index 613dce389c..bff1126279 100644 --- a/src/main/java/org/scream3r/jssc/SerialPort.java +++ b/src/main/java/org/scream3r/jssc/SerialPort.java @@ -428,7 +428,7 @@ public boolean writeIntArray(int[] buffer) throws SerialPortException { * Read byte array from port * * @param byteCount count of bytes for reading - * + * * @return byte array with "byteCount" length * * @throws SerialPortException @@ -438,6 +438,20 @@ public byte[] readBytes(int byteCount) throws SerialPortException { return serialInterface.readBytes(portHandle, byteCount); } + /** + * Read byte array from port + * + * @param buffer, read all bytes available into it + * + * @return bytes read + * + * @throws SerialPortException + */ + public int read(byte[] buffer) throws SerialPortException { + checkPortOpened("read()"); + return serialInterface.read(portHandle, buffer); + } + /** * Read string from port *