Using an HID Device RRS feed

  • Question

  • Hi,

         I'm working on a project that reads input from HID card swipers and I've found several examples of C# apps that interact with them, but the code is complicated and seemingly impossible to debug.  

    (The C# app)

    I was wondering what the best route to take would be to write my own.  Also, all of the examples I've found have polled the device for input rather than waiting for the device to send input (the latter is what I'd like to do.)  Are there any downloadable classes or libraries that might prove useful?  Any help would be greatly appreciated.

    Wednesday, March 4, 2009 3:09 PM

All replies

  • Which Port does your device work on??

    There is a lot in System.IO

    Wednesday, March 4, 2009 3:19 PM
  • The device is on high speed USB as far as I can tell, not sure if thats the answer you were looking for though.
    Wednesday, March 4, 2009 3:22 PM
  • Compared to writing your own code, you are probably far better off using Jan Axelson's (translated) code.
    You cannot get rid of the polling. One way or the other, USB polls devices for data.

    When I looked into USB through HID in C#, I found no alternative to Jan Axelson's code.
    Wednesday, March 4, 2009 3:33 PM
  • See if you can use System.IO.Ports.SerialPort Class.

    Wednesday, March 4, 2009 3:39 PM
  • Thanks for all the replies, I'll look into using System.IO.Ports.SerialPort and if that proves too far out of my expertise I'll go back to attempting to change Jan Axelson's code to suit my needs.
    Wednesday, March 4, 2009 3:46 PM
  • Ok, got Jan Axelson's code to work a bit better for what we needed (increased the wait time for it to timeout on a poll substantially) but now our dilemma is in threading the application as we need it.  We need it to recognize and read from several (up to 8) HID devices at a time.  All of the threading examples I can find are displaying simple output and asynchronous file i/o.  I was wondering if there was some way to tie each USB Handle to a thread and then have those threads do exactly what the initial program does for one HID device (wait for input, process/display it, etc.)  Again any help would be greatly appreciated.
    Thursday, March 5, 2009 8:33 PM
  • I recommend Hid.Net. It works on Windows and also UWP. It is also has a common interfaces with USB.Net which works on Android, Linux, and MacOS.

    Here is a code sample:

    internal sealed class TrezorExample : IDisposable
            #region Fields
            private const int PollMilliseconds = 6000;
            private const int PollMilliseconds = 3000;
            //Define the types of devices to search for. This particular device can be connected to via USB, or Hid
            private readonly List<FilterDeviceDefinition> _DeviceDefinitions = new List<FilterDeviceDefinition>
                new FilterDeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x", UsagePage=65280 },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x534C, ProductId=0x0001, Label="Trezor One Firmware 1.6.x (Android Only)" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, Label="Trezor One Firmware 1.7.x" },
                new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C0, Label="Model T" }
            #region Events
            public event EventHandler TrezorInitialized;
            public event EventHandler TrezorDisconnected;
            #region Public Properties
            public IDevice TrezorDevice { get; private set; }
            public  DeviceListener DeviceListener { get;  }
            #region Constructor
            public TrezorExample()
                DeviceListener = new DeviceListener(_DeviceDefinitions, PollMilliseconds) { Logger = new DebugLogger() };
            #region Event Handlers
            private void DevicePoller_DeviceInitialized(object sender, DeviceEventArgs e)
                TrezorDevice = e.Device;
                TrezorInitialized?.Invoke(this, new EventArgs());
            private void DevicePoller_DeviceDisconnected(object sender, DeviceEventArgs e)
                TrezorDevice = null;
                TrezorDisconnected?.Invoke(this, new EventArgs());
            #region Public Methods
            public void StartListening()
                DeviceListener.DeviceDisconnected += DevicePoller_DeviceDisconnected;
                DeviceListener.DeviceInitialized += DevicePoller_DeviceInitialized;
            public async Task InitializeTrezorAsync()
                //Get the first available device and connect to it
                var devices = await DeviceManager.Current.GetDevicesAsync(_DeviceDefinitions);
                TrezorDevice = devices.FirstOrDefault();
                if (TrezorDevice == null) throw new Exception("There were no devices found");
                await TrezorDevice.InitializeAsync();
            public async Task<byte[]> WriteAndReadFromDeviceAsync()
                //Create a buffer with 3 bytes (initialize)
                var writeBuffer = new byte[64];
                writeBuffer[0] = 0x3f;
                writeBuffer[1] = 0x23;
                writeBuffer[2] = 0x23;
                //Write the data to the device
                return await TrezorDevice.WriteAndReadAsync(writeBuffer);
            public void Dispose()
                DeviceListener.DeviceDisconnected -= DevicePoller_DeviceDisconnected;
                DeviceListener.DeviceInitialized -= DevicePoller_DeviceInitialized;

    Monday, March 11, 2019 6:39 AM