B4A Library UsbSerial library 2.0 - supports more devices

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

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

pxpto

Member
Licensed User
Longtime User

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 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
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…