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:
while the felUsbSerial library uses this code (using USB manager):
With the UsbSerial2.4 I used this code to set the RTS line:
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...
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: