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,584
Last edited:

Emanuel Straub

Member
Licensed User
Longtime User
Yes, it's only happen when both devices are connected.

Do i need it when i only want to do a write on it? I'll give it a try.
 

Emanuel Straub

Member
Licensed User
Longtime User
I dont know if it is limited to a single device.
It's working for a few seconds and then application freezes for some seconds.
After that the application freeze totaly and androids crash and reboots.

And Some times the application freeze totaly and after i disconnected the usb cable the application recovers.
 

Thuong

Member
Licensed User
Longtime User
Thank Erel
Good job !
I've done with China's Arduino Nano (chip CH340)
 

entolium

Member
Licensed User
Longtime User
Thank Erel! Great work!

I'm using with Arduino and works fine, but I have a problem. I send an instruction, and I need to wait for the response.
If use a "Do while" with Doevents, the "DataAvailable" event don't raise... (?)
Without the "Do While", the program runs perfect.

my code is:

Sub Enviar
Respuesta=""
usbserial.Write(bc.StringToBytes(txtEnviar.Text & CRLF,"UTF8"))
Do While Respuesta=""
DoEvents
Loop
txtRespuesta.Text=Respuesta
End Sub

Sub serial_DataAvailable (Buffer() As Byte)
Respuesta=bc.StringFromBytes(Buffer,"UTF8")
End Sub


Any idea?
 

entolium

Member
Licensed User
Longtime User
Hi Erel,

I'm not sure where is the best place for this question :confused:.
The code is very simple, but only works when I don't use the "Do while". I think could be a problem with the library.
 

red30

Well-Known Member
Licensed User
Longtime User
Hi!
I get the array of data:
Sub fel_DataAvailable (Buffer() As Byte)

End Sub
The received array consists of 8 bytes: Buffer(1),Buffer(2), etc. I need to work with each bit of each byte separatly ( f.e. I need to get the 5th bit of the Buffer(1)). How can I do it?
Thank you for the responce.
 

red30

Well-Known Member
Licensed User
Longtime User
Your programme works well but I have a question.
When I connect to the device and exchange data and take the USB OTG cable out, the app closes with an error.

Can I write a tost message that "USB cable is taken out" and to relaunch the app automatically?
 

red30

Well-Known Member
Licensed User
Longtime User
Can you post the error message from the logs?
I can not give Logs, since I can not connect wirelessly B4A for debugging ...
I'm using the USB cable for debug, and since I'm working with otg, I can not connect simultaneously debugging and the device which i am working with

Tablet RoverPad Sky Glory S7, android 5.1. There is a otg support, a flash card is supported too, a mouse too. But when i try to connect my device UsbDevices.Length = 0. Why?
 

red30

Well-Known Member
Licensed User
Longtime User
Why can't you connect?
The tablet is connected to WF router which is connected to switch which is connected to server. The computer is connected to the server too. I run B4A-bridge and push "Start Wireless" and then in "My IP" my router's IP is written. In B4A i write this adress and push Connect, then write this IP and it replies : Ping failed, TimeOut.
I cant understand why...
This means that the OS didn't recognize any connected USB device.
No, it doesnt recognise only this device, flash cards, mouses etc are ok.
I install USB host controller from Playmarket and my devise is in the list of otg devices there, so its VID and PID are correct there.
 

red30

Well-Known Member
Licensed User
Longtime User
I meant that the OS didn't recognize the connected device.
You can try this "fix": https://www.b4x.com/android/forum/t...rs232-adaptor-driver.11449/page-3#post-121125
I did everything as written. On the same device all at once earned (RoverPad Sky Glory S7). In a second (Explay Scream 3G) everything remains the same .. Why?

Today i got the same error on explay Explay Scream 3G. It reads all otg device, but when i connect my : UsbDevices.Length = 0 . The programm usb host controller also sees my device
 

Attachments

  • android.hardware.usb.host.xml
    86 bytes · Views: 445
  • handheld_core_hardware.xml
    4 KB · Views: 413
Status
Not open for further replies.
Top