如何从usb rfid阅读器阅读?

问题描述 投票:2回答:3

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

我的电脑将设备识别为人机接口设备。如果它将其识别为com设备,则使用serialPort对象从设备读取要容易得多,但我不知道如何从USB设备读取。

有帮助吗?

c# usb rfid
3个回答
4
投票

这就是我遇到同样问题时的所作所为。

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秒钟内没有收到数据。


1
投票

请参阅:How do I read input from a USB HID device?

设备可能枚举为通用HID,但可能表现得像键盘。如果是这样,它应该只输入它读取的字符。如果没有,您将不得不从驱动程序轮询它。该链接应该有所帮助。


1
投票

如果他们为您的读卡器提供了SDK,那么请使用它。否则你将不得不知道你的读者的十六进制命令。向读者发送命令并获得响应

© www.soinside.com 2019 - 2024. All rights reserved.