B4A Library felUsbSerial - Alternative Usb Serial library

Status
Not open for further replies.
This library wraps the following open source project: https://github.com/felHR85/UsbSerial (MIT license).

It is an alternative to the UsbSerial2 library.

The following devices are supported: CP210x, CDC, FTDI, PL2303 and CH34x.

Usage is simple. You find the UsbDevice with USB library and then initialize felUsbSerial, configure it and start reading.

B4X:
Sub Process_Globals
   Private usbserial As felUsbSerial
   Private manager As UsbManager
   Private bc As ByteConverter
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     manager.Initialize
   End If
   Dim btnConnect As Button
   btnConnect.Initialize("btnConnect")
   btnConnect.Text = "Connect"
   Activity.AddView(btnConnect, 10dip, 10dip, 100dip, 100dip)
End Sub

Sub btnConnect_Click
   If manager.GetDevices.Length = 0 Then
     Log("No connected usb devices.")
   Else
     Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
     If manager.HasPermission(device) = False Then
       ToastMessageShow("Please allow connection and click again.", True)
       manager.RequestPermission(device)
     Else
       usbserial.Initialize("serial", device, -1)
       usbserial.BaudRate = 115200
       usbserial.DataBits = usbserial.DATA_BITS_8
       usbserial.StartReading
     End If
   End If
End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)
   Log("New data: " & bc.HexFromBytes(Buffer))
End Sub

SetFlowControl is only implemented for CP2102, FTDI and CH34x chips.

Updates

V1.12 - BUFFER_READ_SIZE / BUFFER_WRITE_SIZE fields. Default value is 16 * 1024. You can change these values before initializing the object to change the internal buffers sizes.
 

Attachments

  • felUsbSerial.zip
    75.2 KB · Views: 2,587
Last edited:

red30

Well-Known Member
Licensed User
Longtime User
USB host feature is not implemented perfectly on all devices. Which USB device are you connecting to the Android?
"I connect to the microcontroller PIC18F14K50, which operates in USB CDC mode.everything works ok on all devices that work with OTG, but the tablet RoverPad Sky Glory S7 did not see the connected device. I did as written here (https://www.b4x.com/android/forum/t...rs232-adaptor-driver.11449/page-3#post-121125), - it works. I plugged the other tablet (Explay Scream 3G), made everything as the link still does not see the connected device. But the "USB Host Controller" software sees the device! You understand?"
 

miguelcleman

Member
Licensed User
Longtime User
It´s possible make a usb serial connection has fixed or permanent permission? (whitout msgbox confirmation?)

sory, my english is very bad!
 

drponciano

Member
Licensed User
Longtime User
Hello: When I try to complie the example I get the following error:

usbserial.Initialize(\
javac 1.8.0_102
src\b4a\example\main.java:375: error: package android.hardware.usb does not exist
_usbserial.Initialize("serial",(android.hardware.usb.UsbDevice)(_device.getObject()),(int) (-1));

Seems like it has to do with USBMANAGER. Any idea what am I doing wrong?
 

rtek1000

Active Member
Licensed User
Longtime User
Hi @Erel,

You know one user has a problem with the other library, and you suggested this one, but I tested it and they both do not work.

If the connected device (such as Arduino) sends data quickly without a space (pause) between transmissions, this library and the other one stops.

But the official FTDI app works great, (up to 921600 bauds, flow ctrl: none) and your surce code is available at:
Http://www.ftdichip.com/Support/SoftwareExamples/Android/J2xx_HyperTerm.zip
Http://www.ftdichip.com/Android.htm
Https://play.google.com/store/apps/details?id=com.ftdi.j2xx.hyperterm

Make sure that you or your team can make a new library, or update one, with FTDI source.

Thank you very much!
 

drponciano

Member
Licensed User
Longtime User
Thanks Erel. I had a very old android.jar and now, with the new one, I don't have the compiler error. Using felUSBSerial library the problem gets worse as AsyncStream is called every 15Kbytes instead of the 8k bytes with UBSSerial library. Quite an interesting situation, no?
 

drponciano

Member
Licensed User
Longtime User
Thanks Erele. That is, sureley, what I'm looking for and will solve my problem but, at run time, I get the following error in line

jo.InitializeStatic("com.felhr.usbserial.SerialBuffer").SetField("DEFAULT_READ_BUFFER_SIZE", 2048)


** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
Error occurred on line: 16 (Starter)
java.lang.IllegalAccessException: field is marked 'final'
at java.lang.reflect.Field.setField(Native Method)
at java.lang.reflect.Field.set(Field.java:585)
at anywheresoftware.b4j.object.JavaObject.SetField(JavaObject.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)

...

** Service (starter) Start **
** Activity (main) Resume **

Any chance a similar solution using USBSERIAL 2.4?

Sorry for taking so much of your time but appreciate your help.
 
Last edited:

drponciano

Member
Licensed User
Longtime User
Thanks Erel. Done with reflector. Still chuncks of 15782 bytes with felusbserial as before. Using USBSerial 2.4 the packet is 7936 bytes or about 1.5 seconds of data.
 

Beja

Expert
Licensed User
Longtime User
Usage is simple. You find the UsbDevice with USB library and then initialize felUsbSerial, configure it and start reading.

Hi Erel,
Start reading as I understand is for reading INTO the phone or Android device (like reading sensor values...etc).. Is it also possible to write to the outside world with this library? In other words, I want to use a smartphone with an app with this library to access and control a machine by connecting the smartphone's usb with the target machine's usb port.

Thanks
 

Beja

Expert
Licensed User
Longtime User
Another question:
Today I tried to run the app but there's no response from the "Connect" button.. it's enabled and can be pressed and raised but there's no action.. even when I disconnected the device, it didn't give me the error msg that now device is connected.
I hope there will be some suggestions or a solution to this.
 
Status
Not open for further replies.
Top