1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 07:56:20 +00:00

SerialInputOutputManager.writeAsync(): handle SerialTimeoutException

This commit is contained in:
Kai Morich 2024-05-30 18:57:39 +02:00
parent cab862599d
commit 0c0275675f
2 changed files with 27 additions and 1 deletions

View File

@ -1525,6 +1525,15 @@ public class DeviceTest {
data = telnet.read(2);
assertEquals(2, data.length);
usb.ioManager.setReadTimeout(200);
// with internal SerialTimeoutException
TestBuffer tbuf = new TestBuffer(usb.writeBufferSize + 2*usb.writePacketSize);
usb.ioManager.setWriteTimeout(20); // tbuf len >= 128, needs 133msec @ 9600 baud
usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
telnet.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
usb.ioManager.writeAsync(tbuf.buf);
while(!tbuf.testRead(telnet.read(-1)))
;
}
@Test

View File

@ -9,6 +9,7 @@ package com.hoho.android.usbserial.util;
import android.os.Process;
import android.util.Log;
import com.hoho.android.usbserial.driver.SerialTimeoutException;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import java.io.IOException;
@ -254,7 +255,23 @@ public class SerialInputOutputManager implements Runnable {
if (DEBUG) {
Log.d(TAG, "Writing data len=" + len);
}
try {
mSerialPort.write(buffer, mWriteTimeout);
} catch (SerialTimeoutException ex) {
synchronized (mWriteBufferLock) {
byte[] buffer2 = null;
int len2 = mWriteBuffer.position();
if (len2 > 0) {
buffer2 = new byte[len2];
mWriteBuffer.rewind();
mWriteBuffer.get(buffer2, 0, len2);
mWriteBuffer.clear();
}
mWriteBuffer.put(buffer, ex.bytesTransferred, buffer.length - ex.bytesTransferred);
if (buffer2 != null)
mWriteBuffer.put(buffer2);
}
}
}
}