UsbSerial library - Supports FTDI, Arduinos and other USB serial devices

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
Version 2.0 of this library is available here: http://www.basic4ppc.com/forum/addi...serial-library-2-0-supports-more-devices.html

This library is a wrapper for this open source project: usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
It requires Android 3.1+.

This library together with the wrapper create a simple interface for working with USB serial devices.

Once the USB is opened all the communication is done with AsyncStreams.
Note that prefix mode is not supported.

A simple program that prints the input to the logs and sends a string (the program is attached):
B4X:
Sub Process_Globals
   Dim usb As UsbSerial
   Dim astreams As AsyncStreams
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
End Sub

Sub btnOpen_Click
   If usb.Open(9600) Then
      Log("Connected successfully!")
      astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
   Else
      Log("Error opening USB port")
   End If
End Sub

Sub Astreams_NewData (Buffer() As Byte)
   Log("NewData")
   Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub btnClose_Click
   astreams.Close
End Sub

Sub btnSend_Click
   astreams.Write("abcde".GetBytes("UTF8"))
End Sub

Sub AStreams_Error
   Log("Error: " & LastException)
   astreams.Close
End Sub
Sub Astreams_Terminated
   Log("Terminated")
   astreams.Close
End Sub


Library setup instructions:
1. Download usb-serial-for-android-v010.jar: Downloads - usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
2. Copy this file and the attached library (extract it first) to the libraries folder.

Application setup instructions:
The user has to give your application permission to access the USB device before it can be opened.

1. 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" />)
2. Copy device_filter.xml from the attached example to: <your project>\objects\res\xml and mark it as read-only.

These two steps were already done for the example project.

3. Install the program and attach the USB device. A dialog will appear asking whether you want to start your program.
From now on the USB device will work with your program.

Tips:
1. You can use this open source Windows terminal: Download - Tera Term - SourceForge.JP
2. The example project UI is minimal. Check the logs for the input.
3. Check the unfiltered logs if there are problems opening the USB device. There might be an error message there.
4. Use B4A-Bridge to connect to the Android device.
 

Attachments

nwhitfield

Active Member
Licensed User
Thank you, thank you, thank you!

This is perfect; I have an app that controls a piece of kit so far either via TCP/IP to a desktop computer that drives the unit via serial port, or via a bluetooth to serial adaptor, and this is brilliant.

It took me a only a few minutes to add to my code (and most of that was tidying up the choices to allow for three connection methods instead of two) and now I can directly control the device from the Android.
 

barx

Well-Known Member
Licensed User
Looks like someone has been busy doing some post Holiday season 'wrapping'. Well done Erel. Could come in usefull for my arduino. Had it 12 months + and it hasn't seen light of day. lol
 

Toley

Active Member
Licensed User
Thank you very much Erel. It works very well with an FTDI device. I did not try with an Arduino but this will be my next step.
This is a very great addon to your fantastic software :sign0098:

Update : Arduino Uno also working :)
 
Last edited:

corwin42

Expert
Licensed User
Hmm, seems I have to buy an USB OTG cable for my Galaxy Nexus. I have a cycling computer which has an FTDI interface.
 

wexican

Member
Licensed User
Hi Erel,
Firstly, very well done I know there is a lot of us that will interface all kinds of gadgets to Androids & B4A now with the help of this library!
A quick question, what usb chips are supported? eg FTDI FT232, FT245, Prolific pl2302, pl2303, also is the ATmega on R3 arduinos supported?

Thanks Tom.:sign0098:
 

bluedude

Well-Known Member
Licensed User
Can this replace the USB man for Android accessory stuff?

I have used the USB man for a while but the user experience wasn't that great (multiple confirmations needed). Do you think this works with the ADK Arduino boards and an Android phone?

I guess it will but the sample code does not talk about permissions needed etc.

Cheers,
 

Erel

Administrator
Staff member
Licensed User
The permission dialog will appear once, on the first time that the USB is attached.
 

bluedude

Well-Known Member
Licensed User
In some old sample code for the USB Host stuff there was a lot you needed to do for permissions, that is all gone? So this can work with Android ADK?
 

marco.canta

Active Member
Licensed User
I have a problem, I can not wait to use the library but I have always an error in the manifest.

error in line "android: resource = "@ xml / device_filter" />)"

I read the previous post but I have not solved.
Can you help me?

Thank you!
Marco
 

Erel

Administrator
Staff member
Licensed User
Are you trying to run the attached example?

Which error do you get?
 

synasir

Member
Licensed User
Hi Erel,

I do not understand this:

Library setup instructions:
1. Download usb-serial-for-android-v010.jar: Downloads - usb-serial-for-android - Android USB host serial driver library for CDC, FTDI, Arduino and other devices. - Google Project Hosting
2. Copy this file and the attached library (extract it first) to the libraries folder.

- Do I copy usb-serial-for-android-v010.jar along with the files in the USBSerial.zip to the B4A library folder?


1. You can use this open source Windows terminal: Download - Tera Term - SourceForge.JP

- Is Tera Term used to test the PC to the USB device connection or Android to USB device connection?

Thanks!
-
 

Erel

Administrator
Staff member
Licensed User
- Do I copy usb-serial-for-android-v010.jar along with the files in the USBSerial.zip to the B4A library folder?
Yes.

- Is Tera Term used to test the PC to the USB device connection or Android to USB device connection?
This is a just a PC tool that you can use if you connect the Android serial to the PC serial port.
 

wexican

Member
Licensed User
Hi Erel,

I've tested the library with the example code on a Arnova(Android 4.0.3) & Asus(Android 4.1.1) tablets & it works very well, as far as I can see the baud rate is 9600(as set when opening the port) & also it has 1 start bit, 1 stop bit & 8 data bits. I can't find any information from the google site stating what the parameters are other than the baud rate setting. Will it be possible in a future version to be able to set start,stop & data bits also? This would be very handy to cater for all serial devices out there!

Thanks, Tom
 

Erel

Administrator
Staff member
Licensed User
@wexican, I will add it if / when they update the native library.
 

Toley

Active Member
Licensed User
Will it be possible in a future version to be able to set start,stop & data bits also?
Since the library is made to communicate with FTDI/Arduino (which are always 8N1) I don't see the use of changing those settings.
This would be very handy to cater for all serial devices out there!
I have try with other devices, by adding the PID/VID codes to the XML file, and they are not recognized by the library.
 
Status
Not open for further replies.
Top