如何从USB RFID读卡器中读取数据?

7

我已经购买了一个USB RFID读卡器。当用户将RFID标签放在设备前面时,我该如何读取数据?

我的电脑将该设备识别为“人机接口设备”。如果它将其识别为COM设备,则使用SerialPort对象从设备读取数据会更加容易,但是我不知道如何从USB设备读取数据。

有什么帮助吗?


我也有这个需求,只是目前没有设备。我正在寻找一个读取器,可能有一些可以轻松连接的东西。 - Tsukasa
我已经知道答案了,只是在寻找可能更好的方法。 - Ali.Rashidi
我在一年后发布这个答案,幸运的是我发现了这个网站并完全解决了我的问题。LibUsbDotNet是一个.NET C# USB库 - Ali.Rashidi
你从哪里获取“YOUR USB Vendor and Product ID”?(来自被接受的答案) - levi
从我的电脑的设备管理器中 - Ali.Rashidi
使用libusbdotnet,它真的很有效。 - Ali.Rashidi
3个回答

5
这是我在遇到相同问题时所做的。
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秒内未收到数据。


但是真的能工作吗?我尝试在Mac和Linux上安装LibUsbDotNet,但很难安装这个库...为什么? - riccardo airone
这个可以运行,但是你怎么获取实际的卡号呢?就是印在卡背面的那个? - OJ Raqueño
1
我无法让它工作。由于我的读卡器出现为HID键盘,因此似乎LibUsbDotNet无法访问它:https://github.com/libusb/libusb/wiki/FAQ#Does_libusb_support_USB_HID_devices。除非有人找到了使其工作的方法? - Brent

1
请参考:如何从USB HID设备读取输入? 该设备可能被枚举为通用HID,但可能表现得像键盘一样。 如果是这样,它应该只会打印出它读取的字符。 如果不是,则必须从驱动程序中轮询它。 那个链接应该有所帮助。

1
如果您的读卡器附带了 SDK,请使用它。否则,您需要了解读卡器的十六进制命令。向读卡器发送命令并获取响应。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接