B4A Library UsbSerial library 2.0 - supports more devices

jjspike

Member
Licensed User
Longtime User
I tried v2.4 of USBSerial. Port1 indicated a Qualcomm and port 2 indicated an FTD device. Debug messages stated error opening port 1 and 2. I ran the demo as is therefore everything should be in place.

Thank you!
 

pobss

Member
Licensed User
Longtime User
Hello everyone, I run the example library v2.4 in an Android 4.4 device connected with Prolific 2303, wireless mouse and keyboard, usb to ethernet adapter.
the example works and I can send and receive data from my microcontroller.
the problem is that often the ethernet adapter and the wireless mouse are disconnected so I no longer have control of the device.
does anyone know how I can fix this?
thank you
 

raphaelcno

Active Member
Licensed User
Longtime User
I don't think that you can fix it as the problem is in the internal USB host which is managed by the OS.
Is there an event that can be used to detect a change in the number of USB devices? With such an event it could be possible to close the USB Serial connection and open it again with the new device/port number.
 

raphaelcno

Active Member
Licensed User
Longtime User
Maybe you can try to program your microcontroller to send a simple message to your Android device every second, and your Android app checks when this message arrives. If the app doesn't receive any message for 2-3 seconds, it closes the USB serial connection and opens it again with the new device number.
 

pobss

Member
Licensed User
Longtime User

thanks for the answers, when I send a command to the micro I open the connection, sending the command and out. in this way I never problems.
But I need the app to remain even listening to receive data from the micro.
then I open the connection timer wait 1 second to receive data and out.
this is repeated every 30 seconds.
this solution seems to work for the moment.
the device I use is a dongle tv Measy U2A.
 

Bob Sabrook

Member
Licensed User
Longtime User
Hello folks, I am successfully using UsbSerial 2.4 with an STM32 USB port using the usb type DRIVER_CDCACM.
However I am occasionally loosing ASCII space characters (0x20) from my received data (UTF8). Can anyone suggest how I may fix this?
Problem does NOT occur when interfacing with an FTDI type device. It seems like a speed issue but why only spaces?
Also problem does not happen when talking to same device from a PC/Windows, which suggests a framing problem.
 
Last edited:

Bob Sabrook

Member
Licensed User
Longtime User
How are you collecting the data? You should assume that messages will be split into multiple "packets".

like this;

B4X:
Sub do_Astreams_Newdata(Buffer() As Byte,  index As Int) 'index is 1 based port 
    Dim p As port_t = port(index-1) 'p.str will contain received string. p.sent set when string requested
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    p.str = p.str & s                           '<============accumulate string
    If p.sent AND p.str.EndsWith(Chr(10)) Then  'when terminator seen
      Label1.Text = p.str         'copy to label
      p.str=""                    'clear cumulative string now used
      p.sent = False              'clear flag that got us here
    End If
End Sub
index is the port in use 1 or 2.
 
Last edited:

Bob Sabrook

Member
Licensed User
Longtime User
Yes, it is wrong (at least in the general case where two messages can be merged).

You can use AsyncStreamsText class: https://www.b4x.com/android/forum/threads/27002/#content

Thank you Erel. I see what you mean now, but I am not using the general case where messages can be merged. I send a command and await a 6kbyte reply ending in linefeed, then silence - until I send another command. I have studied your class and can see it assumes data is a text file and feeds the consumer with whole lines when available. Nevertheless your use of stringbuilder to append new data is interesting and its efficiency might even solve my loss of data.

Occasional Data loss seems to occur after about 600 bytes of 6k bytes and it is not only spaces lost. I could request smaller amounts of data, but that may only reduce the probability of data loss , add complication, and not address the cause of the problem.
 

Bob Sabrook

Member
Licensed User
Longtime User
Experimental observations;
* Adding a delay of 2 milliseconds outside the 'if' statement in the new_data handler above gives near 100% reliability (all 6171 bytes are received in 4 to 10 seconds) - this is only an observation - not a fix.
 
Last edited:

jahswant

Well-Known Member
Licensed User
Longtime User
All my attempts to use this lib are unsucessful both versions still unable to open the usb port...
 

jahswant

Well-Known Member
Licensed User
Longtime User
Yep i used it detects very well my USB Printer but fails to connect to it ...
 

Stevanus

New Member
Licensed User
Longtime User
Hi Erel, is there anyway to connect to USB Device (act as USB Host) but still can do charging for the android device ?
i have project to connect Android Device with serial device, first i plan to use wiznet and wifi, but after b4a launch this usbserial lib, i change my plan, but new problem occur, i cannot charge my android device
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…