//写卡线程
private void PortWriteThread(object state)
{
byte[] bSend;
byte verify;
Array.Clear(dtResvice, 0, dtResvice.Length);
//关卡
bSend = new byte[8] { 0x3c, 0x04, 0x01, 0x68, 0x00, 0x00, 0x51, 0x0d };
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
Thread.Sleep(50);
Close();
Open();
//寻卡
Array.Clear(dtResvice, 0, dtResvice.Length);
bSend = new byte[8] { 0x3c, 0x04, 0x01, 0x70, 0x01, 0x00, 0x00, 0x0d };
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
Thread.Sleep(100);
verify = dtResvice[0];
for (int i = 1; i < ipos - 2; i++)
{
verify = (byte)(verify ^ dtResvice[i]);
}
if (verify == dtResvice[ipos - 2] && dtResvice[ipos - 1] == 0x0d)
{
if (dtResvice[0] == 0x3c)
if (dtResvice[1] == 0x05)
{
dtWriteCard[0] = dtResvice[2];
dtWriteCard[1] = dtResvice[3];
dtWriteCard[2] = dtResvice[4];
dtWriteCard[3] = dtResvice[5];
}
else
{
iMutex--;
threadOvered.Set();
return;
}
}
else
{
iMutex--;
threadOvered.Set();
return;
}
if (!(dtReadCard[0] == dtWriteCard[0] &&
dtReadCard[0] == dtWriteCard[0] &&
dtReadCard[0] == dtWriteCard[0] &&
dtReadCard[0] == dtWriteCard[0]))
{
}
Thread.Sleep(50);
Close();
Open();
Array.Clear(dtResvice, 0, dtResvice.Length);
//密码验证 3C 08 01 6A 00 01 四字节卡号 BCC 0D 密码认证
bSend = new byte[12] { 0x3c, 0x08, 0x01, 0x6a, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d };
bSend[6] = dtReadCard[0];
bSend[7] = dtReadCard[1];
bSend[8] = dtReadCard[2];
bSend[9] = dtReadCard[3];
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
verify = dtResvice[0];
for (int i = 1; i < ipos - 2; i++)
{
verify = (byte)(verify ^ dtResvice[i]);
}
if (verify == dtResvice[ipos - 2] && dtResvice[ipos - 1] == 0x0d)
{
if (dtResvice[0] == 0x3c)
if (dtResvice[1] != 0x02)
{
iMutex--;
threadOvered.Set();
return;
}
}
else
{
iMutex--;
threadOvered.Set();
return;
}
Thread.Sleep(50);
Close();
Open();
Array.Clear(dtResvice, 0, dtResvice.Length);
//写数据 3C 13 01 67 00({jd1}块号) DATA0~DATA15 BCC 0D
bSend = new byte[23] { 0x3c, 0x13, 0x01, 0x67, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d };
Buffer.BlockCopy(dtData, 0, bSend, 5, 16);
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
verify = dtResvice[0];
for (int i = 1; i < ipos - 2; i++)
{
verify = (byte)(verify ^ dtResvice[i]);
}
if (verify == dtResvice[ipos - 2] && dtResvice[ipos - 1] == 0x0d)
{
if (dtResvice[0] == 0x3c)
if (dtResvice[1] == 0x02)
{
bDataSuc = true;
}
else
{
iMutex--;
threadOvered.Set();
return;
}
}
Thread.Sleep(50);
Close();
Open();
Array.Clear(dtResvice, 0, dtResvice.Length);
//关卡 3C 04 01 68 00 00 51 0D关闭卡
bSend = new byte[8] { 0x3c, 0x04, 0x01, 0x68, 0x00, 0x00, 0x51, 0x0d };
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
Thread.Sleep(50);
Close();
Open();
Array.Clear(dtResvice, 0, dtResvice.Length);
//发光管亮一次:3C 03 01 6D 55(TIME) BCC 0D
bSend = new byte[7] { 0x3c, 0x03, 0x01, 0x6d, 0x0a, 0x00, 0x0d };
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
Thread.Sleep(50);
Close();
Open();
Array.Clear(dtResvice, 0, dtResvice.Length);
//蜂鸣器响一声:3C 03 01 6B 55(TIME) BCC 0D
bSend = new byte[7] { 0x3c, 0x03, 0x01, 0x6b, 0x0a, 0x00, 0x0d };
verify = bSend[0];
for (int i = 1; i < bSend.Length - 2; i++)
{
verify = (byte)(verify ^ bSend[i]);
}
bSend[bSend.Length - 2] = verify;
ipos = 0;
portDataGet.Reset();
sp.Write(bSend, 0, bSend.Length);
portDataGet.WaitOne();
Thread.Sleep(50);
iMutex--;
threadOvered.Set();
if (HaveWriteData != null)
{
HaveWriteData();
}
Close();
Open();
}
}
}
////界面代码
private void Form1_Load(object sender, EventArgs e)
{
ic = new ICCard("COM3", 9600);
ic.HaveReadData += GetReadData;
ic.HaveWriteData += GetWriteData;
ic.Open();
timer1.Enabled = true;
panel1.Visible = false;
}
private void GetReadData()
{
this.Invoke(new EventHandler(ShowInfo1));
}
private void GetWriteData()
{
this.Invoke(new EventHandler(ShowInfo2));
}
private void ShowInfo1(object sender, EventArgs e)
{
timer1.Enabled = false;
panel1.Visible = true;
textBox3.Focus();
}
private void ShowInfo2(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
textBox2.Text = string.Empty;
if (textBox3.Text == ic.pwdsData)
{
textBox1.Text = ic.sData;
textBox2.Text = ic.idsData;
timer1.Enabled = true;
panel1.Visible = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ic.ReadData();
}
private void button2_Click(object sender, EventArgs e)
{
ic.Open();
ic.dtData[6] = 0x55;
ic.WriteData();
}