Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I connect to a Server Side USB (HID) Device from within an ASP.NET Application?

Tags:

c#

asp.net

usb

hid

I'm trying to write my own controller for a USB device instead of using the SDK that comes with the product (I feel the sdk is sub-par).

The USB Device is plugged into the SAME SERVER that this application is running on.

So I decided to head over to Nuget and grab the HidLibrary

PM> Install-Package hidlibrary

and I proceeded to follow the example found on GitHub.

First I went into my control panel to verify the VendorID and the ProductID

VendorID and ProductID

And I dropped it into my code.

Then I set a breakpoint on the line that grabs the device, but unfortunately it always comes back null.

using HidLibrary;
public class MyController : ApiController
{

    private const int VendorId = 0x0BC7;
    private const int ProductId = 0x0001;

    private static HidDevice _device;

    // POST api/<controller>
    public string Post(CommandModel command)
    {

        _device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();

        if (_device != null)
        {
            // getting here means the device exists
        }
        else
        {
            // ending up here means the device doesn't exist
            throw new Exception("device not connected");
        }
        return null;
    }

I'm hoping it's something silly, and not some deal-breaking permissions issue regarding connecting to a USB device directly from an IIS worker.

like image 957
Chase Florell Avatar asked Nov 16 '12 20:11

Chase Florell


People also ask

How does HID USB work?

The USB HID class keyboard is normally designed with an IN endpoint that communicates keystrokes to the computer and an OUT endpoint that communicates the status of the keyboard's LEDs from the computer to the keyboard.

What is the HID mean in reference to USB devices?

Human Interface Devices (HID) is a device class definition to replace PS/2-style connectors with a generic USB driver to support HID devices such as keyboards, mice, game controllers, and so on.

What is a HID connection?

An HID (Human Interface Device) is a type of device that takes input from or provides output to humans. It also refers to the HID protocol, a standard for bi-directional communication between a host and a device that is designed to simplify the installation procedure.

What is HID support?

HID(Human Interface Device) technology provides support for devices such as mouse and keyboard. It is designed to provide a low latency link, with low power requirements. Consumer can connect mouse and keyboards for data input and to select data at various sections in TV.


2 Answers

Despite your hopes to be something silly, it is not. You have some deal-breaking permission issues. If you will browse Mike O'Brien's code from GitHub of the Hid Library you will see that it calls Win32 API functions located in: kernel32.dll, setupapi.dll, user32.dll, hid.dll (Native.cs).

The enumeration itself it's done through setupapi.dll functions. It browse all the installed devices and filters what it's need it.

So... I think it's a security issue to execute kernel32.dll code directly from a web-app in IIS with anonymous authentication, don't you?

If you really need to communicate with that HID (who knows maybe it's a temperature sensor, or something else) I would do a separate Windows service and the IIS hosted web app would communication through WCF with this service. This service would like a proxy.

like image 172
garzanti Avatar answered Sep 28 '22 20:09

garzanti


Put the same code in a console application and run it. That will help you verify if it's your code or environment.

If it's environment, try using Process Monitor to see if there are any hidden access errors. Also try enumerating all devices, not just looking for the one device you're after, just to see if you can do it in ASP.NET.

like image 28
Samuel Neff Avatar answered Sep 28 '22 19:09

Samuel Neff