felHR85 / UsbSerial

Usb serial controller for Android
MIT License
1.81k stars 589 forks source link

FTDI receives two 0 bytes for every one byte of any value in sync mode #324

Open subvertallchris opened 3 years ago

subvertallchris commented 3 years ago

I'm using an FTDI 232R and trying to use sync mode so I can work around the inability to change the read buffer size in async mode.

When sending, the FTDI receives two 0s for every one byte of any value. For example:

port.syncWrite(byteArrayOf(1, 1, 1), 0)

The FTDI will receive [0, 0, 0, 0, 0, 0].

After entirely too much troubleshooting, I found this is caused by calling port.open() followed by port.syncOpen(). I changed from this:

port.open()
port.setBaudRate(460800)
port.setDataBits(UsbSerialInterface.DATA_BITS_8)
port.setStopBits(UsbSerialInterface.STOP_BITS_1)
port.setParity(UsbSerialInterface.PARITY_NONE)
port.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF)
port.syncOpen()

To this:

port.syncOpen()
port.setBaudRate(460800)
port.setDataBits(UsbSerialInterface.DATA_BITS_8)
port.setStopBits(UsbSerialInterface.STOP_BITS_1)
port.setParity(UsbSerialInterface.PARITY_NONE)
port.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF)

The code examples led me to believe that one should call open() to open the connection and then syncOpen() to enter sync mode but this appears to be incorrect. Just use syncOpen.

I don't expect a resolution to this issue but I struggled with this for way too long and I hope this provides help to someone in the future.

snyderdan commented 3 years ago

Your troubleshooting time was not spent in vein, thank you. I had the exact same scenario, and you saved me some real headache.