如何读取usb rfid阅读器



我买了一个usb rfid阅读器。当用户在设备前放置射频识别标签时,我如何读取数据?

我的计算机将该设备识别为人机界面设备。如果它将其识别为com设备,则从带有serialPort对象的设备中读取要容易得多,但我不知道如何从usb设备中读取。

帮忙吗?

当我遇到同样的问题时,我就是这样做的。

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("rnDone!rn");
        }
        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 HID设备读取输入?

设备可以枚举为通用的HID,但可能表现得像键盘。如果是这样,它应该只输出它所读取的字符。如果没有,你必须从司机那里轮询。这个链接应该有帮助。

如果他们给了你一个SDK与你的阅读器,那么使用它。否则你将不得不知道你的阅读器的十六进制命令。向阅读器发送命令并获得响应

相关内容

  • 没有找到相关文章

最新更新