USB HID Class

wexican

Member
Licensed User
Longtime User
Hi All,

I'm not too long using B4A but I must congratulate Erel & others for their fantastic work, B4A is the best I've seen in long long time, well done!

To my question:
I have a USB based weather station of type WH1081PC that normally connects to a pc via usb & sends data regularly up to wunderground.com. I want to write my own app to use on a small android media player to do the same without the hassles of trying to keep a windows pc running. I'm currently testing software on a Arnova 10bG3 running ICS 4.0.3 & have got it to send data to wundergound using the HTTP libs. The next stage is to get the weather station to talk to the Arnova using the usb port, I have a descriptor listing all data of the wh1081 usb requirements, I have a list of the commands to the wh1081 & the responses & meanings etc. The wh1081 is a HID device & needs to be polled to receive data back, does anybody have any sample programs or snippets of code to interface a HID type device to B4A? I'm not sure if this is even possible from some of the responses I see in other threads, can somebody please clarify if this is possible.

Thanks,

Tom
 

wexican

Member
Licensed User
Longtime User
Thanks Erel, I'll try out the sample code in the tutorial to see if the weather station is recognised by my tablet & media player.

Cheers!

Tom
 
Upvote 0

wexican

Member
Licensed User
Longtime User
Hi All,

I tested the weather station last night(as suggested by Erel) with the code AGraham(thanks very much) posted in the USB tutorial to test if my device could be detected by my Arnova tablet, to my delight it was detected & the interface descriptor information was returned. Have a look below for what I received:

Device 0 details

Manufacturer: Љ
Product: Љ
Serial: Љ

DeviceName: /dev/bus/usb/001/019
DeviceClass: USB_CLASS_PER_INTERFACE (per-interface basis)
DeviceSubClass: 0
Device ID: 3FBh
ProductID: 8021h
VendorID: 1941h

