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

Beja

Expert
Licensed User
Which device have you connected to the USB port?

Thanks Erel.. This one.

UT-885
UT-885.jpg
 

marcbu

Member
Licensed User
I try to get FT312 (as host via AOA) running but I get runtime error

B4X:
Sub Connect_FT312D
    Dim usbserial As felUsbSerial
    Dim manager As UsbManager ' normally declared in Process_Globals
    Dim UsbAcc() As UsbAccessory
   
    manager.Initialize 'normally in Activity_Create(FirstTime)


    If manager.GetDevices.Length = 0 Then
        Log("No connected usb devices.")  'Does not find any device
    End If

    UsbAcc = manager.GetAccessories
    If UsbAcc(0).Model.EndsWith("FT312D") Then  ' for test only short version without iteration
        Log("Device found " & UsbAcc(0).Model)    ' Does find the device
    End If
    ' .... after manager.HasAccessoryPermission is True
    usbserial.Initialize("FTDI_data",UsbAcc(0),-1) ' is there any typecast possible for parameter 2
    'Error: java.lang.IllegalArgumentException: argument 2 should have type android.hardware.usb.UsbDevice, got anywheresoftware.b4a.objects.usb.UsbManagerWrapper$UsbAccessoryWrapper
End Sub

How is it possible to do a type cast for second parameter for usbserial.Initialize?
 
Last edited:

mast4rbug

Member
Licensed User
Hi. Thanks for this USB Serial implementation, it works really fine. Only one thing, about the real time, I notice some lag before the Data is sent, between 0 second to about 1 second randomly each time I click my button, is it normal? I need the Data to be sent immediately without Delay.
Thanks!
 

mast4rbug

Member
Licensed User
When I use the other lib with USBSERIAL instead of FELUSBSERIAL the communication is real time, but when I connect with usb.Open(9600) in the example, it connect or send data only 1 time on 10 try, it make Java IO error often when I send Data... I don't know why.
 

carycai

Member
Licensed User
please see the attach picture.

I add a timer into the demo code,send string "test" every 1 second.And receive string was splited into two lines:"t" and "est". (ps: I link the serial pin 2 & 3 with wire,so it can write and read by itself.)
Why? Is there anything wrong?
Please help me.
 

Attachments

  • test.png
    test.png
    49.8 KB · Views: 112
Last edited:

Filippo

Expert
Licensed User
Hi Erel,

can this Digispark be implemented in the library?
 

Filippo

Expert
Licensed User
I do not know if that can help, here's the output of " StartingIntent.ExtrasToString":
Logger connected to: asus Nexus 7
--------- beginning of crash
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (smcrono) Create ***
** Service (smcrono) Start **
** Receiver (smcmm) OnReceive **
*** Service (smcmm) Create ***
** Service (smcmm) Start **
USB_DEVICE_ATTACHED:Bundle[{device=UsbDevice[mName=/dev/bus/usb/002/003,mVendorId=5840,mProductId=1875,mClass=255,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=null,mSerialNumber=null,mConfigurations=[
UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=50,mInterfaces=[
UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=0,mSubclass=0,mProtocol=0,mEndpoints=[]]]}]

Here are also various Ini files of Windows drivers, maybe it is also helpful.
 

Attachments

  • Digistump Drivers.zip
    5.7 KB · Views: 106

rtek1000

Active Member
Licensed User
Hello,

I am very happy with this library, I am able to transfer a lot of data from an Arduino Nano with FTDI converter at a speed of 230400 bauds!
(Device: Smartphone Motorola Moto G2 and Android 6)

1- How can I detect if the port has already been opened?

I've put a control variable, but I think it would be useful if the library has some means of monitoring.

2- In case of bad contact, or if the user disconnects the USB cable, what would be the best way to detect this?
 
Status
Not open for further replies.
Top