SimpleUsbTerminal
SimpleUsbTerminal copied to clipboard
how to handle CTS detection in write method?
Hi, i did add a usual while loop to the
void write(byte[] data) throws IOException {
if(serialPort == null)
throw new IOException("not connected");
while(!serialPort.getCTS());
serialPort.write(data, WRITE_WAIT_MILLIS);
}
there is only one problem, if the device don't gets free, the app will crash. if i don't use CTS, the datas will lost, because the receiver ignores them.
is it possible on serialPort.write() to handle CTS?
What do you mean with CTS handling? Flow control as in https://github.com/mik3y/usb-serial-for-android/issues/318?
i did edit the write method like this. so it is waiting for CTS nearly 50millis. whats do you think about?
void write(byte[] data) throws IOException, InterruptedException {
write(data, false);
}
void write(byte[] data, boolean checkCTS) throws IOException, InterruptedException {
if(serialPort == null)
throw new IOException("not connected");
if(checkCTS && serialPort.getSupportedControlLines().contains(UsbSerialPort.ControlLine.CTS)) {
int writeCounter = 50;
for (int i = 50; i >= 0; i--) {
if (serialPort.getCTS()) break;
writeCounter = i;
Thread.sleep(1);
}
if (writeCounter < 1) throw new IOException("CTS Timeout");
}
serialPort.write(data, WRITE_WAIT_MILLIS);
}
might work in your case, but cannot replace real flow control (https://github.com/mik3y/usb-serial-for-android/issues/318)