mik3y / usb-serial-for-android

Android USB host serial driver library for CDC, FTDI, Arduino and other devices.
MIT License
4.72k stars 1.57k forks source link

Data being received sliced in two parts #573

Closed rdelvillara closed 2 months ago

rdelvillara commented 2 months ago

Hello guys,

I have an Android application that receives serial data from a physical device. To simulate the device we are using Docklight. For some unknown reason, the data being sent by Docklight is being received in two parts. First I received one byte, then the rest of the byte array, meaning the listener function onNewData is being called twice for every sequence sent from Docklight.

It is important to note that the Android app has previously been tested as we also use another cable with a different chipset, for that we use a different usb-serial library, and everything works fine.

This is the code we are using to configure the serial port.

    override fun startSerialConnection(device: UsbDevice, connection: UsbDeviceConnection) {
        val driver = usbDefaultProber.probeDevice(device)
        usbSerialPort = driver.ports[0]

        try{
            usbSerialPort!!.open(connection)
            usbSerialPort!!.setParameters(
                BaudRate.BPS19200.value,
                UsbSerialPort.DATABITS_7,
                UsbSerialPort.STOPBITS_1,
                UsbSerialPort.PARITY_EVEN
            )
            usbSerialPort!!.rts = false
            usbSerialPort!!.dtr = false

            usbIoManager = SerialInputOutputManager(usbSerialPort, this)
            usbIoManager!!.readTimeout = READ_WAIT_MILLIS
            usbIoManager!!.start()
        } catch (e: IOException){
            sendSignal(Constants.ACTION_USB_NOT_SUPPORTED)
        }

        USBSerial.serialPortConnected = true
        Log.d(TAG, "USB READY")
        sendSignal(Constants.ACTION_USB_READY)
    }

Am I missing anything?

kai-morich commented 2 months ago

see https://github.com/mik3y/usb-serial-for-android/wiki/FAQ#user-content-When_reading_a_message_why_dont_all_bytes_arrive_at_the_same_time