bush1 = new SolidBrush(Color.Red);
bush2 = new SolidBrush(Color.Green);
bush3 = new SolidBrush(Color.Yellow);
bush4 = new SolidBrush(Color.Black);
}
//窗体加载
private void Form1_Load(object sender, EventArgs e)
{
//串口初始化
cmbPort.SelectedIndex = 2;
cmbBaudRate.SelectedIndex = 4;
cmbDataBits.SelectedIndex = 0;
cmbStopBits.SelectedIndex = 0;
cmbParity.SelectedIndex = 0;
cmbSector.SelectedIndex = 1;
//定时器初始化
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 = 1;
comboBox2.SelectedIndex = 1;
comboBox3.SelectedIndex = 1;
}
//串口打开与关闭
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;
btnClear.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;
// 数据接收模式变化时,设置串口的数据接收侦听事件
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;
btnClear.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++;
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);
//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 03 08 E3 64 41 21 E3 64 41 00 C7 DD
//卡信息
if (status_num == 1)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 81 03 08”))
{
textBox9.Text = txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
txtStatus.Text = “低频卡信息获取成功!”;
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法获取卡信息!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 02 01 00 1C B3
//关闭天线
if (status_num == 2)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals(“FF 55 00 00 82 02 01 00”))
{
txtStatus.Text = “关闭天线成功!”;
g1.FillEllipse(bush4, 0, 0, 60, 60);//黑色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法关闭天线!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 03 01 00 E0 B2
//打开天线
if (status_num == 3)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals(“FF 55 00 00 82 03 01 00”))
{
txtStatus.Text = “打开天线成功!”;
g1.FillEllipse(bush1, 0, 0, 60, 60);//红色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法打开天线!”;
}
}
//发送命令,激活高频卡
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 00 04 E3 64 41 21 68 6B
//打开天线
if (status_num == 4)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 82 00 04”))
{
txtStatus.Text = “激活高频卡成功!”;
g2.FillEllipse(bush1, 0, 0, 60, 60);//红色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法激活高频卡!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 09 01 00 7A 71
//发送命令,高频卡HALT
if (status_num == 5)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals(“FF 55 00 00 82 09 01 00”))
{
txtStatus.Text = “高频卡HALT成功!”;
g2.FillEllipse(bush4, 0, 0, 60, 60);//黑色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法HALT高频卡!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 80 00 00 E8 25
//发送命令,禁止CRC16校验
if (status_num == 6)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 80 00 00”))
{
txtStatus.Text = “禁止CRC16校验成功!”;
g3.FillEllipse(bush4, 0, 0, 60, 60);//黑色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法进行禁止CRC16校验!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 80 01 00 78 24
//发送命令,使能CRC16校验
if (status_num == 7)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 80 01 00”))
{
txtStatus.Text = “使能CRC16校验成功!”;
g3.FillEllipse(bush1, 0, 0, 60, 60);//黑色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法进行使能CRC16校验!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 85 02 01 00 2F C7
//发送命令,高频卡密码认证
if (status_num == 8)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals(“FF 55 00 00 85 02 01 00”))
{
txtStatus.Text = “高频卡密码认证成功!”;
g2.FillEllipse(bush4, 0, 0, 60, 60);//黑色
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法进行高频卡密码认证!”;
}
}
//0---------1---------2---------3---------4---------5---------6---------7---------
//01234567890123456789012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 83 01 10 22 33 44 55 11 11 11 11 11 11 11 11 11 11 11 11 CB 62
//数据读取
if (status_num == 9)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 83 01 10”))
{
textBox1.Text = str.Substring(i + 21, 47);//截取16个字节
txtStatus.Text = “数据读取成功!”;
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法读取数据!”;
}
}
//0---------1---------2---------3---------4---------5---------6---------7---------
//01234567890123456789012345678901234567890123456789012345678901234567890123456789
//FF 55 00 00 83 02 01 00 09 0F
//数据写入
if (status_num == 10)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals(“FF 55 00 00 83 02 01 00”))
{
txtStatus.Text = “数据写入成功!”;
break;
}
}
if (i >= 5)
{
txtStatus.Text = “无法写入数据!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 81 03 08 E3 64 41 21 E3 64 41 00 C7 DD
//进入停车场
if (status_num == 11)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 81 03 08”))
{
txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
txtStatus.Text = “读卡成功,进入停车场,开始记录时间!”;
now1 = DateTime.Now;
textBox3.Text = now1.ToString(“yyyy-MM-dd HH:mm:ss”);//入场时间
//金额计算,显示余额
textBox4.Text = money.ToString();//显示余额
//--------------------------------------------------------------------------------------
//将信息写入文件
var path1 = AppDomain.CurrentDomain.BaseDirectory + @“data.txt”;
if (!File.Exists(path))
{
File.Create(path);
}
string str2 = File.ReadAllText(path); //读文件,存储到字符串str2
int j;
for (j = 0; j < str2.Length - 25; j++) //查找是否已经入场
{
if (str2.Substring(j, 11).Equals(txtInformation.Text))
{
txtStatus.Text += “\r\n前面有入场记录,没有出场记录!”;
}
}
str2 += txtInformation.Text; //添加内容到字符串str2
str2 += textBox3.Text; //记录时间
str2 += textBox4.Text; //记录金额
str2 += “\r\n”;
File.WriteAllText(path, str2);//将字符串str2内容写入str1路径指向的文件
//--------------------------------------------------------------------------------------
break;
}
}
if (i >= 5)
{
txtStatus.Text = “读卡失败,请再次读取!”;
}
}
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 81 03 08 E3 64 41 21 E3 64 41 00 C7 DD
//汽车离开停车场信息
if (status_num == 12)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals(“FF 55 00 00 81 03 08”))
{
txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
txtStatus.Text = “离开停车场!\r\n查找入场记录—”;
now2 = DateTime.Now;
textBox5.Text = now2.ToString();//出场时间
//-------------------------------------------------------------------------------------
//读出文件,查找卡信息、时间信息
//0---------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//E3 64 41 212016/1/26 18:31:5210000
//E3 64 41 212016/1/26 18:31:5610000
var path1 = AppDomain.CurrentDomain.BaseDirectory + @“data.txt”;
if (!File.Exists(path))
{
File.Create(path);
}
string str2 = File.ReadAllText(path); //读文件,存储到字符串str2
int j;
for (j = 0; j < str2.Length - 30; j++) //查找是否已经入场
{
if (str2.Substring(j, 11).Equals(txtInformation.Text))
{
break;
}
}
if (j < str2.Length - 30)//有卡信息,先提取时间,然后删除卡信息
{
txtStatus.Text += “查找入场记录!\r\n计费中—”;
var timeStr = str2.Substring(j + 11, 19);
//提取时间
now3 = DateTime.ParseExact(timeStr, “年-月-日 时:分:秒”, null);
//停留时间
textBox8.Text = (now2 - now3).ToString();//显示停留时间
//计算秒,做为消费金额
TimeSpan ts = now2.Subtract(now3).Duration();
money1 = int.Parse(ts.Hours.ToString()) * 3600 + int.Parse(ts.Minutes.ToString()) * 60 + int.Parse(ts.Seconds.ToString());
money = money - money1;
textBox6.Text = money1.ToString();//显示消费金额
textBox4.Text = money.ToString(); //显示余额
txtStatus.Text += “计费完毕!\r\n欢迎下次光临!”;
}
else
{
txtStatus.Text += “未查到入场记录!”;
}
//--------------------------------------------------------------
//删除卡信息
for (j = 0; j < str2.Length - 30; j++) //查找是否已经入场
{
if (str2.Substring(j, 11).Equals(txtInformation.Text))
{
str2 = str2.Remove(j, 35);
}
}
File.AppendAllText(path, str2);//重新写入文件
//--------------------------------------------------------------
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
//低频卡信息读取,注册
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 = “卡有问题!”;
}
}
}
//综合实验
private void button9_Click(object sender, EventArgs e)
{
txtSend.Text = “”;
txtReceive.Text = “”;
txtInformation.Text = “”;
status_num = 10;
//-------------0---------1---------2---------3–
//-------------012345678901234567890123456789
String str1 = “FF 55 00 00 01 01 00 50 74”;
str1 = str1.Remove(21, 2);
str1 = str1.Insert(21, comboBox4.Text);
str1 = str1.Remove(24, 11);
str1 = str1.Insert(24, textBox5.Text);
str1 = str1.Remove(36, 11);
str1 = str1.Insert(36, textBox10.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);//显示
}
//常规读,正确开门
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 btnClear_Click(object sender, EventArgs e)
{
txtSend.Text = “”;
txtReceive.Text = “”;
txtInformation.Text = “”;
txtStatus.Text = “数据已经清除!”;
}
//发送读取高频卡信息命令(FF 55 00 00 01 03 00 30 75)
//发送命令,获取高频卡信息
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数嵌入式工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以+V:Vip1104z获取!!! (备注:嵌入式)
最后
资料整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~
你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!
**
因此收集整理了一份《2024年嵌入式&物联网开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-fzkyPV6S-1712318372112)]
[外链图片转存中…(img-RhWLKKgM-1712318372114)]
[外链图片转存中…(img-boHvley9-1712318372114)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上嵌入式&物联网开发知识点,真正体系化!
[外链图片转存中…(img-Z0RGl2xy-1712318372115)]
[外链图片转存中…(img-khJVbPU2-1712318372116)]
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以+V:Vip1104z获取!!! (备注:嵌入式)
最后
资料整理不易,觉得有帮助的朋友可以帮忙点赞分享支持一下小编~
你的支持,我的动力;祝各位前程似锦,offer不断,步步高升!!!