0%

中控CR10MW发卡器开发 (C#)

中控CR10MW提供的两个DLL库, mi.dll, USB.dll, 这两个需要用来作为调用发卡器的控制接口.
mi.dll是开发需要调用的接口, USB.dll是前者依赖的DLL.
mi.dll提供的API各种函数 (这里用C# 表示)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_OpenComm", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern long OpenComm(ref byte comm, long nBoudrate);

[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_CloseComm", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int CloseComm(long handle);


[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_PCDRead", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_PCDRead(int handle, int deviceAddr, byte mode, byte blk_Addr, byte Num_blk, ref byte snr, ref byte Buffer);



[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_PCDWrite", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_PCDWrite(int handle, int deviceAddr, byte mode, byte blk_Addr, byte Num_blk, ref byte snr, ref byte Buffer);

//设置通讯地址
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_SetDeviceAddress", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_SetDeviceAddress(int handle, int deviceAddr, byte new_Addr, ref byte Buffer);

//计算机的比特率
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_SetBandrate", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_SetBandrate(int handle, int deviceAddr, byte newBaud, ref byte Buffer);



//设置灯的工作状态,包括,灯亮的周期以及循环的次数
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_ControlLED", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_ControlLED(int handle, int deviceAddr, byte freq, byte duration, ref byte Buffer);


//设置灯的工作状态,包括,灯亮的周期以及循环的次数
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_ControlBuzzer", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_ControlBuzzer(int handle, int deviceAddr, byte freq, byte duration, ref byte Buffer);

//读取由厂家预设的1个字节的读卡器地址和8个字节序列号
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_GetSerNum", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_GetSerNum(int handle, int deviceAddr, ref byte Buffer);

//读取由厂家预设的1个字节的读卡器地址和8个字节序列号
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_SetSerNum", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_SetSerNum(int handle, int deviceAddr, ref byte newvalue, ref byte Buffer);


//自动读取卡Ultralight的卡号
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "UL_Request", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int UL_Request(int handle, int deviceAddr, byte mode, ref byte snr);

//选择卡,使卡进入被中断的状态…
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "MF_Halt", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int MF_Halt(int handle, int deviceAddr);
//读取卡中指定扇区的数据
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "UL_HLRead", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int UL_HLRead(int handle, int deviceAddr, byte mode, byte blk_Addr, ref byte snr, ref byte Buffer);

//写取卡中指定扇区的数据
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "UL_HLWrite", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int UL_HLWrite(int handle, int deviceAddr, byte mode, byte blk_Addr, ref byte snr, ref byte Buffer);

//写取卡中指定扇区的数据
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "ISO15693_Inventory", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int ISO15693_Inventory(int handle, int deviceAddr, ref byte snr, ref byte Buffer);

//读取数据
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_ISO15693Read", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_ISO15693Read(int handle, int deviceAddr, byte flags, int blk_Addr, int Num_blk, ref byte uid, ref byte Buffer);

//写数据
[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_ISO15693Write", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_ISO15693Write(int handle, int deviceAddr, byte flags, int blk_Addr, int Num_blk, ref byte uid, ref byte Buffer);


[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_PCDInitVal", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_PCDInitVal(int handle, int deviceAddr, byte mode, byte Num_blk, ref byte snr, ref byte Buffer);

[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_PCDDec", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_PCDDec(int handle, int deviceAddr, byte mode, byte Num_blk, ref byte snr, ref byte Buffer);

[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "API_PCDInc", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int API_PCDInc(int handle, int deviceAddr, byte mode, byte Num_blk, ref byte snr, ref byte Buffer);


[System.Runtime.InteropServices.DllImport("mi.dll", EntryPoint = "GET_SNR", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
public static extern int GET_SNR(int handle, int deviceAddr, byte mode, byte half, ref byte snr, ref byte Buffer);

读取卡号的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int ret = 0;
int blk_Addr = 0;
int devAddr = 0;
byte mode = 0;
byte Num_blk = 0;

byte[] asnr = new byte[21];
byte[] aRecvBuffer = new byte[2049];

mode = 0x0;
//设备地址,如果只有一个
devAddr = Convert.ToInt32(myVal("00"));
//读取开始地址
blk_Addr = 0;
int hComm = 0;

//读取块数
Num_blk = 1;
//一个指针,传递的是六个字节的密钥
ret = Convert.ToInt32(hexToBin("FF FF FF FF FF FF", ref asnr));
ret = InterviewCardReader.API_PCDRead(0, devAddr, mode, (byte)blk_Addr, Num_blk, ref asnr[0], ref aRecvBuffer[0]);
uint cardNum = BitConverter.ToUInt32(asnr, 0);
MessageBox.Show("卡号: " + cardNum);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int myVal(string str)
{
int retVal = 0;

if (str.Length == 1)
{
if (string.CompareOrdinal(str, "0") >= 0 && string.CompareOrdinal(str, "9") <= 0)
{
retVal = Convert.ToInt32(NumericHelper.Val(str));
}
else if (string.CompareOrdinal(str, "a") >= 0 && string.CompareOrdinal(str, "z") <= 0)
{
retVal = Microsoft.VisualBasic.Strings.Asc(str) - Microsoft.VisualBasic.Strings.Asc("a") + 10;
}
else if (string.CompareOrdinal(str, "A") >= 0 && string.CompareOrdinal(str, "Z") <= 0)
{
retVal = Microsoft.VisualBasic.Strings.Asc(str) - Microsoft.VisualBasic.Strings.Asc("A") + 10;
}
}

return retVal;
}
隐藏
推进创文常态化 共建文明襄阳城 · 🄽🄾5️⃣9️⃣7️⃣2️⃣ · 做文明襄阳人 建文明襄阳城 凝聚文明正能量 筑梦千年古襄阳 · 🄽🄾5️⃣9️⃣7️⃣2️⃣ · 创建全国文明城市 加快建设汉江流域中心城市 · 🄽🄾5️⃣9️⃣7️⃣2️⃣ · 讲文明 树新风 · 🄽🄾5️⃣9️⃣7️⃣2️⃣ · 用微笑融化陌生 用文明美化襄阳 · 🄽🄾5️⃣9️⃣7️⃣2️⃣ · 争当文明使者 播撒文明新风