【物联网原理与应用】实验三:RFID低频卡实验——门禁系统
目录
一、实验目的
1.认识NEWLab实训平台
2.学习低频RFID卡工作原理
3.认识低频卡读写套件:M3核心模块、LF射频模块、低频卡等板块
4.掌握NEWLab实训平台的低频RFID卡相关硬件电路基本原理
二、实验原理
典型的门禁系统如图所示,包括门禁控制器、读卡器、电控锁以及其它设备。
(1)门禁控制器 :门禁系统的核心部分,相当于计算机的CPU,它负责整个系统输入、输出信息的处理和储存,控制等等。
(2)读卡器:读取卡片中数据的设备。
(3)电控锁 :门禁系统中锁门的执行部件。用户应根据门的材料、出门要求等需求,选取不同的锁具。主要用电磁锁,并且电磁锁断电后是开门的,符合消防要求。
(4)其它设备
出门按钮:按一下打开门的设备,适用于对出门无限制的情况。
门 磁:用于检测门的安全/开关状态等。
电 源:整个系统的供电设备,分为普通和后备式(带蓄电池的)两种。
传输部分:传输部分主要包含电源线和信号线。
三、实验内容及步骤
(1)双击Visual Studio图标,打开软件。
(2)点击“打开项目…”,找到“ 低频卡门禁系统综合实验”工程文件。
(3)点击菜单栏“运行”按钮,工作界面如图所示。
(4)根据实际连接情况,选择串口端口、波特率选择“115200”,数据位“8”,停止位“one”,奇偶校验位“None”,并点击“打开串口”按键,执行状态栏显示串口通信状态,如图所示。
(5)如果需要,请进行下面步骤: 点击【获取卡信息】,获取低频卡信息(选做); 点击【初始化低频卡】,进行初始化低频卡操作(选做); 点击【常规读】,进行常规读块操作(选做); 点击【常规写】,进行常规写块操作(选做); 点击【加密】,进行常规写块操作(选做); 点击【解密】,进行常规写块操作(选做); 点击【保护读】,进行低频卡保护读块操作(选做); 点击【保护写】,进行低频卡保护写读块操作(选做)。
(6)将低频卡靠近LF射频模块,选择【注册界面】页面,进行【获取卡信息】
(7)将低频卡靠近LF射频模块,选择【注册界面】页面,进行【注册】
(8)将低频卡靠近LF射频模块,选择【门禁功能】页面。点击【门禁功能】,如果是注册卡,显示“注册卡,开门!”(8)点击【门禁功能】,如果不是注册卡,显示“非注册卡!”
四、实验结果
五、参考代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace 高频
{
public partial class Form1 : Form
{
public delegate void showReceiveDelegate(string text); //当采用响应模式,应申明一个委托,实现不同线程的控件实验
SerialPort com = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
int com_num = 0;
int timer_num;
string txtName = "RegisterInfo.txt";
TabPage currentPage = null;
/*-------------------------------------------------------------
* status_num状态字
* 1,表示读取信息
--------------------------------------------------------------*/
int status_num=0;//状态字
public Form1()
{
InitializeComponent();
}
//窗体加载
private void Form1_Load(object sender, EventArgs e)
{
cmbPort.Items.Clear();
try
{
cmbPort.Items.AddRange(SerialPort.GetPortNames());
}
catch (Win32Exception ex)
{
MessageBox.Show("没有找到串口,请接上串口后重启软件!" + ex.Message);
}
//串口初始化
if (cmbPort.Items.Count > 0)
{
cmbPort.SelectedIndex = 0;
}
cmbBaudRate.SelectedIndex = 4;
cmbDataBits.SelectedIndex = 0;
cmbStopBits.SelectedIndex = 0;
cmbParity.SelectedIndex = 0;
//定时器初始化
System.Timers.Timer t = new System.Timers.Timer(50);//实例化Timer类,设置间隔时间为1000毫秒 就是1秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout); //到达时间的时候执行事件;
t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件;
timer_num = 0;
//存储数据空间数据初始化
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
comboBox5.SelectedIndex = 0;
}
//private void initTabClickEvent()
//{
// tabControl1.Selecting += tabControl1_Selecting;
//}
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
currentPage = e.TabPage;
}
//串口打开与关闭
private void btnOpen_Click(object sender, EventArgs e)
{
if (btnOpen.Text == "打开串口")
{
try
{
if (!com.IsOpen)
{
com.PortName = cmbPort.Text;
com.BaudRate = int.Parse(cmbBaudRate.Text);
com.DataBits = int.Parse(cmbDataBits.Text);
switch (cmbStopBits.SelectedIndex)
{
case 0:
com.StopBits = StopBits.One; break;
case 1:
com.StopBits = StopBits.Two; break;
case 2:
com.StopBits = StopBits.OnePointFive; break;
case 3:
com.StopBits = StopBits.None; break;
}
switch (cmbParity.SelectedIndex)
{
case 0: com.Parity = Parity.None; break;
case 1: com.Parity = Parity.Odd; break;
case 2: com.Parity = Parity.Even; break;
}
com.Open();//打开串口
}
btnOpen.Text = "关闭串口";
txtStatus.Text = "串口已打开!";
btnInformation.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button10.Enabled = true;
button11.Enabled = true;
button11_Click(null, null);
// 数据接收模式变化时,设置串口的数据接收侦听事件
try
{
com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived); //加载接收事件
}
catch (Exception err)
{
txtStatus.Text = err.ToString();
}
}
catch
{ txtStatus.Text = "串口打开错误或串口不存在!"; }
}
else //关闭串口
try
{
if (com.IsOpen)
com.Close(); //关闭串口
btnOpen.Text = "打开串口";
txtStatus.Text = "串口已关闭!";
btnInformation.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
button11.Enabled = false;
}
catch
{
txtStatus.Text = "串口关闭错误或串口不存在!";
}
}
//--------------------------------------------------------------------------------
//定时器相关设置
public void theout(object source, System.Timers.ElapsedEventArgs e)
{
timer_num++;
if (AppDomain.CurrentDomain != null && this != null)
{
this.BeginInvoke(new TextOption(function1));//invok 委托实现跨线程的调用
}
}
delegate void TextOption();//定义一个委托
void function1()
{
if ((timer_num > 10)&&(com_num>5))
{
com_num = 0;
try
{
int count = com.BytesToRead;
byte[] readBuffer = new byte[count];
com.Read(readBuffer, 0, count);
//String strReceive = Encoding.Default.GetString(readBuffer); //字母、数字、汉字转换为字符串
String strReceive = getStringFromBytes(readBuffer); //转十六进制
this.Invoke(new showReceiveDelegate(doShowReceive), strReceive);
}
catch (Exception err)
{
txtStatus.Text = err.ToString();
}
}
}
//--------------------------------------------------------------------------------
// 响应模式时,串口接收数据事件
private void com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
com_num = com.BytesToRead;
timer_num = 0;
}
//异步线程处理接受的字符,显示在接收的文本框中
public void doShowReceive(string str)
{
var path = AppDomain.CurrentDomain.BaseDirectory + txtName;
txtReceive.Text = str;
//-----------------------------------------------------
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 81 01 08 07 08 50 2A 82 D5 E5 5F 58 C2
//低频卡信息读取
if (status_num == 1)
{
int i;
status_num = 0;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 01 08"))
{
textBox9.Text = txtInformation.Text = str.Substring(i + 21, 23);//截取4个字节
txtStatus.Text = "低频卡信息读取成功!";
break;
}
}
if (i >= 10)
{
txtStatus.Text = "无法获取低频卡信息!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。\n2.卡片被加密了,请使用保护读写块功能,或者解密后再尝试", "无法获取低频卡信息!");
}
}
//-----------------------------------------------------
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 04 01 00 EE 33
//低频卡初始化操作
else if (status_num == 2)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 82 04 01 00"))
{
txtStatus.Text = "低频卡初始化操作成功!";
DecryptState();
break;
}
}
if(i>=5)
{
txtStatus.Text = "无法进行低频卡初始化操作!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。\n2.卡片被加密了,请使用保护读写块功能,或者解密后再尝试", "无法进行低频卡初始化操作!");
//Access_num = status_num; //0成功,9无卡,其他记录步骤
}
}
//------------------------------------------------------------
//发送命令,常规读块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 02 04 00 00 87
//接收FF 55 00 00 83 03 04 00 28 01 17 66 5D
else if (status_num == 3)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 03 04"))
{
txtStatus.Text = "常规读块操作操作成功!";
textBox1.Text = str.Substring(i + 21, 11);//截取4个字节
break;
}
}
if (i >= 5)
{
txtStatus.Text = "常规读块操作未成功!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。\n2.卡片被加密了,请使用保护读写块功能,或者解密后再尝试", "常规读块操作未成功!");
textBox1.Text = "";
}
}
//------------------------------------------------------------
//发送命令,常规写块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 04 05 01 00 00 00 00 47 48
//接收FF 55 00 00 83 04 01 00 23 8E
else if (status_num == 4)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 83 04 01 00"))
{
txtStatus.Text = "常规写块操作操作成功!";
break;
}
}
if (i >= 5)
{
txtStatus.Text = "常规写块操作未成功!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。\n2.卡片被加密了,请使用保护读写块功能,或者解密后再尝试", "常规写块操作未成功!");
textBox1.Text = "";
}
}
//------------------------------------------------------------
//加密解密操作,加密
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 02 05 04 12 34 56 78 91 A3
//接收FF 55 00 00 82 05 01 00 C0 33
else if (status_num == 5)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 82 05 01 00"))
{
txtStatus.Text = "加密操作成功!";
EncryptState();
break;
}
}
if (i >= 5)
{
txtStatus.Text = "加密操作未成功!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。2.卡片是否加密过,是否进行解密?", "加密操作未成功!");
}
}
//------------------------------------------------------------
//加密解密操作,解除密码
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 02 06 04 12 34 56 78 A2 A3
//接收FF 55 00 00 82 06 01 00 9F 33
else if (status_num == 6)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 82 06 01 00"))
{
txtStatus.Text = "解除密码操作成功!";
DecryptState();
break;
}
}
if (i >= 5)
{
txtStatus.Text = "解除密码操作未成功!";
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。2.卡片是否加密过,密码输入是否正确?", "解除密码操作未成功!");
// Access_num = status_num; //0成功,9无卡,其他记录步骤
}
}
//------------------------------------------------------------
//保护读写块操作,保护读块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 05 05 01 12 34 56 78 b3 23
//接收FF 55 00 00 83 05 04 12 34 56 01 B5 3A
else if (status_num == 7)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 05 04"))
{
txtStatus.Text = "保护读块操作成功!";
textBox5.Text = str.Substring(i + 21, 11);//截取4个字节
break;
}
}
if (i >= 5)
{
txtStatus.Text = "保护读块操作未成功!";
}
}
//------------------------------------------------------------
//保护读写块操作,保护写块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 06 09 01 11 22 33 44 12 34 56 78 08 20
//接收FF 55 00 00 83 06 01 00 5F 0E
else if (status_num == 8)
{
int i;
status_num = 0;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 06 01"))
{
txtStatus.Text = "保护写块操作成功!";
break;
}
}
if (i >= 5)
{
txtStatus.Text = "保护写块操作未成功!";
}
}
//-----------------------------------------------------
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 81 01 08 07 08 50 2A 82 D5 E5 5F 58 C2
//低频卡信息读取,注册
else if (status_num == 9)
{
status_num = 0;
var frameHeader = "FF 55 00 00 81 01 08";
var index = str.IndexOf(frameHeader);
if (index >= 0)
{
txtStatus.Text = "低频卡信息读取成功!";
textBox9.Text = str.Substring(index + frameHeader.Length, 23);//截取8个字节,显示卡信息
textBox15.Text += "获取信息,等待注册!";
//----------------------------------------------------
//将信息写入文件
textBox15.Text += "\r\n成功写入,注册成功!";
try
{
string fileText = "";
if (File.Exists(path))
{
fileText = File.ReadAllText(path); //读文件
//-----------------------------------------------------
//检查是否已经注册,已经注册就不再注册
var records = fileText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (records != null && records.Count() > 0)
{
foreach (var record in records)
{
fileText = fileText.Replace(record + "\r\n", "");
}
}
}
//-----------------------------------------------------
fileText += str; //添加内容到字符串str2
fileText += new string('#', 20) + textBox10.Text; //获取姓名
fileText += new string('#', 20) + textBox11.Text; //获取学号
fileText += new string('#', 20) + comboBox5.Text; //获取性别
fileText += "\r\n";
File.WriteAllText(path, fileText);//将字符串str2内容写入str1路径指向的文件
MessageBox.Show("数据保存到" + path, "注册成功!");
}
catch
{
MessageBox.Show("请检查" + path + "文件是否存在", "注册不成功!");
}
//-----------------------------------------------
}
else
{
txtStatus.Text = "无法获取低频卡信息!";
textBox15.Text += "无法获取信息,无法注册";
}
}
//-----------------------------------------------------
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 81 01 08 07 08 50 2A 82 D5 E5 5F 58 C2
//低频卡信息读取,门禁开门
else if (status_num == 10)
{
status_num = 0;
var frameHeader = "FF 55 00 00 81 01 08";
var index = str.IndexOf(frameHeader);
if (index >= 0)
{
txtStatus.Text = "低频卡信息读取成功!";
textBox14.Text = str.Substring(index + frameHeader.Length, 23);//截取4个字节,显示卡信息
textBox15.Text += "获取信息,等待验证!";
//----------------------------------------------------
//从文件读出数据
try
{
if (File.Exists(path))
{
string fileText = File.ReadAllText(path); //读文件,存储到字符串str2
var records = fileText.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (records != null && records.Count() > 0)
{
foreach (var record in records)
{
if (record.Contains(textBox14.Text))
{
textBox15.Text += "\r\n验证成功,是注册卡!";
label20.Text = "注册卡,开门!";
var propertys = record.Split(new string[] { new string('#', 20) }, StringSplitOptions.RemoveEmptyEntries);
textBox13.Text = propertys[1];
textBox12.Text = propertys[2];
textBox16.Text = propertys[3];
return;
}
}
}
textBox15.Text += "\r\n无法通过验证,不是注册卡!";
label20.Text = "非注册卡!";
}
else
{
label20.Text = "非注册卡!";
}
}
catch
{
MessageBox.Show("请检查" + path + "文件是否存在", "门禁不成功!");
}
//-----------------------------------------------
}
else
{
txtStatus.Text = "无法获取低频卡信息!";
label20.Text = "卡有问题!";
}
}
else if (status_num == 11)
{
int i;
status_num = 0;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 01 08"))
{
DecryptState();
CheckInit();
break;
}
}
if (i >= 10)
{
EncryptState();
MessageBox.Show("1.请检查卡片是否已经放到读卡器上了。\n2.卡片已经被加密了,解密后才能开启", "开启常规读写失败!");
}
}
else if (status_num == 12)
{
status_num = 0;
if (str.IndexOf("FF 55 00 00 83 03 04 00 28 01 17") == -1)
{
UnInitState();
MessageBox.Show("未初始化!请到常规读写界面进行初始化操作!", "未初始化");
}
}
}
/// <summary>
/// 检测是否初始化
/// </summary>
private void CheckInit()
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 12;
//-------------0---------1---------2---------3--
//-------------012345678901234567890123456789
String str1 = "FF 55 00 00 03 03 01 00 CF F1";
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
/// <summary>
/// 加密状态
/// </summary>
private void EncryptState()
{
button4.Enabled = false;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
btnInformation.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
button11.Enabled = false;
}
/// <summary>
/// 解密状态
/// </summary>
private void DecryptState()
{
button4.Enabled = true;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
btnInformation.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button10.Enabled = true;
button11.Enabled = true;
}
/// <summary>
/// 解密状态
/// </summary>
private void UnInitState()
{
button1.Enabled = true;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
btnInformation.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
button11.Enabled = false;
}
// 把字节数组转换为十六进制格式的字符串。
// <param name="pByte">要转换的字节数组。</param>
// <returns>返回十六进制格式的字符串。</returns>
public static string getStringFromBytes(byte[] pByte)
{
string str = ""; //定义字符串类型临时变量。
//遍历字节数组,把每个字节转换成十六进制字符串,不足两位前面添“0”,以空格分隔累加到字符串变量里。
for (int i = 0; i < pByte.Length; i++)
str += (pByte[i].ToString("X").PadLeft(2, '0') + " ");
str = str.TrimEnd(' '); //去掉字符串末尾的空格。
return str; //返回字符串临时变量。
}
//<summary>
//把十六进制格式的字符串转换成字节数组。
//</summary>
//<param name="pString">要转换的十六进制格式的字符串</param>
//<returns>返回字节数组。</returns>
public static byte[] getBytesFromString(string pString)
{
string[] str = pString.Split(' '); //把十六进制格式的字符串按空格转换为字符串数组。
byte[] bytes = new byte[str.Length]; //定义字节数组并初始化,长度为字符串数组的长度。
for (int i = 0; i < str.Length; i++) //遍历字符串数组,把每个字符串转换成字节类型赋值给每个字节变量。
bytes[i] = Convert.ToByte(Convert.ToInt32(str[i], 16));
return bytes; //返回字节数组。
}
//C# CRC16校验算法
public static byte[] crc16(byte[] data, int len)
{
byte[] temdata = new byte[2];
int xda, xdapoly;
byte i, j, xdabit;
xda = 0xFFFF;
xdapoly = 0xA001;
for (i = 0; i < data.Length; i++)
{
xda ^= data[i];
for (j = 0; j < 8; j++)
{
xdabit = (byte)(xda & 0x01);
xda >>= 1;
if (xdabit == 1)
xda ^= xdapoly;
}
}
temdata[0] = (byte)(xda & 0xFF);
temdata[1] = (byte)(xda >> 8);
return temdata;
}
//------------------------------------------------------------
//发送命令,获取低频卡信息
//0---------1---------2---------3---------4---------5---------
//012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 01 01 00 50 74
private void btnInformation_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
txtInformation.Text = "";
status_num = 1;
String str1 = "FF 55 00 00 01 01 00 50 74";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
}
//------------------------------------------------------------
//发送命令,低频卡初始化操作
//0---------1---------2---------3---------4---------5---------
//012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 02 04 00 00 87
//FF 55 00 00 82 04 01 00 EE 33
private void button1_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 2;
String str1 = "FF 55 00 00 02 04 00 00 87";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
}
//------------------------------------------------------------
//发送命令,常规读块操作
//0---------1---------2---------3---------4---------5---------
//012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 03 01 00 CF F1
//接收FF 55 00 00 83 03 04 00 28 01 17 66 5D
private void button2_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 3;
//-------------0---------1---------2---------3--
//-------------012345678901234567890123456789
String str1 = "FF 55 00 00 03 03 01 00 CF F1";
str1 = str1.Remove(21, 2);
str1 = str1.Insert(21, comboBox1.Text); //获取块地址
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//发送命令,常规写块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 04 05 01 00 00 00 00 47 48
//接收FF 55 00 00 83 04 01 00 23 8E
private void button3_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 4;
String str1 = "FF 55 00 00 03 04 05 01 00 00 00 00 47 48";
str1 = str1.Remove(21, 2);
str1 = str1.Insert(21, comboBox2.Text); //获取块地址
str1 = str1.Remove(24, 11);
str1 = str1.Insert(24, textBox2.Text); //获取写入字节串
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//加密解密操作,加密
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 02 05 04 12 34 56 78 91 A3
//接收FF 55 00 00 82 05 01 00 C0 33
private void button4_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 5;
String str1 = "FF 55 00 00 02 05 04 12 34 56 78 91 A3";
//---------------------------------------------------------------
str1 = str1.Remove(21, 11); //获取密码
str1 = str1.Insert(21, textBox3.Text);
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//加密解密操作,解密
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 02 06 04 12 34 56 78 A2 A3
//接收FF 55 00 00 82 06 01 00 9F 33
private void button5_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 6;
String str1 = "FF 55 00 00 02 06 04 12 34 56 78 A2 A3";
//---------------------------------------------------------------
str1 = str1.Remove(21, 11); //获取密码
str1 = str1.Insert(21, textBox4.Text);
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//保护读操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 05 05 01 12 34 56 78 b3 23
//接收
private void button6_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 7;
String str1 = "FF 55 00 00 03 05 05 01 12 34 56 78 b3 23";
//---------------------------------------------------------------
str1 = str1.Remove(21, 2);
str1 = str1.Insert(21, comboBox3.Text); //获取块地址
str1 = str1.Remove(24, 11); //获取密码
str1 = str1.Insert(24, textBox7.Text);
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//保护读写块操作,保护写块操作
//----0---------1---------2---------3---------4---------5---------
//----012345678901234567890123456789012345678901234567890123456789
//发送FF 55 00 00 03 06 09 01 11 22 33 44 12 34 56 78 08 20
//接收FF 55 00 00 83 06 01 00 5F 0E
private void button7_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
status_num = 8;
String str1 = "FF 55 00 00 03 06 09 01 11 22 33 44 12 34 56 78 08 20";
//---------------------------------------------------------------
str1 = str1.Remove(21, 2);
str1 = str1.Insert(21, comboBox4.Text); //获取块地址
str1 = str1.Remove(24, 11); //获取密码
str1 = str1.Insert(24, textBox6.Text);
str1 = str1.Remove(36, 11); //获取数据
str1 = str1.Insert(36, textBox8.Text);
byte[] temdata1 = getBytesFromString(str1);//转换字节,准备CRC16校验
//-----------------------------------------------
int num_len1 = 6;//前2个字节FF 55不加入CRC校验
int num_len2 = 6;//后2个字节50 74是校验位
String str2 = str1.Substring(num_len1, str1.Length - num_len1 - num_len2);
byte[] temdata2 = getBytesFromString(str2);
byte[] temdata3 = crc16(temdata2, temdata2.Length);//CRC校验
temdata1[temdata1.Length - 2] = temdata3[1];//填充校验位
temdata1[temdata1.Length - 1] = temdata3[0];
//-----------------------------------------------
com.Write(temdata1, 0, temdata1.Length); //发到串口
txtSend.Text = getStringFromBytes(temdata1);//显示
}
//------------------------------------------------------------
//发送命令,获取低频卡信息
//0---------1---------2---------3---------4---------5---------
//012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 01 01 00 50 74
private void button11_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
txtInformation.Text = "";
status_num = 11;
String str1 = "FF 55 00 00 01 01 00 50 74";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
}
//------------------------------------------------------------
//发送命令,获取低频卡信息
//0---------1---------2---------3---------4---------5---------
//012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 01 01 00 50 74
private void button8_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtReceive.Text = "";
txtInformation.Text = "";
status_num = 1;
String str1 = "FF 55 00 00 01 01 00 50 74";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
}
//综合实验
private void button9_Click(object sender, EventArgs e)
{
String strB1 = textBox10.Text;
String strB2 = textBox11.Text;
if (strB1.Length>0 && strB2.Length == 8)
{
textBox15.Text = "";
//-------------------------------------------------------------
//发送命令,获取低频卡信息,注册
txtSend.Text = "";
txtReceive.Text = "";
txtInformation.Text = "";
status_num = 9;//注册
String str1 = "FF 55 00 00 01 01 00 50 74";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
//-------------------------------------------------------------
}
else
{
MessageBox.Show("姓名不能为空,且学号须为8位", "信息输入错误!");
}
}
//常规读,正确开门
private void button10_Click(object sender, EventArgs e)
{
textBox12.Text = "";
textBox13.Text = "";
textBox14.Text = "";
textBox15.Text = "";
textBox16.Text = "";
//-------------------------------------------------------------
//发送命令,获取低频卡信息,注册
txtSend.Text = "";
txtReceive.Text = "";
txtInformation.Text = "";
status_num = 10;//门禁
String str1 = "FF 55 00 00 01 01 00 50 74";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
//-------------------------------------------------------------
}
private void txtTextChanged(object sender, EventArgs e)
{
var txt = sender as TextBox;
if (txt != null)
{
//12 34 56 78 ff t xx
var text = txt.Text;
var afterReplaceNotNumber = System.Text.RegularExpressions.Regex.Replace(text, @"[^0-9a-fA-F\s]", "");//过滤非十六进制和空白字符
var afterReplaceSpace = System.Text.RegularExpressions.Regex.Replace(afterReplaceNotNumber, @"(\s){2,}", " ");//替换两个以上连续空白字符为一个空白字符
if (afterReplaceSpace != txt.Text)
{
var start = txt.SelectionStart;
txt.Text = afterReplaceSpace;
txt.Select(start, 0);
}
}
}
}
}
原文地址:https://blog.csdn.net/m0_74164458/article/details/143950197
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!