diff --git a/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java b/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java index 1c7f4ed..39c2d90 100644 --- a/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java +++ b/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java @@ -100,9 +100,9 @@ public class CP210XSerialPort extends AbstractSerialPort { initConnection(); try { throwIfNegative(sendControlTransfer(IFC_ENABLE, UART_ENABLE, null), "Failed to enable uart (error: %d)."); + throwIfNegative(initBaudRate(), "Failed to set baud rate (error: %d)."); throwIfNegative(initParameters(), "Failed to set parameters (error: %d)."); throwIfNegative(initFlowControl(), "Failed to set flow control (error: %d)."); - throwIfNegative(initBaudRate(), "Failed to set baud rate (error: %d)."); } catch (IOException e) { close(); throw e; @@ -116,8 +116,10 @@ public class CP210XSerialPort extends AbstractSerialPort { * @throws IOException Thrown if something fails. */ protected int initParameters() throws IOException { - int config = getStopBits() | getDataBits() | getParity(); - return sendControlTransfer(SET_LINE_CTL, config, null); + int value = getLineCtl(); + value &= ~0x05F3; + value &= getStopBits() | getDataBits() | getParity(); + return sendControlTransfer(SET_LINE_CTL, value, null); } /** @@ -195,7 +197,11 @@ public class CP210XSerialPort extends AbstractSerialPort { (byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00 }; - return sendControlTransfer(SET_FLOW, 0, dataOff); + // work-around for RPLidar, don't set the flow control if it is off + // TODO: check Linux driver to figure out how this should be done properly + // see https://github.com/torvalds/linux/blob/master/drivers/usb/serial/cp210x.c + //return sendControlTransfer(SET_FLOW, 0, dataOff); + return 0; case DSR_DTR: byte[] dataDSRDTR = new byte[]{ (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x00, @@ -228,8 +234,6 @@ public class CP210XSerialPort extends AbstractSerialPort { return Math.min(res, sendControlTransfer(SET_FLOW, 0, dataXONXOFF)); } throw new IOException("Unsupported flow control configuration."); - - } /**