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,011
  • UsbSerial2.5.zip
    36.3 KB · Views: 644
Last edited:
D

Deleted member 103

Guest
Then is the problem on the device and not on the Android version?
 

CPS

New Member
Licensed User
Longtime User
Hi - great library, thanks.

Attached is a proposal (and all the java src required) for adding RS232 'break' function to the library - this is really actually a Android USB host serial driver library addition for Mike but as it would be great to get it merged into this library as well, so I thought I'd post it here for Andrew at the same time. It's a bit long winded so attached as a text file.

Thanks, Clive
 

Attachments

  • basicforandroid.txt
    8.7 KB · Views: 257

nosaj66au

Member
Licensed User
Longtime User
Hi - agree great libary!!!
can anyone confirm if the libary supports parity detection, it appears to support parity set already. I have an older bus protocol which can be read / written to via a USB RS232 port and I have writen a windows driver for it in the past and I look to port to Android. The requirment is to detect Char's with parity of either Mark , Space or None as these are used to indicate the Start, End and Data within a message packet.

Thanks in advance
Jason
 

nosaj66au

Member
Licensed User
Longtime User
Thanks for your fast responses Agraham, however the protocol is already defined and is 8 data bits + parity. I have written a drivers for both AVR micros and windows and have basically had to bit bang or extract from raw uart data. Anybody have any suggestions.

Jason
 

RichardHirst

Member
Licensed User
Longtime User
HI.

I have a FTDI 2232D, this is a dual port device.

The info returns: B4aInterfaceNumber: 0 and B4aInterfaceNumber: 1

If there a method to select which interface is use..?

Thanks

RicHard
 

RichardHirst

Member
Licensed User
Longtime User
No.. I get - "Error opening USB port"

The info returns all the correct endpoints etc for both interface numbers.

The demo code works ok when you attached a single FTDI device.

Richard.
 

agraham

Expert
Licensed User
Longtime User
HI.
If there a method to select which interface is use..?
Unfortunately no, it uses the first it finds. However the source code is available in the posted archive so if you are set up to make libraries you could try modifying to suit your needs.
 

biometrics

Active Member
Licensed User
Longtime User
Hi,

An app that uses the USB Serial library suddenly stopped compiling for some reason and I can't figure out why ... can anyone help?

I have this in the manifest as before:

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" />)


But the compiler now complains with this error message:


B4X:
Parsing code.  0.05
Compiling code.  0.06
Compiling layouts code.  0.02
Generating R file.  Error
AndroidManifest.xml:43: error: Error: No resource found that matches the given name (at 'resource' with value '@xml/device_filter').
 

biometrics

Active Member
Licensed User
Longtime User
Above post solved ... for some unknown reason this file went missing!

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.
 

TR Shearer

New Member
Licensed User
Longtime User
I'm trying to run the UsbSerial demo on a rooted Nook Simple Touch. Eventually I want to read NMEA (GPS) data via a USB2Serial adaptor and display the info on the daylight-readable e-ink screen. I have an app that can do this (TopHat Soaring) so I know the hardware is capable of it but the demo keeps crashing at the first library call - see the log below.

The ReadMe for the library says the JAR and XML files should go in the "Additional Libraries" folder - where is this? I've put them in "C:\Program Files\Anywhere Software\Basic4android\Libraries" and the demo compiles correctly.

Any ideas?

Thanks,
Bob

The log says...

main_btnopen_click (B4A line: 41)
If usb.UsbPresent = usb.USB_NONE Then
java.lang.NoClassDefFoundError: android.hardware.usb.UsbManager
at anywheresoftware.b4a.objects.UsbSerial.UsbPresent(UsbSerial.java:345)
at b4a.example1.main._btnopen_click(main.java:438)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:63)
at android.view.View.performClick(View.java:2373)
at android.view.View.onTouchEvent(View.java:4197)
at android.widget.TextView.onTouchEvent(TextView.java:6618)
at android.view.View.dispatchTouchEvent(View.java:3727)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:904)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:904)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:904)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:904)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1105)
at android.app.Activity.dispatchTouchEvent(Activity.java:2078)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
 

agraham

Expert
Licensed User
Longtime User
Your device seems to be missing the UsbManager class which is used by UsbSerial2. This was added to Android in version 3.1. As the Nook seems to be based on Android 2.1 this would explain why it is not present.

Define your additional libraries folder by Tools -> Configure Paths
 

christian91

New Member
Licensed User
Longtime User
Hi
I have custom interface FT232R with my own PID C7F0.

in the USBSerialDemo2.3, i add
usb.SetCustomDevice(usb.DRIVER_FTDI, 0x0403, 0xC7F0)
before
usb.HasPermission
I can read usb.DeviceInfo
but usb.open() return usb.USB_NONE

Set with Mprog FDTI :
USB VID=0403, USB PID=C7F0
Manufacturer=SCAN, Product=USB ScanST
Bus Powered=1, Max Bus Power=100
High Current I/O=1, Load D2XX Driver=1

Any help?
Thx, Christian
 

Opa

New Member
Licensed User
Longtime User
I switched from FT232RL to CP2102 and I am no longer having this workstopping error ((IOException) java.io.IOException: Expected at least 2 bytes) !
Too bad I cannot say the same when I am using the FTDI chip. I am producing devices which are equipped with FTDI chips and I won't be able to use them with the Android app that I am creating, at least the sold ones will not work with the app. And this is too bad, because I want to impress/be helpful/give more flexibility/ with this app to my current clients (people who already bought devices) as well as the future ones :( :( Would you revise your thoughts about my problem?
Have you ever come across with this error before? ((IOException) java.io.IOException: Expected at least 2 bytes) what could be the reason of it? I found on Google nothing about this, except on your forum (), but it does not help me at all.
I hope you can help me.

Communicating with data coming from a PIC18F252 microcontroller and an FTDI 232 usb to serial converter at 115.2 K, I had the
same annoying error over and over again ( IOException java.io.IOException: expected at least 2 bytes ).
However in my case the solution was easy by using a greater timeout. So after the line
astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
I putted
usb.UsbTimeout=1000 ' default was 200
Hope this helps for you also, if you didn't find this solution already in the meantime ;-)
 
Top