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.b4x.com/android/forum/th...pports-more-devices.28176/page-11#post-259167
This update adds support for devices connected to multiple USB adapters.


V2.5 is available as an attachment to this post. It is identical to version 2.4 referenced aboce but adds the required flag for Pending Intents when targeting SDK 31+.
 

Attachments

  • UsbSerial2.3.zip
    99.2 KB · Views: 6,019
  • UsbSerial2.5.zip
    36.3 KB · Views: 653
Last edited:

agraham

Expert
Licensed User
Longtime User
USB library - Low level access to any USB device that Android lets your program see. Can be difficult as the Android USB implementation deeply sucks and two out of the four possible bulk transfer modes are irredeemable broken, have been since the beginning, and there is no sign of them ever being fixed as far as I know!

UsbSerial library - High level access to certain common USB to RS232 serial adapter chips and Android ADK devices.
 

pixelpop

Active Member
Licensed User
Longtime User
Thanks for explaining the difference. I suppose I was a bit naïve on the whole issue of USB. I will bear this in mind should implementing a USB interface come up again in my development. The USB library is working and, as the old saying goes, if it ain't broke don't fix it. :)

Again...thanks and I really appreciate all the support that this forum provides.
 
Last edited:

pxpto

Member
Licensed User
Longtime User
Hi, I'm having a small problem with UsbSerial 2.3. I'm using the demo project included in the ZIP file.

I initialize the COM port with:

B4X:
Dim dev As Int
dev = usb.Open(9600)
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")

..and I can send texts with:

B4X:
astreams.Write(TextToSend.GetBytes("UTF8"))

..and receive the response with:

B4X:
Sub Astreams_NewData (Buffer() As Byte)
ReceivedByte = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Byte received: " & ReceivedByte)
End Sub

The problem is that if the response is too long, my app doesn't read it all.

I'm using my PC to send "HELLO WORLD" and it works. The B4A app gets "HELLO WORLD".

If I send something like "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", the B4A only receives "A,B,C,D,E,F,G,H,I,J,K,L,M,N". It doesn't receive the rest: ",O,P,Q,R,S,T,U,V,W,X,Y,Z".

I'm using 9600N1 and already tried it with another serial device.

Can anyone help? Thank you!
 

pxpto

Member
Licensed User
Longtime User
Hi, I'm having a small problem with UsbSerial 2.3. I'm using the demo project included in the ZIP file.

I initialize the COM port with:

B4X:
Dim dev As Int
dev = usb.Open(9600)
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")

..and I can send texts with:

B4X:
astreams.Write(TextToSend.GetBytes("UTF8"))

..and receive the response with:

B4X:
Sub Astreams_NewData (Buffer() As Byte)
ReceivedByte = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Byte received: " & ReceivedByte)
End Sub

The problem is that if the response is too long, my app doesn't read it all.

I'm using my PC to send "HELLO WORLD" and it works. The B4A app gets "HELLO WORLD".

If I send something like "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", the B4A only receives "A,B,C,D,E,F,G,H,I,J,K,L,M,N". It doesn't receive the rest: ",O,P,Q,R,S,T,U,V,W,X,Y,Z".

I'm using 9600N1 and already tried it with another serial device.

Can anyone help? Thank you!

This problem doesn't occur at 115200 bps.. But I really need to use 9600 bps.. Any suggestions?
 

pxpto

Member
Licensed User
Longtime User
Hi, I'm having a small problem with UsbSerial 2.3. I'm using the demo project included in the ZIP file.

I initialize the COM port with:

B4X:
Dim dev As Int
dev = usb.Open(9600)
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")

..and I can send texts with:

B4X:
astreams.Write(TextToSend.GetBytes("UTF8"))

..and receive the response with:

B4X:
Sub Astreams_NewData (Buffer() As Byte)
ReceivedByte = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log("Byte received: " & ReceivedByte)
End Sub

The problem is that if the response is too long, my app doesn't read it all.

I'm using my PC to send "HELLO WORLD" and it works. The B4A app gets "HELLO WORLD".

If I send something like "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", the B4A only receives "A,B,C,D,E,F,G,H,I,J,K,L,M,N". It doesn't receive the rest: ",O,P,Q,R,S,T,U,V,W,X,Y,Z".

I'm using 9600N1 and already tried it with another serial device.

Can anyone help? Thank you!

I did some additional tests. This is where my app receives text from the serial port:

B4X:
Sub Astreams_NewData (Buffer() As Byte)
    ReceivedBytes = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log("Astreams_NewData received " & Buffer.Length & " bytes: " & ReceivedBytes)
End Sub

Then I send it the following text: "A.B.C.D.E.F.G.H.I.J.K.L.M.N.O.P.Q.R.S.T.U.V.W.X.Y.Z"

When I'm using 9600 bps it gives me this:

Astreams_NewData received 1 bytes: A
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: B
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: C
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: D
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: E
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: F
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: G
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: H
Astreams_NewData received 1 bytes: .
Astreams_NewData received 4 bytes: I.J.
Astreams_NewData received 1 bytes: K
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: L
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: M
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: N
Astreams_NewData received 1 bytes: .
Astreams_NewData received 1 bytes: O
(no more text)

But if work at 115200 bps I receive all the text:

Astreams_NewData received 1 bytes: A
Astreams_NewData received 24 bytes: .B.C.D.E.F.G.H.I.J.K.L.M
Astreams_NewData received 19 bytes: .N.O.P.Q.R.S.T.U.V.
Astreams_NewData received 9 bytes: W.X.Y.Z

