B4A Library UsbSerial library 2.0 - supports more devices

Discussion in 'Additional libraries, classes and official updates' started by agraham, Apr 9, 2013.

  1. walterf25

    walterf25 Well-Known Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. walterf25

    walterf25 Well-Known Member Licensed 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

    Code:
    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.

    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Do you get any error message?
     
  5. walterf25

    walterf25 Well-Known Member Licensed User

    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
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    I guess that there are several apps on your device that have registered to this intent.
     
  7. shmulik taiber

    shmulik taiber Member Licensed 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 ?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    This is how network communication works. Messages can be split or merged.

    See AsyncStreams tutorial and AsyncStreamsText class.

    You can send arrays with AStreams.Write.
     
  9. JeanLC

    JeanLC Member Licensed 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? :D

    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.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Currently the UsbSerial library picks the first recognized usb device.
     
  11. JeanLC

    JeanLC Member Licensed User

    Thanks Erel
    So sad it doesn't support it.
    I would take a look into the library code... thanks again.
     
  12. JeanLC

    JeanLC Member Licensed 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.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Please upload it. Thank you :)
     
  14. JeanLC

    JeanLC Member Licensed 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
     

    Attached Files:

  15. Jaime Ramirez

    Jaime Ramirez Member Licensed 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?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

  17. Jaime Ramirez

    Jaime Ramirez Member Licensed User

    Erel:

    My device is rooted, i used a tool name "USB Host Check" and that tools does those
    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?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

  19. Jaime Ramirez

    Jaime Ramirez Member Licensed User

    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....
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    On many devices it is enabled by default. If the manufacturer has disabled it then the only option is to root the device.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice