Hi, I am working with ESP32 (CP2102 USB to UART Bridge Controller). I need to send text command to this device and then immediately read response synchronously in the same function to return data. I am using baud rate 11520 but with lower values the same issue. I checked also on Arduino Uno Rev 3 and the same issue.
if (serialPort.syncOpen()) {
Log.d(TAG, "port opened successfully!")
serialPort.setBaudRate(SERIAL_BAUD_RATE)
serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8)
serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1)
serialPort.setParity(UsbSerialInterface.PARITY_NONE)
/**
* Current flow control Options:
* UsbSerialInterface.FLOW_CONTROL_OFF
* UsbSerialInterface.FLOW_CONTROL_RTS_CTS only for CP2102 and FT232
* UsbSerialInterface.FLOW_CONTROL_DSR_DTR only for CP2102 and FT232
*/
serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF)
//....
}
//.....
//.....
private fun requestData(textMessage: String): String {
send(textMessage)
return read()
}
private fun send(textMessage: String) {
val messageByteArray = "$textMessage$NEW_LINE".toByteArray()
val writeResult = serialPort.syncWrite(messageByteArray, USB_WRITE_TIMEOUT)
}
fun read(): String {
val messageByteArray = ByteArray(1024) //16384 multiply of 64
val readDataLength = serialPort.syncRead(messageByteArray, USB_READ_TIMEOUT)
return messageByteArray.decodeToString()
}
const val SERIAL_BAUD_RATE = 115200 //57600 //115200
const val VENDOR_ID = 4292 // ESP32 4292 //arduino 9025
const val PRODUCT_ID = 60000 // ESP32 60000 //arduino 67
const val USB_READ_TIMEOUT = 3000
const val USB_WRITE_TIMEOUT = 1000
const val NEW_LINE = "\n"
syncWrite is returning result > 0 but messageByteArray from syncRead is empty or sometimes contains not cut and messy data.
What I'm doing wrong ? :)
Hi, I am working with ESP32 (CP2102 USB to UART Bridge Controller). I need to send text command to this device and then immediately read response synchronously in the same function to return data. I am using baud rate 11520 but with lower values the same issue. I checked also on Arduino Uno Rev 3 and the same issue.
syncWrite is returning result > 0 but messageByteArray from syncRead is empty or sometimes contains not cut and messy data. What I'm doing wrong ? :)
Code from ESP32 / Arduino