Android Question USB HID Terminal written in Java

ahmed-1a

Member
Licensed User
Longtime User
Hi everybody,

I found an app which is USB HID Terminal, after contacting with its developer, fortunately it is an open source app written in Java, it works fine with any HID device, I usually make HID devices using MikroC and communicate with them by Visual Basic.net or C#.

I've been trying hard to get USB library in basic4android doing the same, but I couldn't.

I don't know if there is a possibility to convert the code to B4A and get the app work.
 

Lars Andersson

Member
Licensed User
Longtime User
Something you could share with me? Have my pcb with the same chip just in front of me ...
 
Upvote 0

ahmed-1a

Member
Licensed User
Longtime User
Sure
To be honest, I've stopped working on it for a month I guess, my laptop isn't near at the moment, I'm replaying on my mobile phone.
But I'll share the idea and the code
It's really easy, basic and uncomplicated.
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
Ahmed,
I don't want to push you and I understand if you don't want to publish your code...... I hope you have just forgot it or haven't had time;)
 
Upvote 0

ahmed-1a

Member
Licensed User
Longtime User
I'm very sorry lars, studying :(:(:(:(:(:( is the reason

as I said it's very easy, this is when I was trying to receive data, I haven't done the sending but after having a look at the code, you will figure out very quickly

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 VID As Int : VID = 4660
  Dim PID As Int : PID = 1
 
  Dim RxPlcBuffer(64) As Byte
  Dim TxPlcBuffer(64) As Byte
 
  Dim Timer1 As Timer
End Sub
Sub Globals

    Dim btnConnect As Button
    Dim btnDisconnect As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Timer1.Initialize("Timer1", 1000) ' 1000 = 1 second
  Timer1.Enabled = True
    If FirstTime Then
        manager.Initialize
     

    End If
    Activity.LoadLayout("1")
End Sub
Sub Activity_Pause(UserClosed As Boolean)
 
End Sub

Sub btnDisconnect_Click
  If HIDstreams.IsInitialized Then
    Log("Stopped")
  End If
End Sub
Sub btnConnect_Click
    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)
      Log(ud)
    'Log(Bit.ToHexString(ud.VendorId))
    Log(ud.InterfaceCount)
      '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
        'Log(Bit.ToHexString(ud.VendorId))
        'Log(Bit.ToHexString(ud.ProductId))
            '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
                    Log(inEndpoint.MaxPacketSize)
                  Else If endpoint.Direction = manager.USB_DIR_OUT Then
                    outEndpoint = endpoint
                    Log(outEndpoint.MaxPacketSize)
                  End If
              End If
            Next
        End If
      Next
    Next
    manager.RequestPermission(device)
    If manager.HasPermission(device) Then
     
        HIDstreams = manager.OpenDevice(device, interface, True)
    Log("Connected")
 
    End If

End Sub
Sub Timer1_Tick
 
 
    If ( HIDstreams.IsInitialized = True ) Then
     
        'Log("010")
        Log( HIDstreams.BulkTransfer(inEndpoint,RxPlcBuffer,64,300))
            'Log ("Msg Received")
        'End If
     
     
    End If
 
End Sub
 
Upvote 0

ahmed-1a

Member
Licensed User
Longtime User
the most important thing is in the timer tick event....

if RxPlcBuffer contains data, that's mean you received some :)....

if you have any questions, let me know, and sorry again for late reply...
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
Ahmed,

Your sample code doesn't work for me for some reason, guess it's compiler dependent (I'm using Oshonsoft PIC Simulator IDE)
So far I have been able to send data to my MCU (PIC18F4550) via USB but not able to read data yet.
I have attached a "log" from the communication between my PC (VB.NET app that works greate) and the MCU if someone have a suggestion so please.....

PS. "USBConnect.Connection.ControlTransfer(0x21, 0x09, 0x0200, 0x0000, USBConnect.TxPlcBuffer, 8, 100)" works fine to send data

Lars
 

Attachments

  • Communication.PNG
    Communication.PNG
    33.8 KB · Views: 502
Upvote 0

ahmed-1a

Member
Licensed User
Longtime User
I don't think I'm quite sure that I understand you :rolleyes:

but, let's start from the scratch:

1- I'm using mikroC to make a HID usb device, note that it is HID, with reading and writing buffer of 64 bytes. ( you also have to consider VID and PID numbers, it must be the same in the pic code and the android app)
2- You should build the circuit and try it with a computer either by any HID terminal or vb.net app.
3- when you are sure that it works, then try to connect it with your android phone.
4- first let your android app try to read from the pic, and make sure it works, then move to sending to the pic from the android app.

notice this code:

B4X:
Sub Timer1_Tick
    If ( HIDstreams.IsInitialized = True ) Then
        Log( HIDstreams.BulkTransfer(inEndpoint,RxPlcBuffer,64,300))
    End If
End Sub

This code will not show the data in the log window, but it just indicates that some bytes have been received, while the received data is in the RxPlcBuffer, this code is repeated every 1 second, so should modify it if some data is received.
for example:
if you make the pic send a 64 byte to the android when you press a button, it will show that some bytes have been received in the log window. because the code in a timer, the code will be repeated again, but the result is different, in the log there will be 0 byte received unless u push the button again.

does that make sense ?
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
Will try to clarify....

The MCU compiler I'm using clearly say "Data exchange is implemented with USB Feature, Input and Output reports with 8 bytes of data" and the command I mentioned earlier to send data is 8 byte and works, I assume then that ".bulktrasfer" or ".controltransfer" should be 8 bytes for receive data as well but as I clearly do something wrong here I'm open for suggestion.

My "setup":
18F4550 with LCD showing Data in and Data out
PC (win8.1) with VB.NET app for sending and receiving data (USB port monitor running in the background to capture the communication)

The PC App communicates perfectly fine in both direction (both PC and the LCD shows exactly the same data)

I'm open for all suggestion so please don't hesitate to throw them on me ;-)

Regards
Lars
 
Upvote 0

Lars Andersson

Member
Licensed User
Longtime User
I have manage to solve my issue.... I was very simple so I'm embarrassed to say what it was....
The time the PIC needs to send the received data to LCD was longer than I expected so no real problem with my app. (App tried to "grab" the data from the MCU before it was sent)

Lars
 
Upvote 0
Top