-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathForm2.cs
More file actions
207 lines (187 loc) · 7.8 KB
/
Copy pathForm2.cs
File metadata and controls
207 lines (187 loc) · 7.8 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace QueueTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int iRetUSB = 0;
AutoResetEvent are = new AutoResetEvent(false);
private void button1_Click(object sender, EventArgs e)
{
jg = (int)(Convert.ToDecimal(textBox1.Text.Trim()) * 1000);
int iPort;
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
break;
}
if (iRetUSB != 1)
{
this.Invoke(new Action(() => { this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + "身份证读卡器初始化失败,请重试!\r\n"); }));
}
else
{
isread = true;
this.Invoke(new Action(() =>
{
this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + "初始化成功,循环读卡开始(0.2秒执行一次)...\r\n");
}));
new Thread(new ThreadStart(ReadIDCard)) { IsBackground = true }.Start();
}
}
bool isread = true;
int jg = 100;
private void ReadIDCard()
{
int time = 0;
while (isread)
{
time++;
int isHaveCard = CVRSDK.CVR_Authenticate();
if (isHaveCard == 1)
{
int readOk = CVRSDK.CVR_Read_Content(4);
if (readOk == 1)
{
#region
byte[] name = new byte[30];
int length = 30;
CVRSDK.GetPeopleName(ref name[0], ref length);
byte[] number = new byte[30];
length = 36;
CVRSDK.GetPeopleIDCode(ref number[0], ref length);
byte[] address = new byte[30];
length = 70;
CVRSDK.GetPeopleAddress(ref address[0], ref length);
var iCard = System.Text.Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim();
var iName = System.Text.Encoding.GetEncoding("GB2312").GetString(name).Replace("\0", "").Trim();
var iAdress = System.Text.Encoding.GetEncoding("GB2312").GetString(address).Replace("\0", "").Trim();
if (iCard != "")
{
int t = time;
new Thread(() =>
{
BeginInvoke(new Action(() =>
{
this.BeginInvoke(new Action(() => { this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + string.Format("身份证读卡成功:本次读卡循环读取了{3}次,证件号码{0} 姓名{1} 地址{2}\r\n", iCard, iName, iAdress, t)); }));
}));
}) { IsBackground = true }.Start();
time = 0;
}
else
{
BeginInvoke(new Action(() =>
{
this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + " 未读取到身份证号码\r\n");
}));
}
#endregion
}
else
{
BeginInvoke(new Action(() =>
{
this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + " 读卡操作失败!请重新放上卡片。\r\n");
}));
}
}
else
{
BeginInvoke(new Action(() =>
{
this.richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss:fff") + " 未放卡或卡片放置不正确,请重新放上卡片。\r\n");
}));
}
Thread.Sleep(jg);
}
}
private void button2_Click(object sender, EventArgs e)
{
isread = false;
try { CVRSDK.CVR_CloseComm(); }
catch { }
}
ShareMemory2 share = new ShareMemory2();
private void button3_Click(object sender, EventArgs e)
{
share.Init();
var p1 = new Person() { idcard = "6227231987026545645160719", name = "测123123123试", address = "123123123地址", sex = "男" };
var bytes1 = FormatterMessageBytes(p1);
var p2 = new Person() { idcard = "622723198702160743434343419", name = "测试", address = "地址" };
var bytes2 = FormatterMessageBytes(p2);
var p3 = new Person() { idcard = "622723198702160719", name = "测试", address = "地址" };
var bytes3 = FormatterMessageBytes(p3);
var p4 = new Person() { idcard = "622723198702160719234234234234", name = "234234234234234234测试", address = "地址" };
var bytes4 = FormatterMessageBytes(p4);
var p5 = new Person() { idcard = "6227231987021607太热太热19", name = "测试", address = "地址" };
var bytes5 = FormatterMessageBytes(p5);
var p6 = new Person() { idcard = "622723198702160719", name = "测的试", address = "地址" };
var bytes6 = FormatterMessageBytes(p6);
share.Write(bytes6, 0, bytes6.Length);
}
byte[] nbyte = new byte[1024 * 10];
private void button4_Click(object sender, EventArgs e)
{
var bytes = new byte[1024 * 10];
share.Read(ref bytes, 0, bytes.Length);
if (BytesCompare(bytes, nbyte))
return;
var p = FormatterBytes(bytes);
if (p != null)
{
MessageBox.Show(p.idcard);
}
}
byte[] FormatterObjectBytes(object obj)
{
if (obj == null)
throw new ArgumentNullException("obj is null");
byte[] buff;
using (var ms = new MemoryStream())
{
IFormatter iFormatter = new BinaryFormatter();
iFormatter.Serialize(ms, obj);
buff = ms.GetBuffer();
}
return buff;
}
private bool BytesCompare(byte[] b1, byte[] b2)
{
if (b1 == null || b2 == null) return false;
if (b1.Length != b2.Length) return false;
return string.Compare(Convert.ToBase64String(b1), Convert.ToBase64String(b2), false) == 0 ? true : false;
}
Person FormatterBytes(byte[] bytes)
{
byte[] buff;
using (var ms = new MemoryStream(bytes))
{
IFormatter iFormatter = new BinaryFormatter();
var obj = iFormatter.Deserialize(ms) as Person;
return obj;
}
}
public byte[] FormatterMessageBytes(Person message)
{
return FormatterObjectBytes(message);
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}