Author Topic: GenericHID as a Console App  (Read 13506 times)

ccrashh

  • Member
  • ***
  • Posts: 6
GenericHID as a Console App
« on: September 17, 2012, 10:06:45 am »
Was reworking the GenericHID code as a Console Application so I could call it from within another program and have it return a weight from a USB-connected scale.  The code, for the most part, works (though, sometimes it steps oddly through the code and skips the actual data transfer - seems random to me, but am still looking into why it is doing that.  Might require another topic on this forum :) ).  My question is how the heck can you know the PID and VID beforehand?  By that, I mean, every example of HID code I have seen requires the entry of a PID and VID to work.  When the scale is connected, it appears as simply a HID-Compliant Device - no other clearly identifying information.  So, if I have more than one HID Compliant device attached to my PC, how would code know which is the scale?

EDIT:  I added the Module I created.  I removed all references to the Form, and named this GenericHID.vb (overwrote the original one).  I kept all other references the same.




[attachment deleted by admin]
« Last Edit: September 17, 2012, 10:30:30 am by ccrashh »

ccrashh

  • Member
  • ***
  • Posts: 6
Re: GenericHID as a Console App
« Reply #1 on: September 17, 2012, 10:54:19 am »
Oh, and before anyone else mentions it, I have purchased the book USB Complete (4th edition).  It just hasn't arrived yet. :)

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: GenericHID as a Console App
« Reply #2 on: September 17, 2012, 11:12:20 am »
Use the VID and PID if the application is for a particular vendor-specific device.

For device functions that have defined Usages, use the Usage codes from the HIDP_CAPS structure to identify the device's function. For example, a keyboard has a usage of Generic Desktop (01h), Keyboard (06h).

The Usage for weighing devices is 8D.

See the Point of Sale Usage Tables document here:

http://www.usb.org/developers/docs/hidpage/

ccrashh

  • Member
  • ***
  • Posts: 6
Re: GenericHID as a Console App
« Reply #3 on: September 17, 2012, 11:54:58 am »
That's perfect!  Thank you.  I simply added a line:

usagepage =  MyHid.Capabilities.UsagePage and it returned 8D, as per your comment and the table you linked.

Now I just have to loop through all connected USB devices to connect to the right one rather than pass or enter a PID and VID.

ccrashh

  • Member
  • ***
  • Posts: 6
Re: GenericHID as a Console App
« Reply #4 on: September 18, 2012, 11:51:13 am »
Nope.  Still can't figure out why the code works sometimes and not others.  It's very odd.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: GenericHID as a Console App
« Reply #5 on: September 18, 2012, 11:54:36 am »
Sometimes it finds the device and sometimes it doesn't? I would start by adding a breakpoint and examining the information for each detected HID.

ccrashh

  • Member
  • ***
  • Posts: 6
Re: GenericHID as a Console App
« Reply #6 on: September 18, 2012, 12:27:58 pm »
Sometimes it finds the device and sometimes it doesn't? I would start by adding a breakpoint and examining the information for each detected HID.

No. It finds the device ok.  But when I step through the code I can see it go to ExchangeInputAndOutputReports and get to the ar= line, then return to my Sub Main.  Sometimes it steps into  GetInputReportData and I can watch it hit the calculation code I wrote.  But sometimes it doesn't seem to step into it.  No pattern to it at all :)

I know enough of VB.NET to get myself into trouble.  So it is definitely something I am missing that would be obvious to a real VB.NET developer.  I'll keep plugging away at it.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: GenericHID as a Console App
« Reply #7 on: September 18, 2012, 12:48:33 pm »
Examine the HIDP_CAPS structure to find out what reports the device supports. The Usage Tables document I mentioned above might have more info about the scale protocols.

ccrashh

  • Member
  • ***
  • Posts: 6
Re: GenericHID as a Console App
« Reply #8 on: October 23, 2012, 08:23:00 am »
Examine the HIDP_CAPS structure to find out what reports the device supports. The Usage Tables document I mentioned above might have more info about the scale protocols.

Ok.  Have tried everything.  The command line app worked sometimes and other times it failed to return a weight.  Now, even though I have made no changes, it is always failing.  How can an app work sometimes and fail othertimes...and now fail ALL the time?  Same PC, same code, and I tried different USB ports.  Nothing. 

This was so much easier using an RS232 connection.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: GenericHID as a Console App
« Reply #9 on: October 23, 2012, 10:12:02 pm »
The fact that it worked previously is promising.

A hardware USB protocol analyzer would show the traffic on the bus and might offer a clue. If you don't have an analyzer and if the scale is an existing device (not one you're developing), about all you can do is work with the PC software to find the problem.

What to do next depends on how it fails. Some things to consider: Does ReadFile hang or does it return a weight of zero? Do you execute the command line before or after placing a weight on the scale? Does the original Generic HID example still work?