B4ainterfaceNumber: 0
InterfaceClass: USB_CLASS_COMM (human Interface
InterfaceSubClass: 0
InterfaceProtocol: 0

EndpointNumber: 1
EndpointDirection: In
EndpointType: USB_ENDPOINT_XFER_INT(interrupt)
EndpointAttribute: 3
EndpointInterval: 10
EndpointMaxPacketSize: 8

I checked the PID & VID against what my pc uses when the weather station is plugged into it & I see the exact same information. Now all I have to do is write some software to send the commands to the weather station & receive back the requested data. I haven't done this before & hope somebody here has done something similar & may be willing to share some code.

I found out what commands are sent to to the weather station from a pc by using an excellent app called "USBTrace" & also while running Cumulus on the pc, the commands I saw consist of a 8 byte packet which is made up of 2 * 4 byte instructions:

1st byte is instruction(read or write) this is either 0xA0 or 0xA1 for write.
2nd byte is Address byte high to read from / write to.
3rd byte is Address byte low to read from / write to.
4th byte is always 0x20(terminator).

So the above 4 bytes is sent twice & if a read was requested the weather station will always return 32 bytes of data starting from the address given in the read command, or if writing data....after the write command is issued 32 bytes of data is expected to be received within 500ms & then the weather station will return 0xA5 if all was received ok. For the moment I'm only interested in polling the weather station for 32 bytes of the current data.

I'd really appreciate it if anybody has any idea or suggestions on how to implement the functions for writing a command(as above) & reading data back, all across the USB port & of course in B4A.
If I get this all working I'll gladly share the code with whoever wants it!

Best regards & thanks

Tom
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
It might be worth checking with the manufacturer of the weather station to see an SDK exists - maybe even just a PDF or similar that documents the available commands.

Martin.
 
Upvote 0

wexican

Member
Licensed User
Longtime User
Thanks warwound,

I have a list of the commands & replies etc for the weather station, some others on internet have wrote their own programs under linux to read data back from the weather station. I don't think there is a SDK available for it, I know some others who went looking for it & failed. The data I have is from another who reverse engineered the interface. The commands are short enough eg. sending 0xA1,0x00,0x00,0x20,0xA1,0x00,0x00,0x20(8 bytes in total) will return the first 32 bytes of the memory map of the weather station, I also have a listing of the memory map for the station, it's about 128k bytes in size, I have the information on what's what within this data.

Right now I can get it to enumerate & connect etc but can't get it to send the data read command shown above to it. I know the weather station only has an inEndPoint, the example code I got from the tutorial uses both in & out endpoints & when you use a non existant out endpoint the app throws an exception basically stating "out end point not initialized", & correctly so. So as far as I understand then to send a command to a device like this one must use a ControlTransfer method or command to send the requested command to the device. I'm having trouble at the moment in trying to figure out the parameters for this command.
From the manuals it hould be:

ControlTransfer (RequestType As Int, Request As Int, Value As Int, Index As Int, Buffer() As Byte, Length As Int, Timeout As Int) As Int

Some parameters are obvious what they should be but not sure how to use inparticular "Request As Int", "Value As Int", "Index As Int". Found some infomation on "USB in a Nutshell" listing on "RequestType As Int".

Any ideas would be greatly appreciated.:sign0163:

Thanks,

Tom
 
Upvote 0

wexican

Member
Licensed User
Longtime User
Hi all,

I made some progress lastnight in reading data from the weather station, the code fragment below works! It's far from pretty(a mess really) but was only for test & now that the test works down to the real job at hand! The weather station once conneted will retrieve a total of 8 * 8 byte packets containing the weather station configuration & the last recorded data(current conditions). I know the sleep instruction looks bad to have to use it but it was the easiest way to cause a delay between the data request instructions. One thing I noticed is in the ControlTransfer instruction(Request Type) I had to pass a 0x21 to it to get it to work but while testing under USBTrace on the PC it recorded a value of 0x22 here! Not sure if this is a bug in the USB libraries in Android or B4A. A new version will be coming soon with the data displayed graphically. Any questions or comments on the code below will be most welcome!

Thanks guys for the suggestions/help in getting this working.

Tom.


Sub btnSend_Click
Dim sent As Int
Dim bc As ByteConverter

ListView1.Clear
For a = 0 To 7
RX_Data1(a) = 0
RX_Data2(a) = 0
RX_Data3(a) = 0
RX_Data4(a) = 0
RX_Data5(a) = 0
RX_Data6(a) = 0
RX_Data7(a) = 0
RX_Data8(a) = 0
Next

ListView1.AddSingleLine("Read Config")
TX_Data(0) = 0xA1
TX_Data(1) = 0
TX_Data(2) = 0
TX_Data(3) = 0x20
TX_Data(4) = 0xA1
TX_Data(5) = 0
TX_Data(6) = 0
TX_Data(7) = 0x20
sent = connection.ControlTransfer(0x21, 0x09, 0x0200, 0, TX_Data, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data1, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data2, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data3, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data4, 8, 100)
Sleep(25)
ListView1.AddSingleLine("Reading Weather Data!")
TX_Data(0) = 0xA1
TX_Data(1) = RX_Data4(7)
TX_Data(2) = RX_Data4(6)
TX_Data(3) = 0x20
TX_Data(4) = 0xA1
TX_Data(5) = RX_Data4(7)
TX_Data(6) = RX_Data4(6)
TX_Data(7) = 0x20
sent = connection.ControlTransfer(0x21, 0x09, 0x0200, 0, TX_Data, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data5, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data6, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data7, 8, 100)
Sleep(25)
sent = connection.BulkTransfer(inEndpoint, RX_Data8, 8, 100)
Sleep(25)

ListView1.AddSingleLine(bc.HexFromBytes(RX_Data1))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data2))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data3))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data4))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data5))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data6))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data7))
ListView1.AddSingleLine(bc.HexFromBytes(RX_Data8)) ListView1.AddSingleLine("Weather Data Read Done!")
End Sub
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
I am missing something

I have the same type of weather station as wexican. I have tried the example from agraham to see if it will recognise the weather station on my two different droid tablets (both chinese no names).

Neither seems to want to recognise anything on their USB using the USBdetails example............I have tried the weather station, USB sticks, and a few other random USB devices.

The example compiles OK and transfers to both droids using B4bridge OK. They run, but when I click "Connect" I get the "oops no device attached" error box......

not sure what I am doing wrong? Yes, I have the USB library running (0.96). Any ideas? Or do both these tablets (running 4.0) just not like the USB library to drive their USB ports?? Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Comalco

Member
Licensed User
Longtime User
I'm not sure. He says it's the same weather station that wexican is using and it works for him. Either the tablet hardware doesn't support host mode or he isn't using the correct cable.

I am using a standard micro_SB to USB A cable, which is what I can use using a PC conencted to the weather station. I believe its the tablets.....after reading what has been written. I don't think its rooted (although to me that wording means "I don't think it's broken"!!)

I read the link aabout adding an XML file in the system directory - but I cannot find that on the tablet - so I presume thats what everyone means by not rooted??

Thanks for the advice. So there is nothing I can do to use these tablets with this USB I/O relay pcb??
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Adapter Cable

Are you using and OTG cable to switch the USB port to host mode?

If I use one of the USB A adapter leads that comes with the table (that you can use a USB memory stick with).....it doesnt work either. If thats what you mean ??
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
I am using a standard micro_SB to USB A cable, which is what I can use using a PC conencted to the weather station. I believe its the tablets.....after reading what has been written. I don't think its rooted (although to me that wording means "I don't think it's broken"!!)

