mik3y / usb-serial-for-android

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

Received garbage data onNewData callback #545

Closed TiksGanesh closed 8 months ago

TiksGanesh commented 10 months ago

Hi Team, I am using this library in my project. Connecting with CH340 type of hardware. Connection with hardware and communication is good. But it fails on below scenario

I am using polling mechanism in my application where sending command to hardware with help of SerialInputOutputManager class and writeAsync method.

Failure Scenario

  1. onNewData callback I receive garbage data sometimes
  2. Response time out and it calls onRunError callback
  3. Once exception is thrown on onRunError callback, it does not accept any more command continues to throw exception
  4. After step 3, I programatically disconnects and re-connect with hardware.

Suggest me solution, how to avoid above issues?

Implementation at my end

Connection code

 val usbSerialPort = usbSerialDriver.ports[0]
        try {
            usbSerialPort.open(connection)
            usbSerialPort.setParameters(
                9600,
                UsbSerialPort.DATABITS_8,
                UsbSerialPort.STOPBITS_1,
                UsbSerialPort.PARITY_NONE
            )
            usbSerialPort.dtr = true
            usbSerialPort.rts = true
            serialInputOutputManager = SerialInputOutputManager(usbSerialPort, this@SerialCommunicator)
            serialInputOutputManager?.writeTimeout = 2000
            serialInputOutputManager?.readTimeout = 500
            serialInputOutputManager?.readBufferSize = usbSerialPort.readEndpoint.maxPacketSize
            serialInputOutputManager?.start()
override fun onNewData(data: ByteArray?) {
        data?.let {
            val rawData = String(it, StandardCharsets.US_ASCII)
            buildDataFromHardware(rawData)
        }
    }
private fun buildDataFromHardware(rawData: String) {
        stringBuffer.append(rawData)
        val indexOfRCLFChar = rawData.indexOf('\r')
        isCompleteDataReceived = indexOfRCLFChar > 0
        if (isCompleteDataReceived){
            val output = stringBuffer.toString()
            stringBuffer.delete(0, stringBuffer.length)
            isCompleteDataReceived = false
            listener.onData(output, operationType)
            Logger.d(output)
        }
    }

Here are some logs from my application

Thu Nov 16 2023 at 11:42:48:723 pm - Connection: Connecting with hardware Thu Nov 16 2023 at 11:42:48:724 pm - Operation Type: READ_STATUS | Error: Run Time Error Thu Nov 16 2023 at 11:42:48:749 pm - Connection: true | Error:
Thu Nov 16 2023 at 11:42:48:755 pm - Polling: Started Thu Nov 16 2023 at 11:42:48:761 pm - Operation Type: READ_STATUS | Command: $1li,0,20,OE,28

Thu Nov 16 2023 at 11:42:50:781 pm - Operation Status: Waiting to complete previous operation Thu Nov 16 2023 at 11:42:52:811 pm - Operation Status: Waiting to complete previous operation Thu Nov 16 2023 at 11:42:56:844 pm - Operation Status: Waiting to complete previous operation Thu Nov 16 2023 at 11:43:02:879 pm - Operation Status: Retry attempt failed Thu Nov 16 2023 at 11:43:02:903 pm - Polling: Stopped Thu Nov 16 2023 at 11:43:02:914 pm - Connection: Disconnected Thu Nov 16 2023 at 11:43:02:924 pm - Connection: Connecting with hardware Thu Nov 16 2023 at 11:43:02:926 pm - Operation Type: READ_STATUS | Error: Run Time Error Thu Nov 16 2023 at 11:43:02:955 pm - Connection: true | Error:
Thu Nov 16 2023 at 11:43:02:957 pm - Polling: Started Thu Nov 16 2023 at 11:43:02:961 pm - Operation Type: READ_STATUS | Command: $1li,0,20,OE,28

Thu Nov 16 2023 at 11:43:03:522 pm - Operation Type: READ_STATUS | Response: �/I� ��b�b�b�b�b��bj�/I� ��b�b�b�b�b��bj�i,001,0,1,*,001,0,0,0,1,51, Thu Nov 16 2023 at 11:43:04:981 pm - Operation Type: READ_STATUS | Command: $1li,0,20,OE,28

Thu Nov 16 2023 at 11:43:07:002 pm - Operation Status: Waiting to complete previous operation Thu Nov 16 2023 at 11:43:09:022 pm - Operation Status: Waiting to complete previous operation

kai-morich commented 10 months ago

is the baud-rate slightly correct, or slightly different?

TiksGanesh commented 10 months ago

@kai-morich, Baud rate is suggested by hardware team.

Addition Information There is software for windows machine it reads data correctly. It does not throw garbage.

kai-morich commented 10 months ago

power supply from your Android device could be to week. Please try external power supply