如何使RFID读取器使用Microsoft Visual C#自动扫描RFID标签?

时间:2016-03-21 11:11:31

标签: c# rfid

我正在开发一个使用RFID硬件的项目。我在这里遇到一些代码有问题。我想开发一些代码,使RFID阅读器能够自动扫描读卡器附近的Mifare 1K卡。有可能吗?这里是我想要开发的代码。对于RFID阅读器,我使用的是SL500F系列。谢谢你。 :)

boll bConnectedDevice;

private void Form1_Load(object sender, EventArgs e)
        {
            bConnectedDevice = false;
        }

private void btnReqID_Click(object sender, EventArgs e)
    {
                short icdev = 0x0000;
                string card = "";
                int status;
                byte type = (byte)'A';//mifare one 卡询卡方式为A
                byte mode = 0x52;
                ushort TagType = 0;
                byte bcnt = 0x04;//mifare 卡都用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_antenna_sta(icdev, 0);//关闭天线
                        if (status != 0)
                            continue;

                        Sleep(20);
                        status = rf_init_type(icdev, type);
                        if (status != 0)
                            continue;

                        Sleep(20);
                        status = rf_antenna_sta(icdev, 1);//启动天线
                        if (status != 0)
                            continue;

                        Sleep(50);
                        status = rf_request(icdev, mode, ref TagType);//搜寻所有的卡
                        if (status != 0)
                            continue;

                        status = rf_anticoll(icdev, bcnt, pSnr, ref len);//返回卡的序列号
                        if (status != 0)
                            continue;

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

                        status = rf_light(icdev, 3);
                        if (status != 0)
                            continue;
                        Sleep(100);
                        status = rf_beep(icdev, 5);
                        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]);
                        }
                        card = m_cardNo;

                        status = rf_light(icdev, 2);
                        if (status != 0)
                            continue;

                        break;
                    }
    }

0 个答案:

没有答案