本文原创,转载请注明出处!
我们使用IC读卡器时,一般可分为手动读卡和自动监听读卡两种方式。
其中手动读卡根据IC设备厂商提供的Demo修改一下便可实现,相对简单,本文不做过多介绍。
而自动监听读卡,看起来效果要好的多,用户使用起来也要方便一下。其实现的逻辑大家应该都知道,就是通过开一个多线程来轮训IC卡读卡的结果,下面就详细讲一下具体开发过程。(注:本文的读卡器型号为URF-R330,其他设备原理相同)
- 建立读卡器SDK引用包,如果有不明白的,可以到设备官网或百度下载设备接口的文档,直接上代码
/// <summary>
/// common 的摘要说明。
/// </summary>
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 = CallingConvent