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
- 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)
- 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.