C#读取RFID卡号源码_c#

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Runtime.InteropServices;  //调用动态库一定要加入这个引用

namespace idcardreader

{

    public partial class Form1 : Form

    {

        //本示例设备链接:​​https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.23.16795b43onC5S2&id=600862990715​​​

        //外部函数声明:让设备发出声响

        [DllImport("OUR_IDR.dll", EntryPoint = "idr_beep", CallingConvention = CallingConvention.StdCall)]

        static extern byte idr_beep(UInt32 xms);//xms单位为毫秒 

    //------------------------------------------------------------------------------------------------------------------------------------------------------    

        //读取设备编号,可做为软件加密狗用,也可以根据此编号在公司网站上查询保修期限

        [DllImport("OUR_IDR.dll", EntryPoint = "pcdgetdevicenumber", CallingConvention = CallingConvention.StdCall)]

        static extern byte pcdgetdevicenumber(byte[] devicenumber);//devicenumber用于返回编号 

        //------------------------------------------------------------------------------------------------------------------------------------------------------    

        //只读卡号

        [DllImport("OUR_IDR.dll", EntryPoint = "idr_read", CallingConvention = CallingConvention.StdCall)]

        public static extern byte idr_read(byte[] serial);//serial返回卡号

    //------------------------------------------------------------------------------------------------------------------------------------------------------    

        //只读卡号,只读一次,必须拿开卡才能再读到

        [DllImport("OUR_IDR.dll", EntryPoint = "idr_read_once", CallingConvention = CallingConvention.StdCall)]

        public static extern byte idr_read_once(byte[] serial);//serial返回卡号

        //------------------------------------------------------------------------------------------------------------------------------------------------------

        public Form1()

        {

            InitializeComponent();

        }

        private void button2_Click(object sender, EventArgs e)

        {

            byte status;//存放返回值

            byte[] myserial = new byte[5];//卡序列号

            status = idr_read(myserial);

            switch (status)

            {

                case 0:

                    idr_beep(50);

                    MessageBox.Show("读卡成功,卡号为" + System.Convert.ToString(myserial[1] * 256 * 256 * 256 + myserial[2] * 256 * 256 + myserial[3] * 256 + myserial[4]));                    


                    break;

                //......

                case 8:

                    MessageBox.Show("请将卡放在感应区");

                    break;

                default:

                    MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status);

                    break;

            }

        }

        private void button7_Click(object sender, EventArgs e)

        {//嘀一声

            idr_beep(50);

        }

        private void button6_Click(object sender, EventArgs e)

        {//读取设备编号,可做为软件加密狗用

            byte[] devno = new byte[4];

            if (pcdgetdevicenumber(devno) == 0)

            {

                MessageBox.Show(System.Convert.ToString(devno[0]) + "-" + System.Convert.ToString(devno[1]) + "-" + System.Convert.ToString(devno[2]) + "-" + System.Convert.ToString(devno[3]));                

            }

        }

        private void button4_Click(object sender, EventArgs e)

        {

            byte status;//存放返回值

            byte[] myserial = new byte[5];//卡序列号

            status = idr_read_once(myserial);

            switch (status)

            {

                case 0:

                    idr_beep(50);

                    MessageBox.Show("读卡成功,卡号为" + System.Convert.ToString(myserial[1] * 256 * 256 * 256 + myserial[2] * 256 * 256 + myserial[3] * 256 + myserial[4]));                   

                    break;

                //......

                case 8:

                    MessageBox.Show("请将卡放在感应区");

                    break;

                default:

                    MessageBox.Show("返回码(对应的说明请看例子中的注释):" + status);

                    break;

            }

        }

    }

}