2

这是我第一次尝试对 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);
    }

该代码来自制造商,我对它的实际作用知之甚少。

任何信息都会很好。

4

1 回答 1

1

像这样的东西?它将读卡代码以 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);                
        }
    }

用法:

var reader = new CardReader();
reader.Start(CardReaded); 

private void CardReaded(string cardnr){
...
}
于 2013-11-03T15:29:02.597 回答