一、需求
通过刷卡实现系统的登录
二、硬件介绍
1、IC卡读写器使用明华科技KRF-35系列(该系列读写器使用Com口通讯,作者使用笔记本电脑,所以又购买了RS232转USB数据线)
2、IC卡型号mifare Std 1k;符合ISO14443 /1/2/3/4 T=CL 协议
三、编程语言
C#
四、代码实现
读写器提供有mwrf32.dll,并且提供了通用函数\设备操作函数\M1卡专用函数等专用库函数。
1、引用通用库函数
[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);
2、引用M1卡专用函数
[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_request", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=false, CallingConvention=CallingConvention.StdCall)] //说明: 返回设备当前状态 public static extern Int16 rf_request(int icdev, int mode, out UInt16 tagtype); [DllImport("mwrf32.dll", EntryPoint="rf_request_std", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=false, CallingConvention=CallingConvention.StdCall)] //说明: 返回设备当前状态 public static extern Int16 rf_request_std(int icdev, int mode, out UInt16 tagtype); [DllImport("mwrf32.dll", EntryPoint="rf_anticoll", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=false, CallingConvention=CallingConvention.StdCall)] //说明: 返回设备当前状态 public static extern Int16 rf_anticoll(int icdev, int bcnt,out uint snr); [DllImport("mwrf32.dll", EntryPoint="rf_select", SetLastError=true, CharSet=CharSet.Auto, ExactSpelling=false, CallingConvention=CallingConvention.StdCall)] //说明: 返回设备当前状态 public static extern Int16 rf_select(int icdev, uint snr,out byte size);
3、编写C#卡片专用操作类
public class CardHelper { public static int IC_DEV_HANDLER; public static string IC_HARD_VER;//硬件版本号 public static string IC_SOFT_VER;//软件版本号 public static bool TestConn(short _port, int _baudRate) { IC_DEV_HANDLER = CardCommon.rf_init(_port, _baudRate); bool rlt = false; if (IC_DEV_HANDLER > 0) { byte[] str = new byte[30]; CardCommon.rf_get_status(IC_DEV_HANDLER, str); //IC_HARD_VER = System.Text.Encoding.ASCII.GetString(str); IC_HARD_VER = System.Text.Encoding.Default.GetString(str); //蜂鸣提示连接成功 CardCommon.rf_beep(IC_DEV_HANDLER, 30); rlt = true; } else rlt = false; return rlt; } public static bool SeekCard(out string snCode) { UInt16 TagType=0; Int16 rlt; uint snr=0; byte size; snCode = string.Empty; if (IC_DEV_HANDLER <= 0) return false; CardMifareOne.rf_reset(IC_DEV_HANDLER, 3); rlt = CardMifareOne.rf_request(IC_DEV_HANDLER, 1, out TagType); if (rlt != 0) return false; rlt = CardMifareOne.rf_anticoll(IC_DEV_HANDLER, 0,out snr); if (rlt != 0) return false; rlt = CardMifareOne.rf_select(IC_DEV_HANDLER, snr, out size); if (rlt != 0) return false; snCode = CommonF.GetNewCardStrByOld(snr.ToString()); //CardCommon.rf_beep(IC_DEV_HANDLER, 10); return true; } }
4、添加时钟控件,时钟更新频率为1000毫秒,时钟事件中添加寻卡代码
//定义程序集变量 bool m_isLoginSuccess=false; //时钟事件添加如下代码 if (!m_isLoginSuccess) { string cardNum; if (CardHelper.SeekCard(out cardNum)) { //卡片操作代码 } }
已有 6852 位网友参与,快来吐槽:
发表评论