Skip to content

Commit

Permalink
SerialInputOutputManager: use optimal read buffer size to reduce late…
Browse files Browse the repository at this point in the history
…ncy for FTDI and CH34x
  • Loading branch information
kai-morich committed Apr 4, 2021
1 parent c917ac5 commit 848d4e7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ allprojects {
Add library to dependencies
```gradle
dependencies {
implementation 'com.github.mik3y:usb-serial-for-android:3.3.0'
implementation 'com.github.mik3y:usb-serial-for-android:3.3.1'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ public void writeTimeout() throws Exception {
purgeWriteBuffer(purgeTimeout);

// determine write buffer size
int writePacketSize = ((CommonUsbSerialPort)usb.serialPort).getWriteEndpoint().getMaxPacketSize();
int writePacketSize = usb.serialPort.getWriteEndpoint().getMaxPacketSize();
byte[] pbuf = new byte[writePacketSize];
int writePackets = 0;
try {
Expand Down Expand Up @@ -1321,7 +1321,9 @@ public void purgeHwBuffers() throws Exception {
@Test
public void IoManager() throws Exception {
SerialInputOutputManager.DEBUG = true;
usb.ioManager = new SerialInputOutputManager(null);
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
assertNull(usb.ioManager);
usb.ioManager = new SerialInputOutputManager(usb.serialPort);
assertNull(usb.ioManager.getListener());
usb.ioManager.setListener(usb);
assertEquals(usb, usb.ioManager.getListener());
Expand All @@ -1335,7 +1337,7 @@ public void IoManager() throws Exception {
usb.ioManager.setWriteTimeout(11);
assertEquals(11, usb.ioManager.getWriteTimeout());

assertEquals(4096, usb.ioManager.getReadBufferSize());
assertEquals(usb.serialPort.getReadEndpoint().getMaxPacketSize(), usb.ioManager.getReadBufferSize());
usb.ioManager.setReadBufferSize(12);
assertEquals(12, usb.ioManager.getReadBufferSize());
assertEquals(4096, usb.ioManager.getWriteBufferSize());
Expand All @@ -1346,6 +1348,7 @@ public void IoManager() throws Exception {
usb.ioManager.setWriteBufferSize(usb.ioManager.getWriteBufferSize());
usb.ioManager.setReadTimeout(usb.ioManager.getReadTimeout());
usb.ioManager.setWriteTimeout(usb.ioManager.getWriteTimeout());
usb.close();

usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_START)); // creates new IoManager
usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
Expand Down Expand Up @@ -2007,8 +2010,8 @@ public void commonMethods() throws Exception {
usb.open();
assertTrue(usb.serialPort.isOpen());

assertEquals(((CommonUsbSerialPort)usb.serialPort).getWriteEndpoint().getMaxPacketSize(),
((CommonUsbSerialPort)usb.serialPort).getReadEndpoint().getMaxPacketSize());
assertEquals(usb.serialPort.getWriteEndpoint().getMaxPacketSize(),
usb.serialPort.getReadEndpoint().getMaxPacketSize());
s = usb.serialPort.getSerial();
// with target sdk 29 can throw SecurityException before USB permission dialog is confirmed
// not all devices implement serial numbers. some observed values are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,10 @@ public int getPortNumber() {
return mPortNumber;
}

/**
* Returns the write endpoint.
* @return write endpoint
*/
@Override
public UsbEndpoint getWriteEndpoint() { return mWriteEndpoint; }

/**
* Returns the read endpoint.
* @return read endpoint
*/
@Override
public UsbEndpoint getReadEndpoint() { return mReadEndpoint; }

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbManager;

import androidx.annotation.IntDef;
Expand Down Expand Up @@ -74,6 +75,18 @@ enum ControlLine { RTS, CTS, DTR, DSR, CD, RI }
*/
int getPortNumber();

/**
* Returns the write endpoint.
* @return write endpoint
*/
UsbEndpoint getWriteEndpoint();

/**
* Returns the read endpoint.
* @return read endpoint
*/
UsbEndpoint getReadEndpoint();

/**
* The serial number of the underlying UsbDeviceConnection, or {@code null}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class SerialInputOutputManager implements Runnable {
private final Object mReadBufferLock = new Object();
private final Object mWriteBufferLock = new Object();

private ByteBuffer mReadBuffer = ByteBuffer.allocate(BUFSIZ);
private ByteBuffer mReadBuffer; // default size = getReadEndpoint().getMaxPacketSize()
private ByteBuffer mWriteBuffer = ByteBuffer.allocate(BUFSIZ);

public enum State {
Expand Down Expand Up @@ -62,11 +62,13 @@ public interface Listener {

public SerialInputOutputManager(UsbSerialPort serialPort) {
mSerialPort = serialPort;
mReadBuffer = ByteBuffer.allocate(serialPort.getReadEndpoint().getMaxPacketSize());
}

public SerialInputOutputManager(UsbSerialPort serialPort, Listener listener) {
mSerialPort = serialPort;
mListener = listener;
mReadBuffer = ByteBuffer.allocate(serialPort.getReadEndpoint().getMaxPacketSize());
}

public synchronized void setListener(Listener listener) {
Expand Down

0 comments on commit 848d4e7

Please sign in to comment.