C# 如何让RFID阅读器读取,直到得到结果

C# 如何让RFID阅读器读取,直到得到结果,c#,C#,这是我第一次尝试编码RFID阅读器 我有下面的代码,当我点击一个按钮读取卡片ID时,我想让代码不断地读取,直到有人把一张卡片放在读卡器上,然后根据卡片的位置做一些事情 private void btnRequest_Click(object sender, EventArgs e) { txtSearchPurse.Text = ""; short icdev = 0x0000; int status; by

这是我第一次尝试编码RFID阅读器

我有下面的代码,当我点击一个按钮读取卡片ID时,我想让代码不断地读取,直到有人把一张卡片放在读卡器上,然后根据卡片的位置做一些事情

     private void btnRequest_Click(object sender, EventArgs e)  
    {

        txtSearchPurse.Text = "";
        short icdev = 0x0000;
        int status;
        byte type = (byte)'A';//mifare one type is A 卡询卡方式为A
        byte mode = 0x26;  // Request the card which is not halted.
        ushort TagType = 0;
        byte bcnt = 0x04;//mifare 卡都用4, hold on 4
        IntPtr pSnr;
        byte len = 255;
        sbyte size = 0;


        if (!bConnectedDevice)
        {
            MessageBox.Show("Not connect to device!!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        pSnr = Marshal.AllocHGlobal(1024);

        for (int i = 0; i < 2 ;i++ )
        {
            status = rf_request(icdev, mode, ref TagType);//搜寻没有休眠的卡,request card  
            if (status != 0)
                continue;

            status = rf_anticoll(icdev, bcnt, pSnr, ref len);//防冲突得到返回卡的序列号, anticol--get the card sn
            if (status != 0)
                continue;

            status = rf_select(icdev, pSnr, len, ref size);//锁定一张ISO14443-3 TYPE_A 卡, select one card
            if (status != 0)
                continue;

            byte[] szBytes = new byte[len];

            for (int j = 0; j < len; j++)
            {
                szBytes[j] = Marshal.ReadByte(pSnr, j);
            }

            String m_cardNo = String.Empty;

            for (int q = 0; q < len; q++)
            {
                m_cardNo += byteHEX(szBytes[q]);
            }
            txtSearchPurse.Text = m_cardNo;                

            break;
        }

        Marshal.FreeHGlobal(pSnr);
    }
private void btnRequest_单击(对象发送者,事件参数e)
{
txtsearch钱包。Text=“”;
短icdev=0x0000;
智力状态;
字节类型=(字节)“A”;//mifare一种类型是A卡询卡方式为A.
byte mode=0x26;//请求未暂停的卡。
ushort-TagType=0;
字节bcnt=0x04;//mifare卡都用4,等一下
IntPtr峰值信噪比;
字节len=255;
sbyte大小=0;
如果(!b连接设备)
{
显示(“未连接到设备!!”,“错误”,MessageBoxButtons.OK,MessageBoxIcon.error);
返回;
}
pSnr=编组分配全局(1024);
对于(int i=0;i<2;i++)
{
状态=射频请求(icdev、模式、参考标记类型)//搜寻没有休眠的卡,申请卡
如果(状态!=0)
继续;
状态=射频反时钟(icdev、bcnt、pSnr、ref len)//防冲突得到返回卡的序列号, anticol--获取卡序列号
如果(状态!=0)
继续;
状态=射频选择(icdev、pSnr、透镜、参考尺寸)//锁定一张ISO14443-3 A型卡, 选择一张卡片
如果(状态!=0)
继续;
字节[]szBytes=新字节[len];
对于(int j=0;j
这段代码来自制造商,我对它的实际用途知之甚少

任何信息都可以。

像这样的信息? 它以1秒的间隔将读卡代码放置在线程上。 一旦读到卡,就会调用您的代码,您可以处理卡。 这只是一个开始的例子,还有很多地方需要改进

    class CardReader : IDisposable
    {

        IntPtr _pSnr = Marshal.AllocHGlobal(1024);
        private Thread _t;
        private Action<string> _callback;
        private volatile bool _stop;

        public void ReadCard()
        {
            short icdev = 0x0000;
            int status;
            byte type = (byte)'A';//mifare one type is A 卡询卡方式为A
            byte mode = 0x26;  // Request the card which is not halted.
            ushort TagType = 0;
            byte bcnt = 0x04;//mifare 卡都用4, hold on 4
            IntPtr pSnr;
            byte len = 255;
            sbyte size = 0;

            for (int i = 0; i < 2; i++) {
                status = rf_request(icdev, mode, ref TagType);//搜寻没有休眠的卡,request card  
                if (status != 0)
                    continue;

                status = rf_anticoll(icdev, bcnt, pSnr, ref len);//防冲突得到返回卡的序列号, anticol--get the card sn
                if (status != 0)
                    continue;

                status = rf_select(icdev, pSnr, len, ref size);//锁定一张ISO14443-3 TYPE_A 卡, select one card
                if (status != 0)
                    continue;

                byte[] szBytes = new byte[len];

                for (int j = 0; j < len; j++) {
                    szBytes[j] = Marshal.ReadByte(pSnr, j);
                }

                String m_cardNo = String.Empty;

                for (int q = 0; q < len; q++) {
                    m_cardNo += byteHEX(szBytes[q]);
                }

                _callback(m_cardNo);

                break;
            }
        }

        public void Work()
        {
            while (!_stop)
            {
                ReadCard();
                Thread.Sleep(1000);
            }
        }

        public void Start(Action<string> cardRead)
        {
            if (_t != null)
                return;

            _stop = false;

            _callback = cardRead;

            _t = new Thread(Work);
            _t.Start();
        }

        public void Stop()
        {
            if (_t != null)
            {
                _stop = true;
                _t.Join();
                _t = null;
            }
        }

        public void Dispose()
        {
            Marshal.FreeHGlobal(_pSnr);                
        }
    }

你好,Ryan,您应该使用SerialPort控件从设备读取数据。如果是这种情况,那么您可以使用SerialPort的ReadExisting事件。你好,感谢您的快速回复,我现在有一个以上代码的错误,status=rf_anticoll(icdev,bcnt,pSnr,ref len)//防冲突得到返回卡的序列号, anticol——如果(状态!=0)继续,则获取卡片序列号;pSnr从未分配?
var reader = new CardReader();
reader.Start(CardReaded); 

private void CardReaded(string cardnr){
...
}