B4A Library UsbSerial library 2.0 - supports more devices

walterf25

Expert
Licensed User
Longtime User
Hi all, is there a way to detect when the usb device has been disconnected physically from the device, I don't see an event that would be raised if this happened, has anyone had any success with this?

Am I missing something?

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Hi Erel thanks for the reply, i got it working actually i used the BroadCast library for this, now i have another problem

below are the subs where i detect when the usb cable has been attached or detached from the device

B4X:
Sub usbdisconnect_OnReceive (Action As String, i As Object)
  Dim i2 As Intent
  i2 = i

  Log("action " & Action)

  usb.Close
  astreams.Close
  SERIAL_CONNECTED = "Desconectado"
  CallSubDelayed3(Main, "updatescreen", SERIAL_CONNECTED, SOCKET_CONNECTED)

End Sub

Sub usbconnect_OnReceive (Action1 As String, i2 As Object)
  Dim i3 As Intent
  i3 = i2
  Log("action " & Action1)
  Start_Serial     'if cable is re-attached, start usb connection again.
End Sub

They work perfect, the problem now is that if i unplug the cable and reconnect it back on, even though i start the same process for opening the port again, it won't work, any ideas why this might be, the code where i open the open is below.

B4X:
Sub Start_Serial As Boolean
'Start_Serial=False
        If usb.UsbPresent = usb.USB_NONE Then
        Log("Msgbox - no device")
        'Msgbox("No USB device or accessory detected!", "Error")
        'Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission")
    If (usb.HasPermission) Then
    '    Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(38400)  
        If dev <> usb.USB_NONE Then
            Log("Connected successfully!")
            SERIAL_CONNECTED = "Conectado"
            astreams.Initialize(Me, "astreams", usb.GetInputStream, usb.GetOutputStream)
            'usbconnection.Enabled = True
        Else
            Log("Error opening USB port")
            SERIAL_CONNECTED = "Desconectado"
        End If
    Else
        usb.RequestPermission
    End If
  
    If astreams.IsInitialized Then
    astreams.Write("Welcome To USB Receiver" & Chr(10) & Chr(13))
    astreams.Write("By Carlos" & Chr(10) & Chr(13))
    End If
CallSubDelayed3(Main, "updatescreen", SERIAL_CONNECTED, "")
End Sub

Hope you have any advice on this!

thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Do you get any error message?
I don't see any error Erel, it seems that when you re-connect the usb cable a window pops up with the available apps that can be opened using the usb cable, once you click on the app, it seems that it opens up the app again and it works after i click the start button.

I wonder if there's a way to avoid from that window popping up and just re-open the port right away.

Thanks,
Walter
 

shmulik taiber

Member
Licensed User
Longtime User
Hello Erel .
I got the arduino to communicate with nexus 7 through usbserial . however, the info sometimes gets broken in half - for example the number 42 i get as 4 and afterwards 2 . any advise ? also, how can i send something bigger then a byte ?
 

JeanLC

Member
Licensed User
Longtime User
Hello,

Does anyone know how can I open two USB Serial ports using the UsbSerial library?
Does the UsbSerial library supports opening two of them? Apparently it gets just the 1st one.

Everything works great if I only use one of them. But if I want 2? Or 3? Or the same brand 2 FTDIs?

I have two USB Serial Ports No.1-Prolific and No.2-FTDI connected via USB HUB to an Android tablet.
I tried using usb1.SetCustomDevice forcing the Prolific and usb2.SetCustomDevice for the FTDI without luck. (with their VID and PID)
It always gets the same one, the 1st I connected.

Is there a way to do it?

Thanks.
 

JeanLC

Member
Licensed User
Longtime User
Thanks Erel
So sad it doesn't support it.
I would take a look into the library code... thanks again.
 

JeanLC

Member
Licensed User
Longtime User
Hello Erel,

I modified the Library for my use and it's working with 2 USB Serial devices. Should I upload the changes here so anyone can use it with two or more USB devices? Or contact Agraham?

So far it's working Ok, but I don't know if any bugs are hidding.
The calls look like this:
For Device 1: UsbPresent(1) ,HasPermission(1) , DeviceInfo(1), Open(9600, 1) , RequestPermission(1)
For Device 2: UsbPresent(2), HasPermission(2), DeviceInfo(2), Open(9600, 2), RequestPermission(2)
For Device n: UsbPresent(n), HasPermission(n), DeviceInfo(n), Open(9600, n), RequestPermission(n)

The catch is that I have to analyze the incoming data, check DeviceInfo(n) or something else to know what is connected to the USB because depending on the order you plug the USBs it's the order that shows up.
 

JeanLC

Member
Licensed User
Longtime User
UsbSerial Ver 2.4
What's new: Support for multiple USB devices
Version history Updated. You can find the changes by searching "Ver_2.4"
Maybe I over documented it. It was for me... feel free to remove the Nonsense
Library and Example updated to Ver 2.4
hoho driver also updated to support multiple devices
 

Attachments

  • USB_Serial_2.4.zip
    100.1 KB · Views: 2,414

Jaime Ramirez

Member
Licensed User
Longtime User
I am using the version 2.3 I am getting a No USB device or accessory detected error, I do have a FT232RL device attached to the phone, any ideas on what can be missing?
 

Jaime Ramirez

Member
Licensed User
Longtime User
@JeanLC thank you. I've added a link to your update in the first post.

@Jaime Ramirez if your device is rooted then try these steps: http://www.b4x.com/android/forum/threads/pl2303-usb-to-rs232-adaptor-driver.11449/page-3#post-121125
Erel:

My device is rooted, i used a tool name "USB Host Check" and that tools does those
@JeanLC thank you. I've added a link to your update in the first post.

@Jaime Ramirez if your device is rooted then try these steps: http://www.b4x.com/android/forum/threads/pl2303-usb-to-rs232-adaptor-driver.11449/page-3#post-121125
Erel:

My device is rooted, I used a tool named "USB Host Check", that tool creates and modifies the required files. I checked the files for those modifications to be there, then rebooted but still not detecting my FTDI RS232R device. I am using a small OTG cable to connect directly the phone to the FTDI device, who is supplying the power?..Do I have to use a powered USB hub maybe?
 

Jaime Ramirez

Member
Licensed User
Longtime User
The Android device is supplying the power. It is worth testing it with a powered hub. I encountered the same issue in the past: http://www.b4x.com/android/forum/threads/android-usb-host-tutorial-adbtest.11289/#content
Erel:

Is there a way to easily enable the USB Host mode?, I am developing this app for a custom DSP board, but if the user has to root the device then enable the USB host mode this can be discouraging for the user as it may be perceived as a risky procedure....
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…