mik3y / usb-serial-for-android

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

Configure the serial communication parameters #512

Closed tonnqueiroz closed 1 year ago

tonnqueiroz commented 1 year ago

Can anyone help me, please?

I'm trying to send CLI commands to a CISCO Router (Model C921-4P) through Kotlin code, but I'm having trouble with the penultimate step of the process.

First Driver: com.hoho.android.usbserial.driver.ProlificSerialDriver@410e752

I can open the connection with the device: android.hardware.usb.UsbDeviceConnection@3be0c0d I get the port of the device: _ProlificSerialPort device_name=/dev/bus/usb/002/002 device_id=2002 portnumber=0 I open the port: OK

BUT I CAN'T CONFIGURE THE SERIAL COMMUNICATION PARAMETERS IF THE LOGIC USED TO CONFIRM IF THIS STEP IS CORRECT...

// Parameters contained in the equipment manual
val desiredBaudRate = 9600
val desiredDataBits = 8
val desiredStopBits = UsbSerialPort.STOPBITS_1
val desiredParity = UsbSerialPort.PARITY_NONE

// Configure the serial port parameters
port?.setParameters(desiredBaudRate, desiredDataBits, desiredStopBits, desiredParity)

// Try to read a few bytes to verify if the parameters are correctly configured
val buffer = ByteArray(10)
val numBytesRead = port?.read(buffer, 1000) ?: 0

if (numBytesRead == buffer.size) {
    // The parameters are correctly configured
    Log.d("CLI", "Serial communication parameters configured correctly")
} else {
    // The parameters are not configured correctly
    return "Error configuring the serial communication parameters"
}
kai-morich commented 1 year ago

you cannot read a fixed number of bytes, see fragmentation and buffer size