C# Winform 多线程监听IC卡读卡器 您所在的位置:网站首页 读卡器说明 C# Winform 多线程监听IC卡读卡器

C# Winform 多线程监听IC卡读卡器

2023-08-18 02:32| 来源: 网络整理| 查看: 265

 本文原创,转载请注明出处!

    我们使用IC读卡器时,一般可分为手动读卡和自动监听读卡两种方式。

    其中手动读卡根据IC设备厂商提供的Demo修改一下便可实现,相对简单,本文不做过多介绍。

    而自动监听读卡,看起来效果要好的多,用户使用起来也要方便一下。其实现的逻辑大家应该都知道,就是通过开一个多线程来轮训IC卡读卡的结果,下面就详细讲一下具体开发过程。(注:本文的读卡器型号为URF-R330,其他设备原理相同)

建立读卡器SDK引用包,如果有不明白的,可以到设备官网或百度下载设备接口的文档,直接上代码 /// /// common 的摘要说明。 /// public class common { public common() { } public int icdev; // 通讯设备标识符 [DllImport("mwrf32.dll", EntryPoint = "rf_init", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明:初始化串口通讯接口 public static extern int rf_init(Int16 port, int baud); [DllImport("mwrf32.dll", EntryPoint = "rf_exit", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: 关闭通讯口 public static extern Int16 rf_exit(int icdev); [DllImport("mwrf32.dll", EntryPoint = "rf_get_status", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_get_status(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] state); [DllImport("mwrf32.dll", EntryPoint = "rf_beep", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_beep(int icdev, int msec); [DllImport("mwrf32.dll", EntryPoint = "rf_load_key", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_load_key(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff); [DllImport("mwrf32.dll", EntryPoint = "rf_changeb3", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: 修改扇区密码 public static extern Int16 rf_changeb3(int icdev, int _SecNr, [MarshalAs(UnmanagedType.LPArray)]byte[] _KeyA, int _B0, int _B1, int _B2, int _B3, int _Bk, [MarshalAs(UnmanagedType.LPArray)]byte[] _KeyB); [DllImport("mwrf32.dll", EntryPoint = "rf_load_key_hex", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_load_key_hex(int icdev, int mode, int secnr, [MarshalAs(UnmanagedType.LPArray)]byte[] keybuff); [DllImport("mwrf32.dll", EntryPoint = "a_hex", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 a_hex([MarshalAs(UnmanagedType.LPArray)]byte[] asc, [MarshalAs(UnmanagedType.LPArray)]byte[] hex, int len); [DllImport("mwrf32.dll", EntryPoint = "hex_a", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 hex_a([MarshalAs(UnmanagedType.LPArray)]byte[] hex, [MarshalAs(UnmanagedType.LPArray)]byte[] asc, int len); [DllImport("mwrf32.dll", EntryPoint = "rf_reset", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_reset(int icdev, int msec); [DllImport("mwrf32.dll", EntryPoint = "rf_clr_control_bit", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_clr_control_bit(int icdev, int _b); [DllImport("mwrf32.dll", EntryPoint = "rf_set_control_bit", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_set_control_bit(int icdev, int _b); [DllImport("mwrf32.dll", EntryPoint = "rf_disp8", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_disp8(int icdev, short mode, [MarshalAs(UnmanagedType.LPArray)]byte[] disp); [DllImport("mwrf32.dll", EntryPoint = "rf_disp", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_disp(int icdev, short mode, int digit); [DllImport("mwrf32.dll", EntryPoint = "rf_encrypt", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_encrypt([MarshalAs(UnmanagedType.LPArray)]byte[] key, [MarshalAs(UnmanagedType.LPArray)]byte[] ptrsource, int len, [MarshalAs(UnmanagedType.LPArray)]byte[] ptrdest); [DllImport("mwrf32.dll", EntryPoint = "rf_decrypt", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_decrypt([MarshalAs(UnmanagedType.LPArray)]byte[] key, [MarshalAs(UnmanagedType.LPArray)]byte[] ptrsource, int len, [MarshalAs(UnmanagedType.LPArray)]byte[] ptrdest); [DllImport("mwrf32.dll", EntryPoint = "rf_srd_eeprom", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_srd_eeprom(int icdev, int offset, int len, [MarshalAs(UnmanagedType.LPArray)]byte[] databuff); [DllImport("mwrf32.dll", EntryPoint = "rf_swr_eeprom", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_swr_eeprom(int icdev, int offset, int len, [MarshalAs(UnmanagedType.LPArray)]byte[] databuff); [DllImport("mwrf32.dll", EntryPoint = "rf_setport", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_setport(int icdev, byte _byte); [DllImport("mwrf32.dll", EntryPoint = "rf_getport", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_getport(int icdev, out byte _byte); [DllImport("mwrf32.dll", EntryPoint = "rf_gettime", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_gettime(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] time); [DllImport("mwrf32.dll", EntryPoint = "rf_gettime_hex", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_gettime_hex(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] time); [DllImport("mwrf32.dll", EntryPoint = "rf_settime_hex", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_settime_hex(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] time); [DllImport("mwrf32.dll", EntryPoint = "rf_settime", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_settime(int icdev, [MarshalAs(UnmanagedType.LPArray)]byte[] time); [DllImport("mwrf32.dll", EntryPoint = " rf_setbright", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_setbright(int icdev, byte bright); [DllImport("mwrf32.dll", EntryPoint = "rf_ctl_mode", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_ctl_mode(int icdev, int mode); [DllImport("mwrf32.dll", EntryPoint = "rf_disp_mode", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 rf_disp_mode(int icdev, int mode); [DllImport("mwrf32.dll", EntryPoint = "lib_ver", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明: public static extern Int16 lib_ver([MarshalAs(UnmanagedType.LPArray)]byte[] ver); [DllImport("mwrf32.dll", EntryPoint = "rf_halt", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)] //说明:挂起 public static extern Int16 rf_halt(int icdev); 建立多线程方式下读卡类,就是根据上面的SDK封装成可直接使用的多线程读卡类,具体功能请参考代码的注释。 /* *作者:** *时间:2018年6月21日 *说明:多线程方式轮训读取IC卡 *注意:次类只包含IC设备操作内容,具体使用需要参考具体页面(FrmHYK) */ /// /// IC多线程读卡 /// public static class ICTool_Thread { /// /// 通讯设备标识符 /// public static int icdev; /// /// 状态 /// public static Int16 st; public static int sec; /// /// 密码 /// public static string cardpwd = "0516dffd6150"; /// /// 接受刷卡卡号的控件 /// public static Control cardNumberReciver = null; /// /// 链接设备,在FormLoad事件中调用,同时开启多线程 /// public static void initEquipment() { lastCardNumer = ""; st = 0; byte[] ver = new byte[30]; st = common.lib_ver(ver); string sver = System.Text.Encoding.ASCII.GetString(ver); Int16 port = 1; int baud = 9600; icdev = common.rf_init(port, baud); if (icdev > 0) { byte[] status = new byte[30]; st = common.rf_get_status(icdev, status); common.rf_beep(icdev, 10); } else { MessageBox.Show("打开串口失败!"); } } /// /// 断开设备,在FormClosed事件中调用,同时关闭多线程(很重要) /// public static void disposeEquipment() { common.rf_exit(icdev); } /// /// 上次刷卡的卡号,防止卡放在设备上一直处理读卡和蜂鸣,注意在合适的时候清空此变量 /// static string lastCardNumer = ""; /// /// 读卡 /// /// 参数=2 /// public static void readCard(int isec) { while (true) { try { //寻卡 UInt16 tagtype = 0; byte size = 0; uint snr = 0; mifareone.rf_reset(icdev, 3); st = mifareone.rf_request(icdev, 1, out tagtype); if (st != 0) { //throw new Exception("request error!"); continue; } st = mifareone.rf_anticoll(icdev, 0, out snr); if (st != 0) { //throw new Exception("anticoll error!"); continue; } string snrstr = ""; snrstr = snr.ToString("X"); st = mifareone.rf_select(icdev, snr, out size); if (st != 0) { //throw new Exception("select error!"); continue; } sec = isec; //认证 byte[] key1 = new byte[17]; byte[] key2 = new byte[7]; int i = 0; string skey = cardpwd; int keylen = skey.Length; key1 = Encoding.ASCII.GetBytes(skey); common.a_hex(key1, key2, 12); st = common.rf_load_key(icdev, 0, sec, key2); if (st != 0) { //throw new Exception("装载密码失败!"); continue; } st = mifareone.rf_authentication(icdev, 0, sec); if (st != 0) { //throw new Exception("认证失败!"); continue; } //写数据 byte[] data = new byte[16]; byte[] buff = new byte[32]; for (i = 0; i < 16; i++) data[i] = 0; for (i = 0; i < 32; i++) buff[i] = 0; st = mifareone.rf_read(icdev, sec * 4 + 1, data); if (st == 0) { common.hex_a(data, buff, 16); common.rf_halt(icdev); if (cardNumberReciver != null) { string cardNumber = System.Text.Encoding.ASCII.GetString(buff).Replace("F", "").Replace("f", ""); if (lastCardNumer != cardNumber) { cardNumberReciver.Invoke(new ThreadStart(delegate() { common.rf_beep(icdev, 5);                                    cardNumberReciver.Text = cardNumber; lastCardNumer = cardNumber; })); } Thread.Sleep(1000); } } else { //throw new Exception("读数据失败!"); continue; } } catch { } } } }

在页面添加多线程处理 ①增加变量 Thread t; ②FormLoad事件 private void FormXXXX_Load(object sender, EventArgs e) { System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; ICTool_Thread.initEquipment();//初始化IC设备 ICTool_Thread.cardNumberReciver = this.txt_XFKH;//设置接受卡号的控件 t = new Thread(new ThreadStart(readCard));//初始化线程 t.Start();//开启线程 }

/// /// 线程方法 /// void readCard() { ICTool_Thread.readCard(2); } ③FormClosed事件 //关闭线程计时器 private void FormXXXX_FormClosed(object sender, FormClosedEventArgs e) { t.Abort(); ICTool_Thread.disposeEquipment(); }

搞定收工,调试运行便可直接数卡读数了



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有