c#高频卡停车场收费系统
开发工具及技术介绍
开发工具:
Microsoft Visual Studio 2019(简称 VS)是美国的开发工具包系列产品。VS 是一个基本的开发工具集,它包括了整个中所需要的大部分工具,如 UML 工具、代码管控工具、(IDE)等等。所写的目标代码适用于微软支持的所有平台,包括、、、、和 Microsoft 及。2018 年 6 月,微软宣布开发新一代集成开发环境(integrated development environment,IDE)Visual Studio 2019。
MySQL 8.0:MySQL 是一个,由瑞典 MySQL AB 公司开发,属于 旗下产品。MySQL 是最流行的之一,在 Web 应用方面,MySQL 是最好的 (Relational Database Management System,关系数据库管理系统) 应用软件之一。
技术介绍:
该课程设计主要使用了.net Window 窗体编程技术,Oracle 的 MySql.Data 技术以及 MySQL8.0 数据库的连接和交互技术以实现数据在系统中的可视化和可修改化。利用编译的 DBHelper.cs 文件实现该技术,对数据库的内容进行增删改查等操作。
2.总体设计
系统总体架构
员工或管理员在登录页面选择不同的身份输入自己的工号和密码后进入系统主页面,点击该页面的菜单项内容进入不同的系统模块进行不同的操作。管理员用户的菜单会额外显示员工管理选项和车位收费标准选项。普通员工登陆之后,菜单中的员工管理选项和车位收费标准选项会隐藏且普通员工无法进行操作。该系统为实现相关的操作,共在数据库中创建了保存管理员相关信息的管理员表、保存员工信息的员工表、保存停车证信息的停车证表、保存停车位信息的停车位表和保存收费标准的收费标准表五个表。
登录模块总体设计
登录模块主要用于识别用户的身份,并按照不同的用户身份进行不同的判断,成功则按照不同的身份类型进入主界面,失败按照识别原因进行相应的提示。模块架构图如图 2-1 所示。
登录模块总体设计
登录模块主要用于识别用户的身份,并按照不同的用户身份进行不同的判断,成功则按照不同的身份类型进入主界面,失败按照识别原因进行相应的提示。模块架构图如图 2-1 所示。

图 2-1 系统总体架构图
登录模块总体设计
登录模块主要用于识别用户的身份,并按照不同的用户身份进行不同的判断,成功则按照不同的身份类型进入主界面,失败按照识别原因进行相应的提示。模块架构图如图 2-1 所示。

图 2-2 登录模块架构图
主界面模块总体设计
主界面由一个 MenuStrip 菜单控件、一个文本提示框 Label 控件和一个 Panel 容器控件组成。菜单选项用于实现用户方便快捷的选择和使用系统的功能,当用户身份为管理员身份时,菜单选项中会额外多出“员工管理”选项和“收费标准”两个选项;当用户身份为普通员工时,这两个选项会隐藏并无法进行操作。当用户需要进行某项操作时,可以点击菜单中的相应选项,点击之后会在 Panel 控件中显示相应的界面以用于用户进行操作。模块架构图如图 2-3 所示。

图 2-3 主界面模块架构图
停车证管理模块总体设计
停车证管理模块包括停车证信息的检索、添加、编辑和删除等功能。主界面菜单中的“停车证管理”选项的子选项用于选择相应功能。停车证办理选项用于实现停车证信息的添加,停车证检索选项用于实现停车证信息的检索、添加、编辑和删除功能。模块架构图如图 2-4 所示。

图 2-4 停车证管理模块架构图
停车位管理模块总体设计
停车位管理模块包括车位信息的查询、添加、编辑和删除以及车辆的驶入、驶出和计费等功能。主界面菜单中的“车位管理”选项用于选择车辆驶入和车辆驶出并计费两种功能;主界面菜单中的“车位信息检索”选项用于实现停车位信息的检索、添加、编辑和删除功能。模块架构图如图 2-5 所示。

图 2-5 停车位管理模块架构图
员工管理模块总体设计
员工管理模块包括对员工信息的检索、添加、编辑和删除的功能。当用户身份为管理员时,主界面的菜单选项会显示“员工管理”选项,用于实现员工信息的查询、添加、删除和密码重置等功能。模块架构图如图 2-6 所示。

图 2-6 员工管理模块架构图
上位机图效果图

硬件设计

硬件连接方式

