1
0
mirror of https://github.com/mik3y/usb-serial-for-android.git synced 2026-08-15 18:33:02 +00:00

FTDI read() now waits until timeout

previously returned after periodic FTDI status response (default 16 msec)
This commit is contained in:
kai-morich
2020-09-05 12:00:37 +02:00
parent 80e8eb8a60
commit 6f4cd0313c
3 changed files with 38 additions and 26 deletions
@@ -158,15 +158,12 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
}
nread = buf.position();
}
if (nread > 0) {
return readFilter(dest, nread);
} else {
if (nread > 0)
return nread;
else
return 0;
}
}
protected int readFilter(final byte[] buffer, int len) throws IOException { return len; }
@Override
public int write(final byte[] src, final int timeout) throws IOException {
int offset = 0;
@@ -136,7 +136,22 @@ public class FtdiSerialDriver implements UsbSerialDriver {
}
@Override
protected int readFilter(byte[] buffer, int totalBytesRead) throws IOException {
public int read(final byte[] dest, final int timeout) throws IOException {
int nread;
if (timeout != 0) {
long endTime = System.currentTimeMillis() + timeout;
do {
nread = super.read(dest, Math.max(1, (int)(endTime - System.currentTimeMillis())));
} while (nread == READ_HEADER_LENGTH && System.currentTimeMillis() < endTime);
} else {
do {
nread = super.read(dest, timeout);
} while (nread == READ_HEADER_LENGTH);
}
return readFilter(dest, nread);
}
private int readFilter(byte[] buffer, int totalBytesRead) throws IOException {
final int maxPacketSize = mReadEndpoint.getMaxPacketSize();
int destPos = 0;
for(int srcPos = 0; srcPos < totalBytesRead; srcPos += maxPacketSize) {
@@ -146,6 +161,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
System.arraycopy(buffer, srcPos + READ_HEADER_LENGTH, buffer, destPos, length);
destPos += length;
}
//Log.d(TAG, "read filter " + totalBytesRead + " -> " + destPos);
return destPos;
}