private void button4_Click(object sender, EventArgs e) { if (!mt.isOpen()) { println("串口未打开"); return; } string cardId = String.Empty; try { string card = mt.write(MessageDefine.readCard, 9, 500); cardId = card.Substring(8, 12); println("卡片内码:" + cardId); } catch (Exception ex) { Console.WriteLine(ex.Message); println("读卡失败"); return; } try { string isPasswordRight = mt.write(MessageDefine.loadPassword(comboBox2.Text), 6, 500); if (!isPasswordRight.Substring(9, 2).Equals("59")) { println("验证密码失败:" + isPasswordRight); return; } } catch (Exception ex) { Console.WriteLine(ex.Message); println("加载密码失败"); return; } if (checkBox1.Checked) { updatePassword(checkBox1.Text, comboBox2.Text, comboBox3.Text, cardId); } if (checkBox2.Checked) { updatePassword(checkBox2.Text, comboBox2.Text, comboBox3.Text, cardId); } if (checkBox3.Checked) { updatePassword(checkBox3.Text, comboBox2.Text, comboBox3.Text, cardId); } if (checkBox4.Checked) { updatePassword(checkBox4.Text, comboBox2.Text, comboBox3.Text, cardId); } }
private void updatePassword(string block, string oldPassword, string newPassword, string cardId) { try { string card = mt.write(MessageDefine.modifyPassword(block, oldPassword, newPassword), 9, 500); if (card.Substring(8, 12).Equals(cardId)) { println("修改扇区" + block + "密码成功"); return; } println("修改扇区" + block + "密码失败:" + card); } catch (Exception ex) { Console.WriteLine(ex.Message); println("修改扇区" + block + "密码失败"); } }