程序代码
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";//定义Brush bush1 = null;Brush bush2 = null;Brush bush3 = null;Brush bush4 = null;Graphics g1 = null;Graphics g2 = null;Graphics g3 = null;//--------------------------------------------//停车场计费系统DateTime now1 = DateTime.Now;//获取当前时间DateTime now2 = DateTime.Now;//获取当前时间DateTime now3 = DateTime.Now;//获取当前时间public int money = 10000;public int money1 = 1;/*-------------------------------------------------------------* status_num状态字* 1,表示读取信息* 2,激活高频卡--------------------------------------------------------------*/int status_num=0;//状态字public Form1 (){InitializeComponent();//初始化g1 = this.pictureBox1.CreateGraphics();g2 = this.pictureBox2.CreateGraphics();g3 = this.pictureBox3.CreateGraphics();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//发送命令,高频卡HALTif (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); //读文件,存储到字符串str2int j;for (j = 0; j < str2.Length - 25; j++) //查找是否已经入场{if (str2.Substring(j, 11).Equals(txtInformation.Text)){txtStatus.Text += "\r\n前面有入场记录,没有出场记录!";}}str2 += txtInformation.Text; //添加内容到字符串str2str2 += 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:5610000var path1 = AppDomain.CurrentDomain.BaseDirectory + @"data.txt";if (!File.Exists(path)){File.Create(path);}string str2 = File.ReadAllText(path); //读文件,存储到字符串str2int 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; //添加内容到字符串str2fileText += 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); //读文件,存储到字符串str2var 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--//-------------012345678901234567890123456789String 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) //发送命令,获取高频卡信息private void btnInformation_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 1;String str1 = "FF 55 00 00 01 03 00 30 75";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//关闭天线private void button1_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 2;String str1 = "FF 55 00 00 02 02 00 A0 84";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//打开天线private void button2_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 3;String str1 = "FF 55 00 00 02 03 00 30 85";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,激活高频卡private void button3_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";status_num = 4;String str1 = "FF 55 00 00 02 00 00 C0 85";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,高频卡HALTprivate void button4_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 5;String str1 = "FF 55 00 00 02 09 00 90 83";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,禁止CRC16校验private void button5_Click_1(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 6;String str1 = "FF 55 00 00 00 00 00 00 00";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,使能CRC16校验 FF 55 00 00 00 01 00 90 25private void button6_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 7;String str1 = "FF 55 00 00 00 01 00 90 25";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,高频M1卡密码认证//FF 55 00 00 05 02 08 00 FF FF FF FF FF FF 00 ED 9Astatus_num = 8;private void button7_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 8;String str1 = "00 00 05 02 08 00 " + this.txtBoxSector.Text + " " + (Convert.ToInt32(this.cmbSector.Text) * 4).ToString("X").PadLeft(2, '0');byte[] data = getBytesFromString(str1);var crc = crc16(data, data.Length);Array.Reverse(crc);str1 = "FF 55 " + str1 + " " + getStringFromBytes(crc);data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送命令,高频M1卡块数据块读取 FF 55 00 00 03 01 01 01 CF 91private void button8_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 9;//-------------0---------1---------2---------3--//-------------012345678901234567890123456789String str1 = "FF 55 00 00 03 01 01 01 CF 91";//str1 = str1.Remove(18, 2); //获取数据块的地址//str1 = str1.Insert(18, comboBox1.Text);str1 = str1.Remove(21, 2);//(地址 扇区号*4 + 块号 16进制)str1 = str1.Insert(21, (Convert.ToInt32(this.comboBox1.Text) * 4 + Convert.ToInt32(this.comboBox2.Text)).ToString("X").PadLeft(2, '0'));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);//显示}//发送命令,高频M1卡块数据块写入 FF 55 00 00 03 02 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 01 96 05private void button90_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 10;//-------------0---------1---------2---------3---------4---------5---------6---------7---------//-------------01234567890123456789012345678901234567890123456789012345678901234567890123456789String str1 = "FF 55 00 00 03 02 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 01 96 05";str1 = str1.Remove(21, 47);str1 = str1.Insert(21, textBox2.Text); //修改内容str1 = str1.Remove(69, 2);str1 = str1.Insert(69, (Convert.ToInt32(this.comboBox1.Text) * 4 + Convert.ToInt32(this.comboBox3.Text)).ToString("X").PadLeft(2, '0'));//存储块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);//显示}// 把字节数组转换为十六进制格式的字符串。// 要转换的字节数组。// 返回十六进制格式的字符串。 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; //返回字符串临时变量。}////把十六进制格式的字符串转换成字节数组。// //要转换的十六进制格式的字符串//返回字节数组。 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;}//发送读取高频卡信息命令(FF 55 00 00 01 03 00 30 75) //发送命令,获取高频卡信息,记录入停车场信息private void button20_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";status_num = 11;String str1 = "FF 55 00 00 01 03 00 30 75";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}//发送读取高频卡信息命令(FF 55 00 00 01 03 00 30 75) //发送命令,获取高频卡信息,记录出停车场信息private void button11_Click(object sender, EventArgs e){txtSend.Text = "";txtReceive.Text = "";txtInformation.Text = "";textBox6.Text = "";status_num = 12;String str1 = "FF 55 00 00 01 03 00 30 75";byte[] data = getBytesFromString(str1);com.Write(data, 0, data.Length);txtSend.Text = str1;}private void tabPage9_Click(object sender, EventArgs e){}private void button14_Click(object sender, EventArgs e){}private void button14_Click_1(object sender, EventArgs e){}private void tabPage8_Click(object sender, EventArgs e){}private void button13_Click(object sender, EventArgs e){ }private void label26_Click(object sender, EventArgs e){}private void label27_Click(object sender, EventArgs e){}}}
--------黄金宁 21物联网应用技术2班
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
