using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); comboBoxPort.SelectedIndex = 0; comboBoxBaud.SelectedIndex = 0; comboBoxType.SelectedIndex = 1; comboBoxBright.SelectedIndex = 14; comboBoxSec.SelectedIndex = 1; comboBoxCardMode.SelectedIndex = 0; textBoxWData.Text = "11223344556677889900aabbccddeeff"; textBoxCmd.Text = "0084000008"; } //变量 public IntPtr icdev=IntPtr.Zero; Int16 st; byte[] snr = new byte[5]; //卡片序列号 Int16 port; //端口号 Int32 baud; //波特率 byte sector; //扇区号 private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e) { port = (Int16)comboBoxPort.SelectedIndex; } private void comboBoxBaud_SelectedIndexChanged(object sender, EventArgs e) { baud = Convert.ToInt32(comboBoxBaud.Text); } private void button1_Click(object sender, EventArgs e) { icdev = Program.rf_init(port, baud); //连接设备 if (icdev.ToInt32() > 0) { listBox1.Items.Add("Com Connect success!"); byte[] status = new byte[30]; st = Program.rf_get_status(icdev, status); //读取硬件版本号 //lbHardVer.Text=System.Text.Encoding.ASCII.GetString(status); listBox1.Items.Add(System.Text.Encoding.Default.GetString(status)); //Program.rf_beep(icdev, 25); } else listBox1.Items.Add("Com Connect failed!"); byte[] key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; byte mode = 0; //加载密码,此密码是用来验证卡片的密码 for (byte sector = 0; sector < 16; sector++) { st = Program.rf_load_key(icdev, mode, sector, key); if (st != 0) { string s1 = Convert.ToString(sector); listBox1.Items.Add(s1 + " sector rf_load_key error!"); } } } private void button1_Click_1(object sender, EventArgs e) { st = Program.rf_card(icdev, (byte)comboBoxCardMode.SelectedIndex, snr);    //寻卡 if (st != 0) { listBox1.Items.Add("rf_card error"); } else { byte[] snr1 = new byte[8]; listBox1.Items.Add("rf_card right!"); Program.hex_a(snr, snr1, 4); //将卡号转换为16进制字符串 listBox1.Items.Add(System.Text.Encoding.Default.GetString(snr1)); } } private void button2_Click(object sender, EventArgs e) { st = Program.rf_exit(icdev); if (st == 0) { listBox1.Items.Add("断开连接!"); } } private void button5_Click(object sender, EventArgs e) { //设置亮度 st = Program.rf_setbright(icdev, (byte)comboBoxBright.SelectedIndex); if (st != 0) { listBox1.Items.Add("设置亮度失败!"); } else { listBox1.Items.Add("设置亮度成功!"); } if (radioButtonComputer.Checked) { st =Program.rf_ctl_mode(icdev, 0); //设置控件模式:0为计算机控制,1为读写器控制 if (st != 0) { listBox1.Items.Add("设置控制模式失败!"); } else { listBox1.Items.Add("当前控制模式: 计算机控制!"); } Int16 dispLen = (Int16)(textBoxComperData.TextLength / 2); byte[] disp_str = Encoding.Default.GetBytes(textBoxComperData.Text); byte[] dispData = new byte[8]; Program.a_hex(disp_str, dispData, dispLen); //先将用户输入的数据转换成16进制 //byte[] dispData = new byte[] { 0x00, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88 }; //受计算机控制时,显示方式由显示数据决定;每个字节的最高位为1表示本位数后的小数点亮,为0表示小数点灭。 Program.rf_disp8(icdev, dispLen, dispData); //显示内容 } else { string strTime = DateTime.Now.ToString("yyMMddHHmmss");//获取系统时间 strTime=strTime.Insert(2, "17"); //加入星期,随便写两个字符 byte[] byteTime = Encoding.Default.GetBytes(strTime); byte[] byteTimeHex = new byte[8]; Program.a_hex(byteTime, byteTimeHex, 14); //转换为7个字节的16进制数据 Program.rf_settime(icdev, byteTimeHex); //设置读写器时间 Program.rf_disp_mode(icdev, (byte)(comboBoxType.SelectedIndex)); //设置显示日期或时间 st = Program.rf_ctl_mode(icdev, 1); //设置控件模式:0为计算机控制,1为读写器控制 if (st != 0) { listBox1.Items.Add("设置控制模式失败!"); } else { listBox1.Items.Add("当前控制模式: 读写器控制!"); } } } private void button6_Click(object sender, EventArgs e) { st = Program.rf_card(icdev, 0, snr);    //寻卡 if (st != 0) { listBox1.Items.Add("rf_card error"); } else { byte[] snr1 = new byte[8]; listBox1.Items.Add("rf_card right!"); Program.hex_a(snr, snr1, 4); listBox1.Items.Add(System.Text.Encoding.Default.GetString(snr1)); } } private void radioButtonComputer_CheckedChanged(object sender, EventArgs e) { if (radioButtonComputer.Checked) { textBoxComperData.Enabled = true; comboBoxType.Enabled = false; } else { textBoxComperData.Enabled = false; comboBoxType.Enabled = true; } } private void button2_Click_1(object sender, EventArgs e) { st = Program.rf_authentication(icdev, 0, sector); //验证密码 if (st != 0) { listBox1.Items.Add("rf_authentication error!"); } else { listBox1.Items.Add("rf_authentication right!"); } } private void comboBoxSec_SelectedIndexChanged(object sender, EventArgs e) { //comboBoxSec选择扇区后,设置此扇区所包含的块号,每个扇区的第四块为密码块,所以comboBoxBlock没有增加 comboBoxBlock.Items.Clear(); sector=(byte)comboBoxSec.SelectedIndex; switch (sector) { case 0: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 1: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 2: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 3: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 4: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 5: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 6: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 7: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 8: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 9: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 10: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 11: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 12: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 13: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 14: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; case 15: comboBoxBlock.Items.Add(sector * 4); comboBoxBlock.Items.Add(sector * 4 + 1); comboBoxBlock.Items.Add(sector * 4 + 2); break; } comboBoxBlock.SelectedIndex = 0; } private void button4_Click(object sender, EventArgs e) { byte[] databuffer = new byte[32]; byte block = Convert.ToByte(comboBoxBlock.Text); st = Program.rf_read_hex(icdev, block, databuffer); //读数据,此函数读出来的是16进制字符串,也就是把每个字节数据的16进制A​S​C​Ⅱ码以字符串形式输出 //st = Program.rf_read(icdev, block, databuffer); if (st != 0) { listBox1.Items.Add("rf_read error! " + st.ToString()); } else { listBox1.Items.Add("rf_read right!"); listBox1.Items.Add(System.Text.Encoding.Default.GetString(databuffer)); } } private void button7_Click(object sender, EventArgs e) { if (textBoxWData.TextLength < 32) { MessageBox.Show("请输入32位长度的数据","error",MessageBoxButtons.OK,MessageBoxIcon.Error); return; } byte block = Convert.ToByte(comboBoxBlock.Text); byte[] data = new byte[32]; data = Encoding.Default.GetBytes(textBoxWData.Text); st = Program.rf_write_hex(icdev, block, data); //写数据,此函数写入的数据长度必须为32位,它会把输入的数据每两位转化为1个字节 //data长度为32位,也要确定是16进制字符串 //st = Program.rf_write(icdev, block, data); if (st != 0) { listBox1.Items.Add("rf_write error! " + st.ToString()); } else { listBox1.Items.Add("rf_write right!"); } /* 改密码 byte[] keya = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; byte[] keyb = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; st = Program.rf_changeb3(icdev, sector, keya, 0, 0, 0, 1, 0, keyb); if (st != 0) { listBox1.Items.Add("rf_changeb3 error!"); listBox1.Items.Add(st.ToString()); } else { listBox1.Items.Add("rf_changeb3 right!"); } * */ } private void button3_Click(object sender, EventArgs e) { byte[] resetData = new byte[256]; byte[] rData = new byte[512]; st = Program.rf_pro_rst(icdev, resetData); if (st != 0) { listBox1.Items.Add("复位失败!"); } else { Program.hex_a(resetData, rData, resetData[0]); //将复位信息转换为ASC字符串存放在rData中 listBox1.Items.Add(System.Text.Encoding.Default.GetString(rData)); } } private void button8_Click(object sender, EventArgs e) { byte[] rev_data=new byte[256]; byte[] rData = new byte[512]; byte cmdLen=(byte)(textBoxCmd.TextLength/2); //发送命令的长度 //byte[] sendCmd = new byte[] { 0, 0, 0, 5, 0, 0x84, 0, 0, 8 }; //第四个字节为发送的命令长度 byte[] sendCmd = new byte[cmdLen + 4]; byte[] strCmd = Encoding.Default.GetBytes(textBoxCmd.Text); //将命令转化为字节 byte[] byteCmd=new byte[256]; Program.a_hex(strCmd, byteCmd, (Int16)strCmd.Length); //将命令转化为16进制数据 sendCmd[3] = (byte)(cmdLen);//第四个字节为发送的命令长度 for (int i = 0; i < cmdLen; i++) { sendCmd[i + 4] = byteCmd[i]; //将命令(byteCmd)复制进要发送的数据中 } st = Program.rf_pro_trn(icdev, sendCmd, rev_data); if (st != 0) { listBox1.Items.Add("发送命令失败!"); } else { byte[] gdData = new byte[256]; Array.Copy(rev_data,4,gdData,0,rev_data[3]);//去掉前面4个字节的无用信息 Program.hex_a(gdData, rData, (short)(rev_data[3]-2)); //去掉CPU卡返回的状态码 listBox1.Items.Add(System.Text.Encoding.Default.GetString(rData)); } } } }