SimpleUsbTerminal icon indicating copy to clipboard operation
SimpleUsbTerminal copied to clipboard

how to handle CTS detection in write method?

Open harryberlin opened this issue 1 year ago • 2 comments

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?

harryberlin avatar Jan 27 '24 20:01 harryberlin

What do you mean with CTS handling? Flow control as in https://github.com/mik3y/usb-serial-for-android/issues/318?

kai-morich avatar Feb 02 '24 10:02 kai-morich

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);
}

harryberlin avatar Feb 04 '24 19:02 harryberlin

might work in your case, but cannot replace real flow control (https://github.com/mik3y/usb-serial-for-android/issues/318)

kai-morich avatar Mar 23 '24 07:03 kai-morich