0

I am working on a serial communication project in android with a USB connection. The response from the CH340 cable was correct on Modbus but when I connect it with android it shows only 85 characters strings whereas it should return 100 characters string. My Setup is : Android Device: Lenovo M10 Tab Serial Device at other end: Qinheng CH340

  1. USB request string to write :

    var string ="1,3,0,49,0,40,20,27"
    val spliteString: Array<String> = string.split(",").toTypedArray()
    val dataByte = arrayOfNulls<Byte>(spliteString.size)
    for (i in spliteString.indices) {
        dataByte[i] = spliteString[i].toInt(10).toByte()
    }
    val bytesArray = ByteArray(dataByte.size)
    var i = 0
    for (b in dataByte) bytesArray[i++] = b!!
    return bytesArray 

and then calling write data method, I have written the content on the USB as follows:

MyApp.driver.WriteData(bytesArray , bytesArray.size)
  1. Handle connection using the following

     if (MyApp.driver.SetConfig(19200, 8.toByte(), 2.toByte(), 0.toByte(), 0.toByte())) {
                    Toast.makeText(this@MainActivity, "Config successfully", Toast.LENGTH_SHORT).show()
                }

response from the serial device that I am writing:


    handler = object : Handler(Looper.myLooper()!!) {
                override fun handleMessage(msg: Message) {
                    readText!!.append(msg.obj as String)
                }
            }

Now response in Modbus shows 100 characters and in my android, it shows 85 characters.

For the connection, I have tried following libraries

https://github.com/felHR85/UsbSerial

but this won't work, when I have written on the USB serial port it nothing returns in the response.

Then I have used CH34xUARTDriver from http://www.wch.cn/download/CH341SER_ANDROID_ZIP.html but it only gives me a static 85 character string and not the one which should be received from the hardware.

Any help is appreciated.

Younes
  • 462
  • 7
  • 15
  • Welcome to StackOverflow. Please provide more information: Add the relevant code to your question. Describe the setup: which device is sending, what device is at the other end of the USB-to-serial cable? At the moment, it's close to impossible to help you. – Codo Feb 24 '21 at 06:47
  • okay thank you sir. I will update my question – krishna4741 Feb 24 '21 at 07:14
  • Thanks for adding the code. However, several things are still unclear: What device is at the other end of the USB-to-serial cable? What is "Modbus" (it usually refers to a protocol)? Which side is sending the 100 bytes and which side is receiving? What is the type of `MyApp.driver`? Which of the two mentioned Android driver libraries does the code use? – Codo Feb 24 '21 at 11:04

0 Answers0