diff --git a/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java b/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java index f44c331..9aa824b 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java +++ b/usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java @@ -689,6 +689,63 @@ public class FTDISerialDevice extends UsbSerialDevice return readen; } + @Override + public int syncRead(byte[] buffer, int offset, int length, int timeout) { + + long beginTime = System.currentTimeMillis(); + long stopTime = beginTime + timeout; + + if(asyncMode) + { + return -1; + } + + if(buffer == null) + { + return 0; + } + + int n = length / 62; + if(length % 62 != 0) + { + n++; + } + + byte[] tempBuffer = new byte[length + n * 2]; + + int readen = 0; + + do + { + int timeLeft = 0; + if(timeout > 0) + { + timeLeft = (int) (stopTime - System.currentTimeMillis()); + if (timeLeft <= 0) + { + break; + } + } + + int numberBytes = connection.bulkTransfer(inEndpoint, tempBuffer, tempBuffer.length, timeLeft); + + if(numberBytes > 2) // Data received + { + byte[] newBuffer = this.ftdiUtilities.adaptArray(tempBuffer); + System.arraycopy(newBuffer, 0, buffer, offset, length); + + int p = numberBytes / 64; + if(numberBytes % 64 != 0) + { + p++; + } + readen = numberBytes - p * 2; + } + }while(readen <= 0); + + return readen; + } + private static final byte[] skip = new byte[2]; /** diff --git a/usbserial/src/main/java/com/felhr/usbserial/SerialInputStream.java b/usbserial/src/main/java/com/felhr/usbserial/SerialInputStream.java index afa8448..458e28e 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/SerialInputStream.java +++ b/usbserial/src/main/java/com/felhr/usbserial/SerialInputStream.java @@ -54,6 +54,28 @@ public class SerialInputStream extends InputStream return device.syncRead(b, timeout); } + @Override + public int read(byte b[], int off, int len) + { + if(off < 0 ){ + throw new IndexOutOfBoundsException("Offset must be >= 0"); + } + + if(len < 0){ + throw new IndexOutOfBoundsException("Length must positive"); + } + + if(len > b.length - off) { + throw new IndexOutOfBoundsException("Length greater than b.length - off"); + } + + if (off == 0 && len == b.length) { + return read(b); + } + + return device.syncRead(b, off, len, timeout); + } + @Override public int available() throws IOException { if(bufferSize > 0) diff --git a/usbserial/src/main/java/com/felhr/usbserial/SerialOutputStream.java b/usbserial/src/main/java/com/felhr/usbserial/SerialOutputStream.java index 31b13cf..b1da2fd 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/SerialOutputStream.java +++ b/usbserial/src/main/java/com/felhr/usbserial/SerialOutputStream.java @@ -25,6 +25,29 @@ public class SerialOutputStream extends OutputStream device.syncWrite(b, timeout); } + @Override + public void write(byte b[], int off, int len) + { + if(off < 0 ){ + throw new IndexOutOfBoundsException("Offset must be >= 0"); + } + + if(len < 0){ + throw new IndexOutOfBoundsException("Length must positive"); + } + + if(off + len > b.length) { + throw new IndexOutOfBoundsException("off + length greater than buffer length"); + } + + if (off == 0 && len == b.length) { + write(b); + return; + } + + device.syncWrite(b, off, len, timeout); + } + public void setTimeout(int timeout) { this.timeout = timeout; } diff --git a/usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java b/usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java index 5be2083..fe24aa4 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java +++ b/usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java @@ -5,6 +5,7 @@ import com.felhr.deviceids.CP210xIds; import com.felhr.deviceids.FTDISioIds; import com.felhr.deviceids.PL2303Ids; +import android.annotation.TargetApi; import android.hardware.usb.UsbConstants; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; @@ -216,6 +217,35 @@ public abstract class UsbSerialDevice implements UsbSerialInterface return connection.bulkTransfer(inEndpoint, buffer, buffer.length, timeout); } + @TargetApi(18) + @Override + public int syncWrite(byte[] buffer, int offset, int length, int timeout) { + if(!asyncMode) + { + if(buffer == null) + return 0; + + return connection.bulkTransfer(outEndpoint, buffer, offset, length, timeout); + }else + { + return -1; + } + } + + @TargetApi(18) + @Override + public int syncRead(byte[] buffer, int offset, int length, int timeout) { + if(asyncMode) + { + return -1; + } + + if (buffer == null) + return 0; + + return connection.bulkTransfer(inEndpoint, buffer, offset, length, timeout); + } + // Serial port configuration @Override public abstract void setBaudRate(int baudRate); diff --git a/usbserial/src/main/java/com/felhr/usbserial/UsbSerialInterface.java b/usbserial/src/main/java/com/felhr/usbserial/UsbSerialInterface.java index 952b184..ffcbb34 100644 --- a/usbserial/src/main/java/com/felhr/usbserial/UsbSerialInterface.java +++ b/usbserial/src/main/java/com/felhr/usbserial/UsbSerialInterface.java @@ -38,6 +38,8 @@ public interface UsbSerialInterface boolean syncOpen(); int syncWrite(byte[] buffer, int timeout); int syncRead(byte[] buffer, int timeout); + int syncWrite(byte[] buffer, int offset, int length, int timeout); + int syncRead(byte[] buffer, int offset, int length, int timeout); void syncClose(); // Serial port configuration