USB connection fails

pecook

Member
Licensed User
Longtime User
Hello all,

I spent whole day yesterday with USB connection example using Erel's ADB code. I managed to run my device via HID profile. Android is sending data via routine SendDataToPlc every 100ms and device is flashing accordingly. All works fine, but after hour or more I get : java.lang.RuntimeException: Error queuing request .

I don't know what to do with it. Its my first app on Android and its driving my crazy. Can please anyone help me ?

Code for New_Data callback:

B4X:
Sub Connection_NewData (Request As UsbRequest, InDirection As Boolean)

   If plc.HIDstreams.IsInitialized = False Then 
      Return 
   End If
   
   ' don't handle OUT requests - False
   If InDirection = False Then 
      plc.ReleaseRequest(Request, plc.OutRequests)
      plc.HIDstreams.ContinueListening
      Return 
   End If
   
   ' and handle INP requests
   If Request.Name = plc.DATA_READ Then
      plc.SendInRequest(plc.DATA_READ, 64)
      
      Log("RX : " & plc.RxPlcBuffer(0) & plc.RxPlcBuffer(63))
      
   End If
   
   plc.ReleaseRequest(Request, plc.InRequests)
   plc.HIDstreams.ContinueListening
   plc.RefreshPlcData
End Sub

and rest of the code in plc block :

B4X:
Sub Process_Globals
   
   Dim manager As UsbManager
   Dim HIDstreams As UsbDeviceConnection
   Dim outEndpoint, inEndpoint As UsbEndpoint
   Dim device As UsbDevice
   Dim interface As UsbInterface

   Dim MSG_READ, DATA_READ As String
   MSG_READ = "Msg-Read" : DATA_READ = "Data-Read"
   
   Dim InRequests, OutRequests As List
   
   Dim VID As Int : VID = 1240
   Dim PID As Int : PID = 63
   
   Dim RxPlcBuffer(64) As Byte
   Dim TxPlcBuffer(64) As Byte
      
End Sub

Sub PlcDisconnect
   If HIDstreams.IsInitialized Then 
      HIDstreams.StopListening
      OutRequests.Clear
      InRequests.Clear
   End If
End Sub

Sub FindAdbDevice As Boolean

   Dim usbdevices() As UsbDevice
   usbdevices = manager.GetDevices
   
   'Iterate over devices and find the correct one
   For i = 0 To usbdevices.Length - 1
   
      Dim ud As UsbDevice
      ud = usbdevices(i)
      
      'Iterate over interfaces
      For a = 0 To ud.InterfaceCount - 1
         Dim inter As UsbInterface
         inter = ud.GetInterface(a)
         
         If ud.VendorId = VID AND ud.ProductId = PID Then
            'found our device and interface
            device = ud
            interface = inter
               
            'Find correct endpoints
            For b = 0 To interface.EndpointCount - 1
               Dim endpoint As UsbEndpoint
               
               endpoint = interface.GetEndpoint(b)
               
               ' And claim it   
               If endpoint.Type = manager.USB_ENDPOINT_XFER_INT Then
                  If endpoint.Direction = manager.USB_DIR_IN Then 
                     inEndpoint = endpoint
                  Else If endpoint.Direction = manager.USB_DIR_OUT Then
                     outEndpoint = endpoint
                  End If
               End If
            Next
         End If
      Next
    Next
End Sub

Sub CheckPermission
   If device.IsInitialized Then 
      If manager.HasPermission(device) = False Then 
         manager.RequestPermission(device)
      Else
         'There is permission. Connect if neccessary.
         If HIDstreams.IsInitialized = False Then
            MakeConnection
         End If
      End If
   End If
End Sub

Sub MakeConnection
   HIDstreams = manager.OpenDevice(device, interface, True)
   HIDstreams.StartListening("connection")
End Sub

Sub SendOutRequest(Name As String, Data() As Byte, Length As Int)
   Dim request As UsbRequest   
   request = GetRequest(True)
   request.Name = Name
   request.Queue(Data, Length)
End Sub

Sub SendInRequest(Name As String, Length As Int)
   Dim request As UsbRequest   
   request = GetRequest(False)
   request.Name = Name
   request.Queue(RxPlcBuffer, Length)
End Sub

Sub ReleaseRequest(Request As UsbRequest, RequestList As List)
   RequestList.Add(Request)
End Sub

Sub GetRequest (Out As Boolean) As UsbRequest
   Dim r As UsbRequest
   Dim RequestList As List
   
   If Out Then RequestList = OutRequests Else RequestList = InRequests
   
   If RequestList.Size = 0 Then
      If Out Then
         r.Initialize(HIDstreams, outEndpoint)
      Else
         r.Initialize(HIDstreams, inEndpoint)
      End If
   Else
      r = RequestList.Get(0)
      RequestList.RemoveAt(0)
   End If
   Return r
End Sub

Sub SendDataToPlc
   If HIDstreams.IsInitialized = True Then
      ' Send test buffer 
      TxPlcBuffer(0) = 0x80
      SendOutRequest("Data", TxPlcBuffer, 64)
   End If   
End Sub
 

pecook

Member
Licensed User
Longtime User
Will do, but I have updated SDK to API15 and also new Google USB drivers. Running test now and see what will happen. Maybe this will solve issue ?
 
Upvote 0

pecook

Member
Licensed User
Longtime User
Ok, just to follow up, because it was random problem with queue exception I have changed USB cable and problem has disappeared. USB HID profile together with USBserial driver works nice.
 
Upvote 0
Top