如何使用 C# 从 USB RFID 阅读器读取 [英] How to read from a USB RFID Reader with C#

查看:90
本文介绍了如何使用 C# 从 USB RFID 阅读器读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚买了一个 USB RFID 阅读器,现在我想读、写和检查读卡器上是否有卡.所以我在这里搜索并找到了下面的代码.对它的描述几乎就是我所需要的:

I just bought an USB RFID Reader and now I want to read, write and check if a card is on the Reader at the moment or not. So I searched here and found the Code below. The description of it is pretty much all what I Need:

  • 按供应商和产品 ID 打开 USB 设备.

  • Opens a USB device by vendor and product id.

打开一个用于阅读的 UsbEndpointReader 类.

Opens a UsbEndpointReader class for reading.

从 Ep01 读取并显示 USB 设备输出,直到 5 秒内未收到数据.

Reads and displays usb device output from Ep01 until no data is received for 5 seconds.

但我不明白USBDevice"、USBDeviceFinder"等来自哪里?这是用户发布的唯一代码.我将它复制到我的 Visual Studio 中,正如预期的那样,例如当前上下文中不存在名称‘USBDevice’"

But I dont understand where "USBDevice", "USBDeviceFinder" etc. are coming from? It's the only Code the user posted. I copied it into my Visual Studio and as expected it says for example "The Name 'USBDevice' does not exist in the current context"

有人可以帮忙解决这个问题吗?

Can someone help how to fix this?

顺便说一句.这是原始问题和答案:原文链接

Btw. this is the original question and the answers: Link to the Original Post

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();
        }
     }
   }
 }

推荐答案

最有可能在 LibUsbDotNet 包中.如果您使用的是 Visual Studio,请打开工具">NuGet 包管理器">管理解决方案的 NuGet 包...",转到浏览"并将其粘贴到搜索栏中.将其添加到您的项目中,现在应该可以开始使用了.

It's most likely in the package LibUsbDotNet. If you are using Visual Studio, open up Tools > NuGet Package Manager > Manage NuGet Packages for Solution..., go to Browse and paste it into the search bar. Add it to your project and now it should be good to go.

这篇关于如何使用 C# 从 USB RFID 阅读器读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