I've played with the "UsbTimeout" setting with no success. Any ideas?
 

pxpto

Member
Licensed User
Longtime User
@pxpto it seems like your adapter doesn't properly support this baud rate.

Hi @Erel thank you for your reply. I've been using this USB-Serial adapter for years with absolutely no issues on many projects, and I tested a different adapter and got the same result.

The usb.DeviceInfo for my 2 adapters:

First adapter:

B4X:
Manufacturer : not available
Product : not available
Serial : not available
DeviceName : /dev/bus/usb/001/005
DeviceClass : USB_CLASS_PER_INTERFACE (per-interface basis)
DeviceSubClass : 0
Device ID : 0x3ED
ProductId : 0x2303
VendorId  :0x67B
  B4aInterfaceNumber : 0
  InterfaceClass : USB_CLASS_VENDOR_SPEC (vendor specific)
  InterfaceSubClass : 0
  InterfaceProtocol : 0
    EndpointNumber : 1
    EndpointDirection : In
    EndpointType : USB_ENDPOINT_XFER_INT (interrupt)
    EndpointAttribute : 3
    EndpointInterval : 1
    EndpointMaxPacketSize : 10
    EndpointNumber : 2
    EndpointDirection : out
    EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
    EndpointAttribute : 2
    EndpointInterval : 0
    EndpointMaxPacketSize : 64
    EndpointNumber : 3
    EndpointDirection : In
    EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
    EndpointAttribute : 2
    EndpointInterval : 0
    EndpointMaxPacketSize : 64

Second adapter:

B4X:
Manufacturer : Prolific Technology Inc.��������������
Product : USB-Serial Controller��������������������
Serial : not available
DeviceName : /dev/bus/usb/001/004
DeviceClass : USB_CLASS_PER_INTERFACE (per-interface basis)
DeviceSubClass : 0
Device ID : 0x3EC
ProductId : 0x2303
VendorId  :0x67B
  B4aInterfaceNumber : 0
  InterfaceClass : USB_CLASS_VENDOR_SPEC (vendor specific)
  InterfaceSubClass : 0
  InterfaceProtocol : 0
    EndpointNumber : 1
    EndpointDirection : In
    EndpointType : USB_ENDPOINT_XFER_INT (interrupt)
    EndpointAttribute : 3
    EndpointInterval : 1
    EndpointMaxPacketSize : 10
    EndpointNumber : 2
    EndpointDirection : out
    EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
    EndpointAttribute : 2
    EndpointInterval : 0
    EndpointMaxPacketSize : 64
    EndpointNumber : 3
    EndpointDirection : In
    EndpointType : USB_ENDPOINT_XFER_BULK (bulk)
    EndpointAttribute : 2
    EndpointInterval : 0
    EndpointMaxPacketSize : 64
 
Last edited:

pxpto

Member
Licensed User
Longtime User
I'm using my PC to send "HELLO WORLD" and it works. The B4A app gets "HELLO WORLD".

If I send something like "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z", the B4A only receives "A,B,C,D,E,F,G,H,I,J,K,L,M,N". It doesn't receive the rest: ",O,P,Q,R,S,T,U,V,W,X,Y,Z".

I'm using 9600N1 and already tried it with another serial device.

I found the problem! I was running the app in Debug mode! It somehow (and comprehensibly) delays the app and messes with time critical libraries like UsbSerial. After compiling it in Release mode it works normally!
 

micro

Well-Known Member
Licensed User
Longtime User
Hi to all,
In my application usb converter work fine,
only that whenever i turn off/on the tablet and restart the application asked me for permission to associate the usb in the program.
In etc/permission the xml files are correct.
Why?
Thanks
 

micro

Well-Known Member
Licensed User
Longtime User
sincerely erel
before, the table did not recognize any usb serial converts, after I downloaded and running
UsbDiagnostic and automatic fixed the problem
After rebooting the tablet has recognized the serial converters and work fine.
The only problem now is the permission.
 

entolium

Member
Licensed User
Longtime User
Hi,

Fantastic Lib!!!!!

But....
I'm reading de java source code, and is very difficult for me :(. I think will be a good idea work with various usb-serial adapters at the same time (array of adapters).
 

micro

Well-Known Member
Licensed User
Longtime User
Strange
I continue to have the same problems also on a asus transformer and onda 5 inch tablet.
Always request permission to powerup the tablet to attach the converter to the program, while after the first permit if I close and reopen the application does not ask me more permission.
can be solved?
Thanks
 

walterf25

Expert
Licensed User
Longtime User
Hello all, i was wondering if someone can shed some light on a problem i have, this is the first time using this library, i'm actually working on a project for a client which requires usb to serial communication, the issue i'm having is that i can send data just fine from the phone to a serial terminal, but the receiving part does not work all the time, i have checked all my connections and made sure i have reliable connection physically speaking.
I don't understand why this would not work, i've also tried an app from google play https://play.google.com/store/apps/details?id=slickdevlabs.apps.usbtoserialterm and i still the same problem where i can only send data from that app to a serial terminal but can not receive, i've tried different baud rates without any luck.

I was wondering if anyone else has seen this issue, or is it just me?

Please any one with any information or advice about how to solve this problem will be greatly appreciated.

Thanks,
Walter
 
Top