Device.Net: HID Device not detecting
Hi,
I am trying to read data from a HID device, the device type is (USB RFID Id Contactless Proximity Smart Card Reader 125KHZ Em4001 Em4100). The device type is not detected and no events like connected and disconnected event triggered.
Please suggest.
I have provided the sample code for your reference
` class Program
{
#region Fields
private static readonly TrezorExample _DeviceConnectionExample = new TrezorExample();
///
static void Main(string[] args)
{
Console.WriteLine("Hello USB!");
WindowsUsbDeviceFactory.Register(Logger, Tracer);
WindowsHidDeviceFactory.Register(Logger, Tracer);
var devices = DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(null);
Console.WriteLine("Currently connected devices: ");
foreach (var device in devices.Result)
{
Console.WriteLine($"{device.DeviceId} - {device.Label} - {device.Manufacturer} - {device.ProductName}");
}
Console.WriteLine();
Console.WriteLine("Connected device completed.");
_DeviceConnectionExample.TrezorInitialized += _DeviceConnectionExample_TrezorInitialized;
_DeviceConnectionExample.TrezorDisconnected += _DeviceConnectionExample_TrezorDisconnected;
Go();
new ManualResetEvent(false).WaitOne();
}
private static async Task Go()
{
var menuOption = await Menu();
switch (menuOption)
{
case 1:
try
{
await _DeviceConnectionExample.InitializeTrezorAsync();
await DisplayDataAsync();
_DeviceConnectionExample.Dispose();
GC.Collect();
await Task.Delay(10000);
}
catch (Exception ex)
{
Console.Clear();
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
break;
case 2:
Console.Clear();
DisplayWaitMessage();
_DeviceConnectionExample.StartListening();
break;
}
}
#region Event Handlers
private static void _DeviceConnectionExample_TrezorDisconnected(object sender, EventArgs e)
{
Console.Clear();
Console.WriteLine("Disconnected.");
DisplayWaitMessage();
}
private static async void _DeviceConnectionExample_TrezorInitialized(object sender, EventArgs e)
{
try
{
Console.Clear();
await DisplayDataAsync();
}
catch (Exception ex)
{
Console.Clear();
Console.WriteLine(ex.ToString());
}
}
#endregion
#region Private Methods
private async static Task<int> Menu()
{
while (true)
{
Console.Clear();
var devices = await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(null);
Console.WriteLine("Currently connected devices: ");
foreach (var device in devices)
{
Console.WriteLine(device.DeviceId);
}
Console.WriteLine();
Console.WriteLine("Console sample. This sample demonstrates either writing to the first found connected device, or listening for a device and then writing to it. If you listen for the device, you will be able to connect and disconnect multiple times. This represents how users may actually use the device.");
Console.WriteLine();
Console.WriteLine("1. Write To Connected Device");
Console.WriteLine("2. Listen For Device");
var consoleKey = Console.ReadKey();
if (consoleKey.KeyChar == '1') return 1;
if (consoleKey.KeyChar == '2') return 2;
}
}
private static async Task DisplayDataAsync()
{
var bytes = await _DeviceConnectionExample.WriteAndReadFromDeviceAsync();
Console.Clear();
Console.WriteLine("Device connected. Output:");
DisplayData(bytes);
}
private static void DisplayData(byte[] readBuffer)
{
Console.WriteLine(string.Join(' ', readBuffer));
Console.ReadKey();
}
private static void DisplayWaitMessage()
{
Console.WriteLine("Waiting for device to be plugged in...");
}
#endregion
}`
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 1
- Comments: 51 (28 by maintainers)
@MelbourneDeveloper Thanks for your quick response, below are the details you wanted. I am uSing Windows 10, VS 2019 FW .NET Core 3.0
Console Output:
Exception Details and breakdown
I got the error @ WindowsBaseDevice.cs
Exception details
Device.Net.Exceptions.ApiException HResult=0x80131500 Message=Could not get Hid Attributes (Call HidD_GetAttributes). Error code: 6 Source=Device.Net StackTrace: at Device.Net.Windows.WindowsDeviceBase.HandleError(Boolean isSuccess, String message) in D:\SourceCode\Device.Net\src\Device.Net\Windows\WindowsDeviceBase.cs:line 37
Call back:
@MelbourneDeveloper
No Problem. I have found a workaround for this. As you stated that this device is detected as a simple keyboard that gave me a hint of reading raw input from keyboard. I used the below article Reading Raw Input from keyboard. Now, I am able to read the device data when I show the card.
If you want to read the data from that device, you should use the proximity card. I can share the one from my end, if you wish you can share your address in a private message through twitter(@SRGaneshRam) or email(sr.ganeshram@hotmail.com)
Thanks.
@melbournedeveloper I just showed the card. I have additional cards with me. If you need I can send the one to Australia.
On Aug 7, 2019 12:57 PM, Christian Findlay notifications@github.com wrote:
@srganeshramhttps://github.com/srganeshram oh no! So, I have to order something else from Amazon?
Also, why can’t you just use the device as a keyboard? The device is essentially a keyboard. The device sends keys to notepad like a regular keyboard. Is there some extra data if you read via USB?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/MelbourneDeveloper/Device.Net/issues/88?email_source=notifications&email_token=ABCG3RHXI5WGBYEOQ6NLKYTQDJ2PRA5CNFSM4IHJTQLKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3XPENA#issuecomment-518976052, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABCG3RAVURMR535HJKA4HIDQDJ2PRANCNFSM4IHJTQLA.
Ok. Thanks for your support. We can wait.
@MelbourneDeveloper I changed as stated.
_ReadSafeFileHandle = APICalls.CreateFile(DeviceId, APICalls.GenericRead, 3, IntPtr.Zero, APICalls.OpenExisting, 0, IntPtr.Zero);
to_ReadSafeFileHandle = APICalls.CreateFile(DeviceId, 0, 3, IntPtr.Zero, APICalls.OpenExisting, 0, IntPtr.Zero);
It seems it worked and passed the above line, I tried to read the data from the device by using “ReadReportAsync” method, but it throws me an error as “System.UnauthorizedAccessException: ‘Access to the path is denied.’”
Error details
Instance details of “_ReadFileStream”
Call back
@MelbourneDeveloper, Package: I am waiting for the delivery. New 3.0 Beta 2 version - I tried with the new beta version, the difference I found is earlier in my console it wont recognize the USB device itself, but now it can able to detect and list the devices.
But, I could not able to read the data still.I have attached the error details for your reference.
Console Window - (Using New version 3.0 Beta 2)
The above highlighted in red is the device I am trying to read, and it was listed out.
Error Details - (Using New version 3.0 Beta 2)
Call back
Let me know if you need any further details…
Thanks