Android Question [solved] UsbSerial(killer) and DTR / RTS control

robotop

Member
Licensed User
Longtime User
Hi all. I want to use an interface based on CH340G chip (usb-serial). I tested the various UsbSerial libraries and unzipping every .jar I discovered that only the "felUsbSerial" has a driver for such chip.
Now, the serial communication, using this library is working, but I can't use the code for activating / deactivating the output lines RTS and DTR, as I did in the past with the UsbSerial2.4. In the java code of the CH340x driver (https://github.com/felHR85/UsbSerial/tree/master/usbserial/src/main/java/com/felhr/usbserial), the call of setDTR and setRTS is implemented, so this function MUST work !

I think the problem is in the different approach in opening the device.
The UsbSerial2.4 library uses a code like this:

B4X:
Sub usbOpen() As Int
    If usbserial.UsbPresent(1) = usbserial.USB_NONE Then
        Msgbox("USB device not found", "error")
        Return 1
    End If
    If usbserial.HasPermission(1) Then ' check permissions
        Dim dev As Int
        dev = usbserial.Open(115200, 1) ' try to open device     
        If dev <> usbserial.USB_NONE Then ' if connected
            usbserial.SetParameters(115200, usbserial.DATABITS_8, usbserial.STOPBITS_1, usbserial.PARITY_NONE)
            astreams.Initialize(usbserial.GetInputStream, usbserial.GetOutputStream, "astreams")
        Else ' if not connected
            Msgbox("Cannot use COM port", "error")
            Return 1
        End If
    Else
        usbserial.RequestPermission(1)
    End If
    Return 0
End Sub

while the felUsbSerial library uses this code (using USB manager):

B4X:
Sub usbOpen() As Int
   If manager.GetDevices.Length = 0 Then
        Msgbox("USB device not found", "error")
        Return 1
   End If
   Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
   If manager.HasPermission(device) = False Then
       Msgbox("Cannot use COM port", "error")
       manager.RequestPermission(device) ' let the user ask for permission
       Return 1
   End If
   usbserial.Initialize("serial", device, -1)
   usbserial.BaudRate = 115200
   usbserial.DataBits = usbserial.DATA_BITS_8
   usbserial.StartReading
   Return 0
End Sub

With the UsbSerial2.4 I used this code to set the RTS line:

B4X:
Sub setRTS
Dim R As Reflector
    R.Target = usbserial
    R.Target = R.getField("driver")
    R.RunMethod2("setRTS", True, "java.lang.boolean")
End Sub

but this gives me an exception if I use it with felUsbSerial. The error comes from the statement R.getfield("driver"), where "driver" is not found.

On the other side, using the UsbSerial2.4 library, the CH340 chip is not recognized. The only chip that is recognized and has the setRTS and setDTR functions active in driver class is the PL2303, but unfortunately I must use a CH340G.

It's possible to add the CH34xSerialDevice.class to the UsbSerial2.4 library ?
Second question (alternative): how can I use the reflector to directly access the setDTR and setRTS functions of the felUsbSerial library ?

Thank you in advance for any help...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
It's possible to add the CH34xSerialDevice.class to the UsbSerial2.4 library ?
No.

Download the latest version of felUsbSerial (v1.10).

You can call those methods with:
B4X:
Dim jo As JavaObject = usbserial
jo.RunMethod("setDTR", Array(True))
jo.RunMethod("setRTS", Array(True))
 
Upvote 0

robotop

Member
Licensed User
Longtime User
Thank you very much, Erel.
I will try immediatly !
 
Upvote 0

robotop

Member
Licensed User
Longtime User
Test done, but there is a problem.

I downloaded the felUsbSerial 1.10 and placed it in the libraries space (old version was overwritten, so I can't try to go back in previous situation !).

The problem is that the usb device isn't enabled as com port (hasn't permissions).
I used the standard manifest for compiling, so user must authorize the device, but I haven't seen any authorizing request when starting the App.

I uninstalled previous version of software and rebooted the tablet with interface connected, but the behaviour was the same.
.
I'm sure that it was recognized with previous version of the library (1.00), 'cause I sent out data from serial output !

Is it possible that something has changed in VID and PID of the device, from version 1.00 to 1.10 ?
My actual device has VID_1A86 PID_7523 (REV_0254).

Where can I download the old version of the library to test the previous state?

Is it possible to have the version number embedded in the name of the zip file, so that one can have a "history" of variations in a library ?

Thanks for your help !
 
Last edited:
Upvote 0

robotop

Member
Licensed User
Longtime User
mmmh, may be I missed the statement to ask for permission... I'm trying to make it now.
EDITED: Exactly... now all works as expected. THANK YOU VERY MUCH, Erel
 
Upvote 0
Top