I read the link aabout adding an XML file in the system directory - but I cannot find that on the tablet - so I presume thats what everyone means by not rooted??

Thanks for the advice. So there is nothing I can do to use these tablets with this USB I/O relay pcb??

I think I have the same issue with trying to get the weather station and the relay I/O pcbs working.......none of these devices are recognised by either of the tablet devices I have......
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
If I use one of the USB A adapter leads that comes with the table (that you can use a USB memory stick with).....it doesnt work either. If thats what you mean ??
Yes, if a memory stick works with the tablet with that cable then the weather station should as the tablet does seem to have host mode hardware.

Your tablet doesn't need to be rooted to see if /system/etc/permissions/android.hardware.usb.host.xml exists but if it doesn't exist then you would need to be rooted to add it. You can check with ES File Explorer from the Play Store but you need to enable "Up to root" in Menu-> Settings -> Root Settings. This does work even if you are not rooted. ES File Explorer will start at the root of your SD card but pressing the Up button at the top will take you to the file system root where you can see the system folder and follow it down to permissions. No doubt other file explorers would work as well if they allow you to navigate to the file system root.
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
that was it - it works now

Yes, if a memory stick works with the tablet with that cable then the weather station should as the tablet does seem to have host mode hardware.

Your tablet doesn't need to be rooted to see if /system/etc/permissions/android.hardware.usb.host.xml exists but if it doesn't exist then you would need to be rooted to add it. You can check with ES File Explorer from the Play Store but you need to enable "Up to root" in Menu-> Settings -> Root Settings. This does work even if you are not rooted. ES File Explorer will start at the root of your SD card but pressing the Up button at the top will take you to the file system root where you can see the system folder and follow it down to permissions. No doubt other file explorers would work as well if they allow you to navigate to the file system root.

OK, downloaded ES File Explorer......followed these instructions, and my 7in chinese no name tablet now works like a charm with the USB Serial Example program............magic, thanks. Now I'll try the 4.3in baby one....

"Enable USB-Host Controller on any Android
This app only checks usb host on your device.
To enable USB host API support you should add a file named android.hardware.usb.host.xml
and containing the following lines:
<permissions>
<feature name="android.hardware.usb.host">
</feature></permissions>
into folder /system/etc/permissions
in that folder find file named handheld_core_hardware.xml or tablet_core_hardware.xml
and add <feature name="android.hardware.usb.host"> into <permissions> section
Reboot your device. Usb host api should work."

of course for an Android 3.1 and higher.
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Realtek WLAN Adapter?????

OK, downloaded ES File Explorer......followed these instructions, and my 7in chinese no name tablet now works like a charm with the USB Serial Example program............magic, thanks. Now I'll try the 4.3in baby one....

"Enable USB-Host Controller on any Android
This app only checks usb host on your device.
To enable USB host API support you should add a file named android.hardware.usb.host.xml
and containing the following lines:
<permissions>
<feature name="android.hardware.usb.host">
</feature></permissions>
into folder /system/etc/permissions
in that folder find file named handheld_core_hardware.xml or tablet_core_hardware.xml
and add <feature name="android.hardware.usb.host"> into <permissions> section
Reboot your device. Usb host api should work."

of course for an Android 3.1 and higher.

So after all the success of getting the 7in tablet working with the USB relays, the 4.3in wouldn't give under the same permissions files treatment. Interestingly, when I run USB Details, the port returns that it is a Realtek 802.11n WLAN adapter.......even with nothing connected. The manual clearly says its a USB port, as does the labelling of the unit, and they supply a micro USB to Type A lead with the product......is the type of port configured somewhere in a file?? I cannot imaging where, but not sure why the USB port would return that it is a WLAN port........?? Ideas or pointers appreciated......
 
Upvote 0

Comalco

Member
Licensed User
Longtime User
Realtek WLAN Adapter

So after all the success of getting the 7in tablet working with the USB relays, the 4.3in wouldn't give under the same permissions files treatment. Interestingly, when I run USB Details, the port returns that it is a Realtek 802.11n WLAN adapter.......even with nothing connected. The manual clearly says its a USB port, as does the labelling of the unit, and they supply a micro USB to Type A lead with the product......is the type of port configured somewhere in a file?? I cannot imaging where, but not sure why the USB port would return that it is a WLAN port........?? Ideas or pointers appreciated......

So much for the previous comment......the 7in tablet does exactly the same thing....it says its a Realtek too! (but at least it drives the relays pcb!)
 
Upvote 0
Top