我想用C#读取rfid标签,我该怎么办? [英] I want to read rfid tag using C#, how do I do it?

查看:864
本文介绍了我想用C#读取rfid标签,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 

使用 System.Windows.Forms;
使用 System.IO.Ports;
使用 System.Threading;


命名空间 rfidProject
{
public partial class RFIDReader:表格
{
private SerialPort RFID;

public RFIDReader()
{
InitializeComponent();
}

private void RFIDReader_Load( object sender,EventArgs e)
{

RFID = new SerialPort();
RFID.PortName = COM1;
RFID.BaudRate = 9600 ;
RFID.DataBits = 8 ;
RFID.Parity = Parity.None;
RFID.StopBits = StopBits.One;
// RFID.RtsEnable = true;

// RFID.Handshake = Handshake.None;

RFID.Open();
// RFID.ReadTimeout = 200;

string [] ports = SerialPort.GetPortNames();
foreach string port in ports)
{
// MessageBox.Show(port);
PORTNameInputBox.Text + = port;
}




RFID.DataReceived + = new SerialDataReceivedEventHandler(RFID_DataReceivedHandler);

// RFID.Close();

}

private void RFID_DataReceivedHandler( object sender,SerialDataReceivedEventArgs e)
{

SerialPort sp =(SerialPort)sender;
string indata = sp.ReadExisting();

// MessageBox.Show(indata);

RFIDTagInputBox.Text + = indata;

}

}
}





什么我试过了:



我尝试使用acr122u-a9阅读器从串口读取rfid标签,但它没有做任何事情。我使用了上面指定的代码。



除串口外还有什么方法可以读取rfid标签吗?

解决方案

< blockquote>你应该与创建它的人交谈 - NFC非接触式支付 - ACR122U USB NFC读卡器| ACS司机 [ ^ ] - 他们应该提供技术支持,并且会比我们更了解他们的产品。如果他们没有,那么找另一个供应商并要求退款!


您正在打开 COM1 。即使是没有RS-232端口的现代PC,这通常也是为芯片组串口保留的。



您的读卡器是一个模拟串口的USB设备。这需要一个提供虚拟COM端口的驱动程序。连接阅读器后,打开设备管理器以列出COM端口。你的读者应该有一个条目。如果没有,请确保已安装驱动程序。记下端口号并在代码中使用它而不是COM1。



您也可以让您的应用程序检查哪些串口可用并显示用于选择的对话框(您已经有了一些代码,但在打开端口后调用它)。但是,代码中的方法仅返回普通的COMx名称,如果您有多个串行设备,则可能难以识别您的阅读器。设备管理器中的列表还显示了所谓的用户友好名称,它通常包含足够的信息来识别读卡器等虚拟端口设备。要获得这些用户友好名称,您可以使用WMI。可以在CP文章中找到仅列出虚拟端口的示例一个有用的WMI工具&如何查找USB到串行适配器 [ ^ ](可能有更好的用于您的目的,但我没有找到任何快速研究)。


using System;

using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;


namespace rfidProject
{
    public partial class RFIDReader : Form
    {
        private SerialPort RFID;

        public RFIDReader()
        {
            InitializeComponent();
        }

        private void RFIDReader_Load(object sender, EventArgs e)
        {

            RFID = new SerialPort();
            RFID.PortName = "COM1";
            RFID.BaudRate = 9600;
            RFID.DataBits = 8;
            RFID.Parity = Parity.None;
            RFID.StopBits = StopBits.One;
            //RFID.RtsEnable = true;

            //RFID.Handshake = Handshake.None; 

            RFID.Open();
           // RFID.ReadTimeout = 200;

            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                //MessageBox.Show(port);
                PORTNameInputBox.Text += port;
            }




            RFID.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceivedHandler);

           // RFID.Close();

        }

        private void RFID_DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {

            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();

            //MessageBox.Show(indata);

            RFIDTagInputBox.Text += indata;

        }

    }
}



What I have tried:

I tried to read rfid tag from serial port using acr122u-a9 reader but it does not do anything. I used the specified code above.

Is there any way reading rfid tag aside from serial port?

解决方案

You should talk to the people who created it - NFC Contactless Payments - ACR122U USB NFC Reader | ACS Drivers[^] - they should provide technical support and will know more about their product than we will. If they don't, then find another supplier and demand your money back!


You are opening COM1. Even with modern PCs that did not have an RS-232 port this is usually reserved for chipset serial ports.

Your reader is an USB device that emulates a serial port. This requires a driver that provides a virtual COM port. When the reader is connected, open the device manager to list the COM ports. There should be an entry for your reader. If not, ensure that the driver is installed. Make a note of the port number and use that in your code instead of "COM1".

You can also let your application check which serial ports are available and show a dialog for selection (you already have some code for that but it is called after opening the port). But the method from your code returns only the plain COMx name which might make it difficult to identify your reader if you have multiple serial devices. The list in the device manager shows also the so called "User friendly name" which usually contains enough information to identify virtual port devices like your reader. To get also these user friendly names you can use WMI. An example listing only virtual ports can be found in the CP article A Useful WMI Tool & How To Find USB to Serial Adaptors[^] (there might be better ones for your purpose but I did not found any upon a quick research).


这篇关于我想用C#读取rfid标签,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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