C# 如何读取usb rfid阅读器?

C# 如何读取usb rfid阅读器?,c#,usb,rfid,C#,Usb,Rfid,我买了一个usb rfid阅读器。当用户将rfid标签放在设备前面时,我如何读取数据 我的计算机将该设备识别为人机界面设备。 如果它将它标识为com设备,那么从带有serialPort对象的设备读取就容易多了,但我不知道如何从usb设备读取 有什么帮助吗 请参见: 设备可能会枚举为通用HID,但其行为可能类似于键盘。如果是这样,它应该只键入它读取的字符。如果没有,你必须从司机那里投票。这个链接应该会有帮助。如果他们给了你一个SDK和你的阅读器,那么就使用它。否则,您将必须了解读取器的十六进制命令

我买了一个usb rfid阅读器。当用户将rfid标签放在设备前面时,我如何读取数据

我的计算机将该设备识别为人机界面设备。 如果它将它标识为com设备,那么从带有serialPort对象的设备读取就容易多了,但我不知道如何从usb设备读取

有什么帮助吗

请参见:


设备可能会枚举为通用HID,但其行为可能类似于键盘。如果是这样,它应该只键入它读取的字符。如果没有,你必须从司机那里投票。这个链接应该会有帮助。

如果他们给了你一个SDK和你的阅读器,那么就使用它。否则,您将必须了解读取器的十六进制命令。向读取器发送命令并获得响应

这就是我遇到相同问题时所做的

using System;
using System.Text;
using LibUsbDotNet;
using LibUsbDotNet.Main;

namespace Examples
{
internal class ReadPolling
{
    public static UsbDevice MyUsbDevice;

    #region SET YOUR USB Vendor and Product ID!

    public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1234, 1);

    #endregion

    public static void Main(string[] args)
    {
        ErrorCode ec = ErrorCode.None;

        try
        {
            // Find and open the usb device.
            MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

            // If the device is open and ready
            if (MyUsbDevice == null) throw new Exception("Device Not Found.");

            // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
            // it exposes an IUsbDevice interface. If not (WinUSB) the 
            // 'wholeUsbDevice' variable will be null indicating this is 
            // an interface of a device; it does not require or support 
            // configuration and interface selection.
            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                // This is a "whole" USB device. Before it can be used, 
                // the desired configuration and interface must be selected.

                // Select config #1
                wholeUsbDevice.SetConfiguration(1);

                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
            }

            // open read endpoint 1.
            UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);


            byte[] readBuffer = new byte[1024];
            while (ec == ErrorCode.None)
            {
                int bytesRead;

                // If the device hasn't sent data in the last 5 seconds,
                // a timeout error (ec = IoTimedOut) will occur. 
                ec = reader.Read(readBuffer, 5000, out bytesRead);

                if (bytesRead == 0) throw new Exception(string.Format("{0}:No more bytes!", ec));
                Console.WriteLine("{0} bytes read", bytesRead);

                // Write that output to the console.
                Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
            }

            Console.WriteLine("\r\nDone!\r\n");
        }
        catch (Exception ex)
        {
            Console.WriteLine();
            Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
        }
        finally
        {
            if (MyUsbDevice != null)
            {
                if (MyUsbDevice.IsOpen)
                {
                    // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                    // it exposes an IUsbDevice interface. If not (WinUSB) the 
                    // 'wholeUsbDevice' variable will be null indicating this is 
                    // an interface of a device; it does not require or support 
                    // configuration and interface selection.
                    IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }

                    MyUsbDevice.Close();
                }
                MyUsbDevice = null;

                // Free usb resources
                UsbDevice.Exit();

            }

            // Wait for user input..
            Console.ReadKey();
        }
    }
  }
}
按供应商和产品id打开USB设备

打开一个UsbEndpointReader类进行阅读


读取并显示来自Ep01的usb设备输出,直到5秒钟内没有收到任何数据。

我也需要这样做,我只是目前没有设备。我正在寻找一个读者,可能有一些东西,可以很容易地连接到。我已经知道答案。我只是想寻找更好的方法。一年后,我发布了这个答案。幸运的是,我找到了这个网站,它彻底解决了我的问题。您从哪里获得“您的USB供应商和产品ID”?(来自已接受的答案)来自我电脑中的设备管理器,但真的有效吗?我试着用mac和linux安装LibUsbDotNet,但是很难安装这个库…为什么?这是可行的,但是如何获得实际的卡号呢?印在卡片背面的那个?我没法让它工作。因为我的阅读器是一个HID键盘,所以LibUsbDotNet似乎无法访问它:。除非其他人找到了一种方法来实现这一点?