Form1.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. comboBoxPort.SelectedIndex = 0;
  18. comboBoxBaud.SelectedIndex = 0;
  19. comboBoxType.SelectedIndex = 1;
  20. comboBoxBright.SelectedIndex = 14;
  21. comboBoxSec.SelectedIndex = 1;
  22. comboBoxCardMode.SelectedIndex = 0;
  23. textBoxWData.Text = "11223344556677889900aabbccddeeff";
  24. textBoxCmd.Text = "0084000008";
  25. }
  26. //变量
  27. public IntPtr icdev=IntPtr.Zero;
  28. Int16 st;
  29. byte[] snr = new byte[5]; //卡片序列号
  30. Int16 port; //端口号
  31. Int32 baud; //波特率
  32. byte sector; //扇区号
  33. private void comboBoxPort_SelectedIndexChanged(object sender, EventArgs e)
  34. {
  35. port = (Int16)comboBoxPort.SelectedIndex;
  36. }
  37. private void comboBoxBaud_SelectedIndexChanged(object sender, EventArgs e)
  38. {
  39. baud = Convert.ToInt32(comboBoxBaud.Text);
  40. }
  41. private void button1_Click(object sender, EventArgs e)
  42. {
  43. icdev = Program.rf_init(port, baud); //连接设备
  44. if (icdev.ToInt32() > 0)
  45. {
  46. listBox1.Items.Add("Com Connect success!");
  47. byte[] status = new byte[30];
  48. st = Program.rf_get_status(icdev, status); //读取硬件版本号
  49. //lbHardVer.Text=System.Text.Encoding.ASCII.GetString(status);
  50. listBox1.Items.Add(System.Text.Encoding.Default.GetString(status));
  51. //Program.rf_beep(icdev, 25);
  52. }
  53. else
  54. listBox1.Items.Add("Com Connect failed!");
  55. byte[] key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  56. byte mode = 0;
  57. //加载密码,此密码是用来验证卡片的密码
  58. for (byte sector = 0; sector < 16; sector++)
  59. {
  60. st = Program.rf_load_key(icdev, mode, sector, key);
  61. if (st != 0)
  62. {
  63. string s1 = Convert.ToString(sector);
  64. listBox1.Items.Add(s1 + " sector rf_load_key error!");
  65. }
  66. }
  67. }
  68. private void button1_Click_1(object sender, EventArgs e)
  69. {
  70. st = Program.rf_card(icdev, (byte)comboBoxCardMode.SelectedIndex, snr);    //寻卡
  71. if (st != 0)
  72. {
  73. listBox1.Items.Add("rf_card error");
  74. }
  75. else
  76. {
  77. byte[] snr1 = new byte[8];
  78. listBox1.Items.Add("rf_card right!");
  79. Program.hex_a(snr, snr1, 4); //将卡号转换为16进制字符串
  80. listBox1.Items.Add(System.Text.Encoding.Default.GetString(snr1));
  81. }
  82. }
  83. private void button2_Click(object sender, EventArgs e)
  84. {
  85. st = Program.rf_exit(icdev);
  86. if (st == 0)
  87. {
  88. listBox1.Items.Add("断开连接!");
  89. }
  90. }
  91. private void button5_Click(object sender, EventArgs e)
  92. {
  93. //设置亮度
  94. st = Program.rf_setbright(icdev, (byte)comboBoxBright.SelectedIndex);
  95. if (st != 0)
  96. {
  97. listBox1.Items.Add("设置亮度失败!");
  98. }
  99. else
  100. {
  101. listBox1.Items.Add("设置亮度成功!");
  102. }
  103. if (radioButtonComputer.Checked)
  104. {
  105. st =Program.rf_ctl_mode(icdev, 0); //设置控件模式:0为计算机控制,1为读写器控制
  106. if (st != 0)
  107. {
  108. listBox1.Items.Add("设置控制模式失败!");
  109. }
  110. else
  111. {
  112. listBox1.Items.Add("当前控制模式: 计算机控制!");
  113. }
  114. Int16 dispLen = (Int16)(textBoxComperData.TextLength / 2);
  115. byte[] disp_str = Encoding.Default.GetBytes(textBoxComperData.Text);
  116. byte[] dispData = new byte[8];
  117. Program.a_hex(disp_str, dispData, dispLen); //先将用户输入的数据转换成16进制
  118. //byte[] dispData = new byte[] { 0x00, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88 };
  119. //受计算机控制时,显示方式由显示数据决定;每个字节的最高位为1表示本位数后的小数点亮,为0表示小数点灭。
  120. Program.rf_disp8(icdev, dispLen, dispData); //显示内容
  121. }
  122. else
  123. {
  124. string strTime = DateTime.Now.ToString("yyMMddHHmmss");//获取系统时间
  125. strTime=strTime.Insert(2, "17"); //加入星期,随便写两个字符
  126. byte[] byteTime = Encoding.Default.GetBytes(strTime);
  127. byte[] byteTimeHex = new byte[8];
  128. Program.a_hex(byteTime, byteTimeHex, 14); //转换为7个字节的16进制数据
  129. Program.rf_settime(icdev, byteTimeHex); //设置读写器时间
  130. Program.rf_disp_mode(icdev, (byte)(comboBoxType.SelectedIndex)); //设置显示日期或时间
  131. st = Program.rf_ctl_mode(icdev, 1); //设置控件模式:0为计算机控制,1为读写器控制
  132. if (st != 0)
  133. {
  134. listBox1.Items.Add("设置控制模式失败!");
  135. }
  136. else
  137. {
  138. listBox1.Items.Add("当前控制模式: 读写器控制!");
  139. }
  140. }
  141. }
  142. private void button6_Click(object sender, EventArgs e)
  143. {
  144. st = Program.rf_card(icdev, 0, snr);    //寻卡
  145. if (st != 0)
  146. {
  147. listBox1.Items.Add("rf_card error");
  148. }
  149. else
  150. {
  151. byte[] snr1 = new byte[8];
  152. listBox1.Items.Add("rf_card right!");
  153. Program.hex_a(snr, snr1, 4);
  154. listBox1.Items.Add(System.Text.Encoding.Default.GetString(snr1));
  155. }
  156. }
  157. private void radioButtonComputer_CheckedChanged(object sender, EventArgs e)
  158. {
  159. if (radioButtonComputer.Checked)
  160. {
  161. textBoxComperData.Enabled = true;
  162. comboBoxType.Enabled = false;
  163. }
  164. else
  165. {
  166. textBoxComperData.Enabled = false;
  167. comboBoxType.Enabled = true;
  168. }
  169. }
  170. private void button2_Click_1(object sender, EventArgs e)
  171. {
  172. st = Program.rf_authentication(icdev, 0, sector); //验证密码
  173. if (st != 0)
  174. {
  175. listBox1.Items.Add("rf_authentication error!");
  176. }
  177. else
  178. {
  179. listBox1.Items.Add("rf_authentication right!");
  180. }
  181. }
  182. private void comboBoxSec_SelectedIndexChanged(object sender, EventArgs e)
  183. {
  184. //comboBoxSec选择扇区后,设置此扇区所包含的块号,每个扇区的第四块为密码块,所以comboBoxBlock没有增加
  185. comboBoxBlock.Items.Clear();
  186. sector=(byte)comboBoxSec.SelectedIndex;
  187. switch (sector)
  188. {
  189. case 0:
  190. comboBoxBlock.Items.Add(sector * 4);
  191. comboBoxBlock.Items.Add(sector * 4 + 1);
  192. comboBoxBlock.Items.Add(sector * 4 + 2);
  193. break;
  194. case 1:
  195. comboBoxBlock.Items.Add(sector * 4);
  196. comboBoxBlock.Items.Add(sector * 4 + 1);
  197. comboBoxBlock.Items.Add(sector * 4 + 2);
  198. break;
  199. case 2:
  200. comboBoxBlock.Items.Add(sector * 4);
  201. comboBoxBlock.Items.Add(sector * 4 + 1);
  202. comboBoxBlock.Items.Add(sector * 4 + 2);
  203. break;
  204. case 3:
  205. comboBoxBlock.Items.Add(sector * 4);
  206. comboBoxBlock.Items.Add(sector * 4 + 1);
  207. comboBoxBlock.Items.Add(sector * 4 + 2);
  208. break;
  209. case 4:
  210. comboBoxBlock.Items.Add(sector * 4);
  211. comboBoxBlock.Items.Add(sector * 4 + 1);
  212. comboBoxBlock.Items.Add(sector * 4 + 2);
  213. break;
  214. case 5:
  215. comboBoxBlock.Items.Add(sector * 4);
  216. comboBoxBlock.Items.Add(sector * 4 + 1);
  217. comboBoxBlock.Items.Add(sector * 4 + 2);
  218. break;
  219. case 6:
  220. comboBoxBlock.Items.Add(sector * 4);
  221. comboBoxBlock.Items.Add(sector * 4 + 1);
  222. comboBoxBlock.Items.Add(sector * 4 + 2);
  223. break;
  224. case 7:
  225. comboBoxBlock.Items.Add(sector * 4);
  226. comboBoxBlock.Items.Add(sector * 4 + 1);
  227. comboBoxBlock.Items.Add(sector * 4 + 2);
  228. break;
  229. case 8:
  230. comboBoxBlock.Items.Add(sector * 4);
  231. comboBoxBlock.Items.Add(sector * 4 + 1);
  232. comboBoxBlock.Items.Add(sector * 4 + 2);
  233. break;
  234. case 9:
  235. comboBoxBlock.Items.Add(sector * 4);
  236. comboBoxBlock.Items.Add(sector * 4 + 1);
  237. comboBoxBlock.Items.Add(sector * 4 + 2);
  238. break;
  239. case 10:
  240. comboBoxBlock.Items.Add(sector * 4);
  241. comboBoxBlock.Items.Add(sector * 4 + 1);
  242. comboBoxBlock.Items.Add(sector * 4 + 2);
  243. break;
  244. case 11:
  245. comboBoxBlock.Items.Add(sector * 4);
  246. comboBoxBlock.Items.Add(sector * 4 + 1);
  247. comboBoxBlock.Items.Add(sector * 4 + 2);
  248. break;
  249. case 12:
  250. comboBoxBlock.Items.Add(sector * 4);
  251. comboBoxBlock.Items.Add(sector * 4 + 1);
  252. comboBoxBlock.Items.Add(sector * 4 + 2);
  253. break;
  254. case 13:
  255. comboBoxBlock.Items.Add(sector * 4);
  256. comboBoxBlock.Items.Add(sector * 4 + 1);
  257. comboBoxBlock.Items.Add(sector * 4 + 2);
  258. break;
  259. case 14:
  260. comboBoxBlock.Items.Add(sector * 4);
  261. comboBoxBlock.Items.Add(sector * 4 + 1);
  262. comboBoxBlock.Items.Add(sector * 4 + 2);
  263. break;
  264. case 15:
  265. comboBoxBlock.Items.Add(sector * 4);
  266. comboBoxBlock.Items.Add(sector * 4 + 1);
  267. comboBoxBlock.Items.Add(sector * 4 + 2);
  268. break;
  269. }
  270. comboBoxBlock.SelectedIndex = 0;
  271. }
  272. private void button4_Click(object sender, EventArgs e)
  273. {
  274. byte[] databuffer = new byte[32];
  275. byte block = Convert.ToByte(comboBoxBlock.Text);
  276. st = Program.rf_read_hex(icdev, block, databuffer); //读数据,此函数读出来的是16进制字符串,也就是把每个字节数据的16进制A​S​C​Ⅱ码以字符串形式输出
  277. //st = Program.rf_read(icdev, block, databuffer);
  278. if (st != 0)
  279. {
  280. listBox1.Items.Add("rf_read error! " + st.ToString());
  281. }
  282. else
  283. {
  284. listBox1.Items.Add("rf_read right!");
  285. listBox1.Items.Add(System.Text.Encoding.Default.GetString(databuffer));
  286. }
  287. }
  288. private void button7_Click(object sender, EventArgs e)
  289. {
  290. if (textBoxWData.TextLength < 32)
  291. {
  292. MessageBox.Show("请输入32位长度的数据","error",MessageBoxButtons.OK,MessageBoxIcon.Error);
  293. return;
  294. }
  295. byte block = Convert.ToByte(comboBoxBlock.Text);
  296. byte[] data = new byte[32];
  297. data = Encoding.Default.GetBytes(textBoxWData.Text);
  298. st = Program.rf_write_hex(icdev, block, data); //写数据,此函数写入的数据长度必须为32位,它会把输入的数据每两位转化为1个字节
  299. //data长度为32位,也要确定是16进制字符串
  300. //st = Program.rf_write(icdev, block, data);
  301. if (st != 0)
  302. {
  303. listBox1.Items.Add("rf_write error! " + st.ToString());
  304. }
  305. else
  306. {
  307. listBox1.Items.Add("rf_write right!");
  308. }
  309. /* 改密码
  310. byte[] keya = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  311. byte[] keyb = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  312. st = Program.rf_changeb3(icdev, sector, keya, 0, 0, 0, 1, 0, keyb);
  313. if (st != 0)
  314. {
  315. listBox1.Items.Add("rf_changeb3 error!");
  316. listBox1.Items.Add(st.ToString());
  317. }
  318. else
  319. {
  320. listBox1.Items.Add("rf_changeb3 right!");
  321. }
  322. * */
  323. }
  324. private void button3_Click(object sender, EventArgs e)
  325. {
  326. byte[] resetData = new byte[256];
  327. byte[] rData = new byte[512];
  328. st = Program.rf_pro_rst(icdev, resetData);
  329. if (st != 0)
  330. {
  331. listBox1.Items.Add("复位失败!");
  332. }
  333. else
  334. {
  335. Program.hex_a(resetData, rData, resetData[0]); //将复位信息转换为ASC字符串存放在rData中
  336. listBox1.Items.Add(System.Text.Encoding.Default.GetString(rData));
  337. }
  338. }
  339. private void button8_Click(object sender, EventArgs e)
  340. {
  341. byte[] rev_data=new byte[256];
  342. byte[] rData = new byte[512];
  343. byte cmdLen=(byte)(textBoxCmd.TextLength/2); //发送命令的长度
  344. //byte[] sendCmd = new byte[] { 0, 0, 0, 5, 0, 0x84, 0, 0, 8 }; //第四个字节为发送的命令长度
  345. byte[] sendCmd = new byte[cmdLen + 4];
  346. byte[] strCmd = Encoding.Default.GetBytes(textBoxCmd.Text); //将命令转化为字节
  347. byte[] byteCmd=new byte[256];
  348. Program.a_hex(strCmd, byteCmd, (Int16)strCmd.Length); //将命令转化为16进制数据
  349. sendCmd[3] = (byte)(cmdLen);//第四个字节为发送的命令长度
  350. for (int i = 0; i < cmdLen; i++)
  351. {
  352. sendCmd[i + 4] = byteCmd[i]; //将命令(byteCmd)复制进要发送的数据中
  353. }
  354. st = Program.rf_pro_trn(icdev, sendCmd, rev_data);
  355. if (st != 0)
  356. {
  357. listBox1.Items.Add("发送命令失败!");
  358. }
  359. else
  360. {
  361. byte[] gdData = new byte[256];
  362. Array.Copy(rev_data,4,gdData,0,rev_data[3]);//去掉前面4个字节的无用信息
  363. Program.hex_a(gdData, rData, (short)(rev_data[3]-2)); //去掉CPU卡返回的状态码
  364. listBox1.Items.Add(System.Text.Encoding.Default.GetString(rData));
  365. }
  366. }
  367. }
  368. }