diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index ad018a7..8a7275d 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -502,7 +502,6 @@ public final class SerialPort private static native void initializeLibrary(); // Initializes the JNI code private static native void uninitializeLibrary(); // Un-initializes the JNI code private final native long openPortNative(); // Opens serial port - //private final native boolean associateNativeHandle(long portHandle);// Associates an already opened file descriptor with this class private final native boolean closePortNative(long portHandle); // Closes serial port private final native boolean configPort(long portHandle); // Changes/sets serial port parameters as defined by this class private final native boolean configTimeouts(long portHandle); // Changes/sets serial port timeouts as defined by this class @@ -510,8 +509,8 @@ public final class SerialPort private final native int waitForEvent(long portHandle); // Waits for serial event to occur as specified in eventFlags private final native int bytesAvailable(long portHandle); // Returns number of bytes available for reading private final native int bytesAwaitingWrite(long portHandle); // Returns number of bytes still waiting to be written - private final native int readBytes(long portHandle, byte[] buffer, long bytesToRead); // Reads bytes from serial port - private final native int writeBytes(long portHandle, byte[] buffer, long bytesToWrite); // Write bytes to serial port + private final native int readBytes(long portHandle, byte[] buffer, long bytesToRead, long offset); // Reads bytes from serial port + private final native int writeBytes(long portHandle, byte[] buffer, long bytesToWrite, long offset); // Write bytes to serial port private final native boolean setBreak(long portHandle); // Set BREAK status on serial line private final native boolean clearBreak(long portHandle); // Clear BREAK status on serial line private final native boolean setRTS(long portHandle); // Set RTS line to 1 @@ -551,7 +550,23 @@ public final class SerialPort * @param bytesToRead The number of bytes to read from the serial port. * @return The number of bytes successfully read, or -1 if there was an error reading from the port. */ - public final int readBytes(byte[] buffer, long bytesToRead) { return readBytes(portHandle, buffer, bytesToRead); } + public final int readBytes(byte[] buffer, long bytesToRead) { return readBytes(portHandle, buffer, bytesToRead, 0); } + + /** + * Reads up to bytesToRead raw data bytes from the serial port and stores them in the buffer starting at the indicated offset. + *
+ * The length of the byte buffer minus the offset must be greater than or equal to the value passed in for bytesToRead + *
+ * If no timeouts were specified or the read timeout was set to 0, this call will block until bytesToRead bytes of data have been successfully read from the serial port. + * Otherwise, this method will return after bytesToRead bytes of data have been read or the number of milliseconds specified by the read timeout have elapsed, + * whichever comes first, regardless of the availability of more data. + * + * @param buffer The buffer into which the raw data is read. + * @param bytesToRead The number of bytes to read from the serial port. + * @param offset The read buffer index into which to begin storing data. + * @return The number of bytes successfully read, or -1 if there was an error reading from the port. + */ + public final int readBytes(byte[] buffer, long bytesToRead, long offset) { return readBytes(portHandle, buffer, bytesToRead, offset); } /** * Writes up to bytesToWrite raw data bytes from the buffer parameter to the serial port. @@ -566,7 +581,23 @@ public final class SerialPort * @param bytesToWrite The number of bytes to write to the serial port. * @return The number of bytes successfully written, or -1 if there was an error writing to the port. */ - public final int writeBytes(byte[] buffer, long bytesToWrite) { return writeBytes(portHandle, buffer, bytesToWrite); } + public final int writeBytes(byte[] buffer, long bytesToWrite) { return writeBytes(portHandle, buffer, bytesToWrite, 0); } + + /** + * Writes up to bytesToWrite raw data bytes from the buffer parameter to the serial port starting at the indicated offset. + *
+ * The length of the byte buffer minus the offset must be greater than or equal to the value passed in for bytesToWrite + *
+ * If no timeouts were specified or the write timeout was set to 0, this call will block until bytesToWrite bytes of data have been successfully written the serial port. + * Otherwise, this method will return after bytesToWrite bytes of data have been written or the number of milliseconds specified by the write timeout have elapsed, + * whichever comes first, regardless of the availability of more data. + * + * @param buffer The buffer containing the raw data to write to the serial port. + * @param bytesToWrite The number of bytes to write to the serial port. + * @param offset The buffer index from which to begin writing to the serial port. + * @return The number of bytes successfully written, or -1 if there was an error writing to the port. + */ + public final int writeBytes(byte[] buffer, long bytesToWrite, long offset) { return writeBytes(portHandle, buffer, bytesToWrite, offset); } /** * Sets the BREAK signal on the serial control line. @@ -1112,7 +1143,7 @@ public final class SerialPort { byte[] newBytes = new byte[numBytesAvailable]; newBytesIndex = 0; - bytesRemaining = readBytes(portHandle, newBytes, newBytes.length); + bytesRemaining = readBytes(portHandle, newBytes, newBytes.length, 0); if(dataPacket.length == 0) userDataListener.serialEvent(new SerialPortEvent(SerialPort.this, LISTENING_EVENT_DATA_RECEIVED, newBytes.clone())); else @@ -1166,13 +1197,13 @@ public final class SerialPort @Override public final int read() throws IOException { - return (readBytes(portHandle, byteBuffer, 1L) < 0) ? -1 : ((int)byteBuffer[0] & 0xFF); + return (readBytes(portHandle, byteBuffer, 1L, 0) < 0) ? -1 : ((int)byteBuffer[0] & 0xFF); } @Override public final int read(byte[] b) throws NullPointerException, IOException { - return readBytes(portHandle, b, b.length); + return readBytes(portHandle, b, b.length, 0); } @Override @@ -1183,20 +1214,14 @@ public final class SerialPort throw new NullPointerException("A null pointer was passed in for the read buffer."); if ((len < 0) || (off < 0) || (len > (b.length - off))) throw new IndexOutOfBoundsException("The specified read offset plus length extends past the end of the specified buffer."); + if (!isOpened) + throw new IOException("This port appears to have been shutdown or disconnected."); if (len == 0) return 0; - // Ensure that read buffer is large enough and the port is open - if (len > byteBuffer.length) - byteBuffer = new byte[len]; - if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); - // Read from the serial port - int numRead = readBytes(portHandle, byteBuffer, len); - if (numRead > 0) - System.arraycopy(byteBuffer, 0, b, off, numRead); - else if (numRead == 0) + int numRead = readBytes(portHandle, b, len, off); + if (numRead == 0) throw new IOException("The read operation timed out before any data was returned."); return numRead; } @@ -1207,7 +1232,7 @@ public final class SerialPort if (!isOpened) throw new IOException("This port appears to have been shutdown or disconnected."); byte[] buffer = new byte[(int)n]; - return readBytes(portHandle, buffer, n); + return readBytes(portHandle, buffer, n, 0); } } @@ -1224,7 +1249,7 @@ public final class SerialPort if (!isOpened) throw new IOException("This port appears to have been shutdown or disconnected."); byteBuffer[0] = (byte)(b & 0xFF); - int bytesWritten = writeBytes(portHandle, byteBuffer, 1L); + int bytesWritten = writeBytes(portHandle, byteBuffer, 1L, 0); if (bytesWritten < 0) throw new IOException("This port appears to have been shutdown or disconnected."); else if (bytesWritten == 0) @@ -1245,27 +1270,17 @@ public final class SerialPort throw new NullPointerException("A null pointer was passed in for the write buffer."); if ((len < 0) || (off < 0) || ((off + len) > b.length)) throw new IndexOutOfBoundsException("The specified write offset plus length extends past the end of the specified buffer."); + if (!isOpened) + throw new IOException("This port appears to have been shutdown or disconnected."); if (len == 0) return; - // Ensure that write buffer is large enough and the port is open - if (len > byteBuffer.length) - byteBuffer = new byte[len]; - if (!isOpened) - throw new IOException("This port appears to have been shutdown or disconnected."); - // Write to the serial port - int numWritten, totalNumWritten = 0, bytesRemaining = len; - do { - System.arraycopy(b, off + totalNumWritten, byteBuffer, 0, bytesRemaining); - numWritten = writeBytes(portHandle, byteBuffer, bytesRemaining); - totalNumWritten += numWritten; - bytesRemaining -= numWritten; - if (numWritten < 0) - throw new IOException("This port appears to have been shutdown or disconnected."); - else if (numWritten == 0) - throw new IOException("The write operation timed out before all data was written."); - } while (bytesRemaining > 0); + int numWritten = writeBytes(portHandle, b, len, off); + if (numWritten < 0) + throw new IOException("This port appears to have been shutdown or disconnected."); + else if (numWritten == 0) + throw new IOException("The write operation timed out before all data was written."); } } } diff --git a/src/main/resources/Android/arm64-v8a/libjSerialComm.so b/src/main/resources/Android/arm64-v8a/libjSerialComm.so index 902a3fe..33f9a9d 100644 Binary files a/src/main/resources/Android/arm64-v8a/libjSerialComm.so and b/src/main/resources/Android/arm64-v8a/libjSerialComm.so differ diff --git a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so index 23ba177..f0d4a08 100644 Binary files a/src/main/resources/Android/armeabi-v7a/libjSerialComm.so and b/src/main/resources/Android/armeabi-v7a/libjSerialComm.so differ diff --git a/src/main/resources/Android/armeabi/libjSerialComm.so b/src/main/resources/Android/armeabi/libjSerialComm.so index 00293d0..cb03717 100644 Binary files a/src/main/resources/Android/armeabi/libjSerialComm.so and b/src/main/resources/Android/armeabi/libjSerialComm.so differ diff --git a/src/main/resources/Android/mips/libjSerialComm.so b/src/main/resources/Android/mips/libjSerialComm.so index 782a744..13e75c0 100644 Binary files a/src/main/resources/Android/mips/libjSerialComm.so and b/src/main/resources/Android/mips/libjSerialComm.so differ diff --git a/src/main/resources/Android/mips64/libjSerialComm.so b/src/main/resources/Android/mips64/libjSerialComm.so index 533581e..f6fb5ca 100644 Binary files a/src/main/resources/Android/mips64/libjSerialComm.so and b/src/main/resources/Android/mips64/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86/libjSerialComm.so b/src/main/resources/Android/x86/libjSerialComm.so index 6fa1740..3347c58 100644 Binary files a/src/main/resources/Android/x86/libjSerialComm.so and b/src/main/resources/Android/x86/libjSerialComm.so differ diff --git a/src/main/resources/Android/x86_64/libjSerialComm.so b/src/main/resources/Android/x86_64/libjSerialComm.so index c852ae4..d20709f 100644 Binary files a/src/main/resources/Android/x86_64/libjSerialComm.so and b/src/main/resources/Android/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv5/libjSerialComm.so b/src/main/resources/Linux/armv5/libjSerialComm.so index b4bb952..9088084 100644 Binary files a/src/main/resources/Linux/armv5/libjSerialComm.so and b/src/main/resources/Linux/armv5/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv6-hf/libjSerialComm.so b/src/main/resources/Linux/armv6-hf/libjSerialComm.so index dc0b685..2cbdda9 100644 Binary files a/src/main/resources/Linux/armv6-hf/libjSerialComm.so and b/src/main/resources/Linux/armv6-hf/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv6/libjSerialComm.so b/src/main/resources/Linux/armv6/libjSerialComm.so index 18d105b..4a20e05 100644 Binary files a/src/main/resources/Linux/armv6/libjSerialComm.so and b/src/main/resources/Linux/armv6/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv7-hf/libjSerialComm.so b/src/main/resources/Linux/armv7-hf/libjSerialComm.so index dc0b685..2cbdda9 100644 Binary files a/src/main/resources/Linux/armv7-hf/libjSerialComm.so and b/src/main/resources/Linux/armv7-hf/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv7/libjSerialComm.so b/src/main/resources/Linux/armv7/libjSerialComm.so index c8389f0..1ad8a0a 100644 Binary files a/src/main/resources/Linux/armv7/libjSerialComm.so and b/src/main/resources/Linux/armv7/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv8_32/libjSerialComm.so b/src/main/resources/Linux/armv8_32/libjSerialComm.so index b6f2980..62ca2ae 100644 Binary files a/src/main/resources/Linux/armv8_32/libjSerialComm.so and b/src/main/resources/Linux/armv8_32/libjSerialComm.so differ diff --git a/src/main/resources/Linux/armv8_64/libjSerialComm.so b/src/main/resources/Linux/armv8_64/libjSerialComm.so index 644c3b5..7c65a28 100644 Binary files a/src/main/resources/Linux/armv8_64/libjSerialComm.so and b/src/main/resources/Linux/armv8_64/libjSerialComm.so differ diff --git a/src/main/resources/Linux/x86/libjSerialComm.so b/src/main/resources/Linux/x86/libjSerialComm.so index ddbbb5c..8f9aa8a 100644 Binary files a/src/main/resources/Linux/x86/libjSerialComm.so and b/src/main/resources/Linux/x86/libjSerialComm.so differ diff --git a/src/main/resources/Linux/x86_64/libjSerialComm.so b/src/main/resources/Linux/x86_64/libjSerialComm.so index 2f47b26..fb4c5a4 100644 Binary files a/src/main/resources/Linux/x86_64/libjSerialComm.so and b/src/main/resources/Linux/x86_64/libjSerialComm.so differ diff --git a/src/main/resources/OSX/x86/libjSerialComm.jnilib b/src/main/resources/OSX/x86/libjSerialComm.jnilib index 709698a..546c1a5 100755 Binary files a/src/main/resources/OSX/x86/libjSerialComm.jnilib and b/src/main/resources/OSX/x86/libjSerialComm.jnilib differ diff --git a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib index 28a6eee..d75b40e 100755 Binary files a/src/main/resources/OSX/x86_64/libjSerialComm.jnilib and b/src/main/resources/OSX/x86_64/libjSerialComm.jnilib differ diff --git a/src/main/resources/Windows/x86/jSerialComm.dll b/src/main/resources/Windows/x86/jSerialComm.dll index 3684a9d..6b25ac3 100644 Binary files a/src/main/resources/Windows/x86/jSerialComm.dll and b/src/main/resources/Windows/x86/jSerialComm.dll differ diff --git a/src/main/resources/Windows/x86/jSerialComm.exp b/src/main/resources/Windows/x86/jSerialComm.exp index 8f27f4f..3ca8d9e 100644 Binary files a/src/main/resources/Windows/x86/jSerialComm.exp and b/src/main/resources/Windows/x86/jSerialComm.exp differ diff --git a/src/main/resources/Windows/x86/jSerialComm.lib b/src/main/resources/Windows/x86/jSerialComm.lib index dccdc44..46cf6ba 100644 Binary files a/src/main/resources/Windows/x86/jSerialComm.lib and b/src/main/resources/Windows/x86/jSerialComm.lib differ diff --git a/src/main/resources/Windows/x86_64/jSerialComm.dll b/src/main/resources/Windows/x86_64/jSerialComm.dll index 3bf6bdb..bef73d7 100644 Binary files a/src/main/resources/Windows/x86_64/jSerialComm.dll and b/src/main/resources/Windows/x86_64/jSerialComm.dll differ diff --git a/src/main/resources/Windows/x86_64/jSerialComm.exp b/src/main/resources/Windows/x86_64/jSerialComm.exp index b7ef6a1..75d92a3 100644 Binary files a/src/main/resources/Windows/x86_64/jSerialComm.exp and b/src/main/resources/Windows/x86_64/jSerialComm.exp differ diff --git a/src/main/resources/Windows/x86_64/jSerialComm.lib b/src/main/resources/Windows/x86_64/jSerialComm.lib index 0e26996..d7e3f2c 100644 Binary files a/src/main/resources/Windows/x86_64/jSerialComm.lib and b/src/main/resources/Windows/x86_64/jSerialComm.lib differ