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

Support for reading RI control changes #527

Closed SDOperations closed 4 months ago

SDOperations commented 1 year ago

Hi! Thanks for this great library!

We use this on one of our applications and it works great for getting serial data and such.

However we now require reading RI control changes. Is that possible using this?

Thanks

kai-morich commented 1 year ago

You can use getRI method https://github.com/mik3y/usb-serial-for-android/blob/fd8c155ca5f69ec80f8f0ac28bb73ffc97b92511/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialPort.java#L202

SDOperations commented 1 year ago

Hi @kai-morich,

It seems I don't quite understand how this works.

How can I attach an interrupt handler to execute a method whenever the RI changes?

I could not find an example for this in the samples.

Thank you very much!

SDOperations commented 1 year ago

I was not able to detect whether or not the getRI method works.

I created a new thread to verify the RI status.

Please note I was able to verify the RI status through realterm.

val ppsThread = Thread {
            while (true) {
                try {
                      if (serialPort.getRI()) {
                          Log.d("Serial State", "RI State is on!");
                      }

                    Thread.sleep(1)
                } catch (e: Exception) {
                    Log.d(this.javaClass.simpleName, e.message)
                }
            }
       }

        Log.d("Serial", "Starting RI Thread")
        ppsThread.start();

The device in question uses the FTDI Serial Driver

kai-morich commented 1 year ago

you have to poll for changes as you did in above example.

note: the FTDI device is the only device that regularily sends control line status as side effect of read call, but as it is not supported by other devices and your app not necessarily always does read, this info is not used, but the FTDI device is actively called in your poll.

SDOperations commented 1 year ago

Do you have any suggestions I could try? I don't quite understand what you meant with the following:

but as it is not supported by other devices and your app not necessarily always does read, this info is not used,

I also tried it without the sleep method, this does not seem to impact the results.

kai-morich commented 1 year ago

Your above code snippet is exactly what you need, but you only sleep for 1 milliseconds. I recommend something like 100 milliseconds to avoid high CPU load and USB traffic.