B4A Library UsbSerial library 2.0 - supports more devices

This is an expanded version of the original UsbSerial library. It has added support for Prolific PL2303 USB to serial converters, Android ADK devices and USB permissions. All devices use the same simple interface intended to be used with AsyncStreams and AsyncStreamsText. Note that AsyncStreams prefix mode is not supported. The library is based on the same open source project Android USB host serial driver library as the existing UsbSerial library but no longer needs a separate jar file as the project source code is incorporated in the library.

The specific enhancements to the library over the original UsbSerial library are :

UsbPresent, HasPermission and RequestPermission are added to identify any attached device or Accessory available to the library and deal with permission to access it.

SetParameters, which must be used after Open(), and the constants for SetParameters provides acess to all the serial line parameters instead of just baud rate.

DeviceInfo provides a string containing information about a device. This works for slave devices only.

Android Accessories, which are host mode devices, are recognised and can be used in the same way as the other slave mode devices.

Prolific PL2303 support is added.

Silicon Labs CP210x support is added - maybe only the CP2102 as I have no hardware to test.

The FTDI "status byte" bug on reading input that existed in version 1.0 of this library is hopefully fixed.


The usb-serial-for-android project and therefore also this library is licensed under the GNU Lesser General Public License v3. http://www.gnu.org/licenses/lgpl.html|http://www.gnu.org/licenses/lgpl.html
Copies of both the General Public License and Lesser General Public License are in the provided archive.

The user has to give your application permission to access the USB device before it can be opened. You can do this in two ways.

As with the original UsbSerial library you can add the following code to the manifest editor

B4X:
AddActivityText(main, <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />)
Then copy device_filter.xml from the demo in the attached archive to: <your project>\objects\res\xml and mark it as read-only. Note that this is an expanded version of the original device_filter.xml file.

Finally install the program and attach the USB device. A dialog will appear asking whether you want to start your program. If you check the “Use by default…” checkbox from now on when the USB device is plugged in your program will be started. If you don’t check the checkbox then you will be asked each time the device is plugged in.

A similar procedure can be used for Accessories as detailed in the “Using an intent filter” section here USB Accessory | Android Developers


Alternatively you can use the new HasPermission and RequestPermission methods without requiring any of the above steps. The demo in the archive incorporates both ways of obtaining permission.

EDIT:- Version 2.1 now posted. See post #4 for details

EDIT:- Version 2.2 now posted. See post #14 for details

EDIT:- Version 2.3 now posted. See post #26 for details

V2.4 is available here: http://www.basic4ppc.com/android/fo...pports-more-devices.28176/page-11#post-259167
This update adds support for devices connected to multiple USB adapters.
 

Attachments

  • UsbSerial2.3.zip
    99.2 KB · Views: 5,263
Last edited by a moderator:

walterf25

Expert
Licensed User
Hi all, is there a way to detect when the usb device has been disconnected physically from the device, I don't see an event that would be raised if this happened, has anyone had any success with this?

Am I missing something?

Thanks,
Walter
 

walterf25

Expert
Licensed User
Hi Erel thanks for the reply, i got it working actually i used the BroadCast library for this, now i have another problem :)

below are the subs where i detect when the usb cable has been attached or detached from the device

B4X:
Sub usbdisconnect_OnReceive (Action As String, i As Object)
  Dim i2 As Intent
  i2 = i

  Log("action " & Action)

  usb.Close
  astreams.Close
  SERIAL_CONNECTED = "Desconectado"
  CallSubDelayed3(Main, "updatescreen", SERIAL_CONNECTED, SOCKET_CONNECTED)

End Sub

Sub usbconnect_OnReceive (Action1 As String, i2 As Object)
  Dim i3 As Intent
  i3 = i2
  Log("action " & Action1)
  Start_Serial     'if cable is re-attached, start usb connection again.
End Sub

They work perfect, the problem now is that if i unplug the cable and reconnect it back on, even though i start the same process for opening the port again, it won't work, any ideas why this might be, the code where i open the open is below.

B4X:
Sub Start_Serial As Boolean
'Start_Serial=False
        If usb.UsbPresent = usb.USB_NONE Then
        Log("Msgbox - no device")
        'Msgbox("No USB device or accessory detected!", "Error")
        'Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission")
    If (usb.HasPermission) Then
    '    Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(38400)  
        If dev <> usb.USB_NONE Then
            Log("Connected successfully!")
            SERIAL_CONNECTED = "Conectado"
            astreams.Initialize(Me, "astreams", usb.GetInputStream, usb.GetOutputStream)
            'usbconnection.Enabled = True
        Else
            Log("Error opening USB port")
            SERIAL_CONNECTED = "Desconectado"
        End If
    Else
        usb.RequestPermission
    End If
  
    If astreams.IsInitialized Then
    astreams.Write("Welcome To USB Receiver" & Chr(10) & Chr(13))
    astreams.Write("By Carlos" & Chr(10) & Chr(13))
    End If
