kopia lustrzana https://github.com/mik3y/usb-serial-for-android
improved exception type also for read with timeout
rodzic
399d3c9c2f
commit
35fdeb1e13
|
@ -13,7 +13,7 @@ android {
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
|
testInstrumentationRunnerArguments = [ // Raspi Windows LinuxVM ...
|
||||||
'rfc2217_server_host': '192.168.0.145',
|
'rfc2217_server_host': '192.168.0.143',
|
||||||
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
|
'rfc2217_server_nonstandard_baudrates': 'true', // true false false
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -253,13 +253,15 @@ public class DeviceTest {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
usb.write(new byte[]{0x00});
|
usb.write(new byte[]{0x00});
|
||||||
fail("write error expected");
|
fail("write closed expected");
|
||||||
} catch (IOException ignored) {
|
} catch(IOException ex) {
|
||||||
|
assertEquals("Connection closed", ex.getMessage());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
usb.read(1);
|
usb.read(1);
|
||||||
fail("read error expected");
|
fail("read closed expected");
|
||||||
} catch (IOException ignored) {
|
} catch(IOException ex) {
|
||||||
|
assertEquals("Connection closed", ex.getMessage());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
|
usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||||
|
@ -281,10 +283,56 @@ public class DeviceTest {
|
||||||
break;
|
break;
|
||||||
Thread.sleep(1);
|
Thread.sleep(1);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
usb.read();
|
||||||
|
fail("closed expected");
|
||||||
|
} catch (IOException ex) {
|
||||||
|
assertEquals("java.io.IOException: Connection closed", ex.getMessage());
|
||||||
|
}
|
||||||
// assertEquals(SerialInputOutputManager.State.STOPPED, usb.usbIoManager.getState());
|
// assertEquals(SerialInputOutputManager.State.STOPPED, usb.usbIoManager.getState());
|
||||||
// unstable. null'ify not-stopped ioManager, else usbClose would try again
|
// unstable. null'ify not-stopped ioManager, else usbClose would try again
|
||||||
if(SerialInputOutputManager.State.STOPPED != usb.ioManager.getState())
|
if(SerialInputOutputManager.State.STOPPED != usb.ioManager.getState())
|
||||||
usb.ioManager = null;
|
usb.ioManager = null;
|
||||||
|
usb.close();
|
||||||
|
|
||||||
|
// close while waiting in read
|
||||||
|
class CloseRunnable implements Runnable {
|
||||||
|
boolean wait;
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while(wait)
|
||||||
|
Thread.sleep(1);
|
||||||
|
Thread.sleep(5);
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
Log.d(TAG, "close");
|
||||||
|
usb.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usb.open(EnumSet.of(UsbWrapper.OpenCloseFlags.NO_IOMANAGER_THREAD));
|
||||||
|
CloseRunnable closer = new CloseRunnable();
|
||||||
|
closer.wait = true;
|
||||||
|
Thread th = new Thread(closer);
|
||||||
|
th.start();
|
||||||
|
try {
|
||||||
|
closer.wait = false;
|
||||||
|
usb.serialPort.read(new byte[256], 2000);
|
||||||
|
fail("closed expected");
|
||||||
|
} catch(IOException ex) {
|
||||||
|
assertEquals("Connection closed", ex.getMessage());
|
||||||
|
}
|
||||||
|
th.join();
|
||||||
|
closer.wait = true;
|
||||||
|
th = new Thread(closer);
|
||||||
|
th.start();
|
||||||
|
try {
|
||||||
|
closer.wait = false;
|
||||||
|
usb.serialPort.read(new byte[256], 0);
|
||||||
|
fail("closed expected");
|
||||||
|
} catch(IOException ex) {
|
||||||
|
assertEquals("Connection closed", ex.getMessage());
|
||||||
|
}
|
||||||
|
th.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -158,6 +158,9 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||||
* use simple USB request supported by all devices to test if connection is still valid
|
* use simple USB request supported by all devices to test if connection is still valid
|
||||||
*/
|
*/
|
||||||
protected void testConnection() throws IOException {
|
protected void testConnection() throws IOException {
|
||||||
|
if(mConnection == null || mUsbRequest == null) {
|
||||||
|
throw new IOException("Connection closed");
|
||||||
|
}
|
||||||
byte[] buf = new byte[2];
|
byte[] buf = new byte[2];
|
||||||
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
int len = mConnection.controlTransfer(0x80 /*DEVICE*/, 0 /*GET_STATUS*/, 0, 0, buf, buf.length, 200);
|
||||||
if(len < 0)
|
if(len < 0)
|
||||||
|
|
|
@ -156,7 +156,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||||
testConnection();
|
testConnection();
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
nread = super.read(dest, timeout, false);
|
nread = super.read(dest, timeout);
|
||||||
} while (nread == READ_HEADER_LENGTH);
|
} while (nread == READ_HEADER_LENGTH);
|
||||||
}
|
}
|
||||||
return readFilter(dest, nread);
|
return readFilter(dest, nread);
|
||||||
|
|
Ładowanie…
Reference in New Issue