CallSubDelayed3(Main, "updatescreen", SERIAL_CONNECTED, "")
End Sub

Hope you have any advice on this!

thanks,
Walter
 

walterf25

Expert
Licensed User
Do you get any error message?
I don't see any error Erel, it seems that when you re-connect the usb cable a window pops up with the available apps that can be opened using the usb cable, once you click on the app, it seems that it opens up the app again and it works after i click the start button.

I wonder if there's a way to avoid from that window popping up and just re-open the port right away.

Thanks,
Walter
 

shmulik taiber

Member
Licensed User
Hello Erel .
I got the arduino to communicate with nexus 7 through usbserial . however, the info sometimes gets broken in half - for example the number 42 i get as 4 and afterwards 2 . any advise ? also, how can i send something bigger then a byte ?
 

JeanLC

Member
Licensed User
Hello,

Does anyone know how can I open two USB Serial ports using the UsbSerial library?
Does the UsbSerial library supports opening two of them? Apparently it gets just the 1st one.

Everything works great if I only use one of them. But if I want 2? Or 3? Or the same brand 2 FTDIs? :D

I have two USB Serial Ports No.1-Prolific and No.2-FTDI connected via USB HUB to an Android tablet.
I tried using usb1.SetCustomDevice forcing the Prolific and usb2.SetCustomDevice for the FTDI without luck. (with their VID and PID)
It always gets the same one, the 1st I connected.

Is there a way to do it?

Thanks.
 

JeanLC

Member
Licensed User
Thanks Erel
So sad it doesn't support it.
I would take a look into the library code... thanks again.
 

JeanLC

Member
Licensed User
Hello Erel,

I modified the Library for my use and it's working with 2 USB Serial devices. Should I upload the changes here so anyone can use it with two or more USB devices? Or contact Agraham?

So far it's working Ok, but I don't know if any bugs are hidding.
The calls look like this:
For Device 1: UsbPresent(1) ,HasPermission(1) , DeviceInfo(1), Open(9600, 1) , RequestPermission(1)
For Device 2: UsbPresent(2), HasPermission(2), DeviceInfo(2), Open(9600, 2), RequestPermission(2)
For Device n: UsbPresent(n), HasPermission(n), DeviceInfo(n), Open(9600, n), RequestPermission(n)

The catch is that I have to analyze the incoming data, check DeviceInfo(n) or something else to know what is connected to the USB because depending on the order you plug the USBs it's the order that shows up.
 

JeanLC

Member
Licensed User
UsbSerial Ver 2.4
What's new: Support for multiple USB devices
Version history Updated. You can find the changes by searching "Ver_2.4"
Maybe I over documented it. It was for me... feel free to remove the Nonsense
Library and Example updated to Ver 2.4
hoho driver also updated to support multiple devices
 

Attachments

  • USB_Serial_2.4.zip
    100.1 KB · Views: 1,930

Jaime Ramirez

Member
Licensed User
I am using the version 2.3 I am getting a No USB device or accessory detected error, I do have a FT232RL device attached to the phone, any ideas on what can be missing?
 

Jaime Ramirez

Member
Licensed User
@JeanLC thank you. I've added a link to your update in the first post.

@Jaime Ramirez if your device is rooted then try these steps: http://www.basic4ppc.com/android/fo...rs232-adaptor-driver.11449/page-3#post-121125
Erel:

My device is rooted, i used a tool name "USB Host Check" and that tools does those
@JeanLC thank you. I've added a link to your update in the first post.

@Jaime Ramirez if your device is rooted then try these steps: http://www.basic4ppc.com/android/fo...rs232-adaptor-driver.11449/page-3#post-121125
Erel:

My device is rooted, I used a tool named "USB Host Check", that tool creates and modifies the required files. I checked the files for those modifications to be there, then rebooted but still not detecting my FTDI RS232R device. I am using a small OTG cable to connect directly the phone to the FTDI device, who is supplying the power?..Do I have to use a powered USB hub maybe?
 

Jaime Ramirez

Member
Licensed User
The Android device is supplying the power. It is worth testing it with a powered hub. I encountered the same issue in the past: http://www.basic4ppc.com/android/forum/threads/android-usb-host-tutorial-adbtest.11289/#content
Erel:

Is there a way to easily enable the USB Host mode?, I am developing this app for a custom DSP board, but if the user has to root the device then enable the USB host mode this can be discouraging for the user as it may be perceived as a risky procedure....
 
Top