物联网应用技术2班-20210320018-李俊运
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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
using System.Collections;
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 timeover = 0;//声明一个全局变量
int timer_num,money;
string path = @"E:\下载\实验03低频卡初始化操作\停车消费记录表.txt";
string kohao,name,xuehao,sex,tk;
/*-------------------------------------------------------------
* status_num状态字
* 1,表示读取信息
--------------------------------------------------------------*/
int status_num=0;//状态字
public Form1()
{
InitializeComponent();
}
//窗体加载
private void Form1_Load(object sender, EventArgs e)
{
//串口初始化
cmbPort.SelectedIndex = 2;
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;
}
//串口打开与关闭
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 = "关闭串口";
btnClear.Enabled = false;
button1.Enabled = false;
button10.Enabled = true;
//button2.Enabled = true;
// 数据接收模式变化时,设置串口的数据接收侦听事件
try
{
com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived); //加载接收事件
}
catch (Exception err)
{
}
}
catch
{ }
}
else //关闭串口
try
{
if (com.IsOpen)
com.Close(); //关闭串口
btnOpen.Text = "打开串口";
btnClear.Enabled = false;
button1.Enabled = false;
button10.Enabled = false;
//button2.Enabled = false;
}
catch
{
}
}
//--------------------------------------------------------------------------------
//定时器相关设置
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);
//String strReceive = Encoding.Default.GetString(readBuffer); //字母、数字、汉字转换为字符串
String strReceive = getStringFromBytes(readBuffer); //转十六进制
this.Invoke(new showReceiveDelegate(doShowReceive), strReceive);
}
catch (Exception err)
{
}
}
}
//--------------------------------------------------------------------------------
// 响应模式时,串口接收数据事件
private void com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
com_num = com.BytesToRead;
timer_num = 0;
}
//异步线程处理接受的字符,显示在接收的文本框中
public void doShowReceive(string 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)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 01 08"))
{
txtInformation.Text = str.Substring(i + 21, 23);//截取4个字节
break;
}
}
if(i>=10)
{
}
}
//-----------------------------------------------------
//----------1---------2---------3---------4---------
//01234567890123456789012345678901234567890123456789
//FF 55 00 00 82 04 01 00 EE 33
//低频卡初始化操作
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 04 01 00"))
{
break;
}
}
if(i>=5)
{
}
}
if (status_num == 3)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 03 04"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 4)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 83 04 01 00"))
{
break;
}
}
if (i >= 5)
{
}
}
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 05 01 00"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 6)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 23).Equals("FF 55 00 00 82 06 01 00"))
{
break;
}
}
if (i >= 5)
{
}
}
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 83 05 04"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 8)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 06 01"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 9)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 01 08"))
{
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 10)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 01 08"))
{
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 11)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 03 08"))
{
txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 12)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 82 03 01"))
{
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 13)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 82 02 01"))
{
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 14)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 17).Equals("FF 55 00 00 82 00"))
{
txtInformation.Text = str.Substring(i + 21, 11);
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 15)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 82 09 01"))
{
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 16)
{
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"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 17)
{
status_num = 0;
int i;
for (i = 0; i < 5; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 83 01 10"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 18)
{
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"))
{
break;
}
}
if (i >= 5)
{
}
}
if (status_num == 19)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 03 08"))
{
txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
break;
}
}
if (i >= 10)
{
}
}
if (status_num == 20)
{
status_num = 0;
int i;
for (i = 0; i < 10; i++)
{
if (str.Substring(i, 20).Equals("FF 55 00 00 81 03 08"))
{
txtInformation.Text = str.Substring(i + 21, 11);//截取4个字节
break;
}
}
if (i >= 10)
{
}
}
}
// 把字节数组转换为十六进制格式的字符串。
// <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;
}
//------------------------------------------------------------
//清空接收框内容
private void btnClear_Click(object sender, EventArgs e)
{
txtSend.Text = "";
txtInformation.Text = "";
}
//------------------------------------------------------------
//发送命令,获取低频卡信息
//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 = "";
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 = "";
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;
}
private void button2_Click(object sender, EventArgs e)
{
txtSend.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);
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 button3_Click(object sender, EventArgs e)
{
txtSend.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.Remove(24, 11);
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 button4_Click(object sender, EventArgs e)
{
txtSend.Text = "";
status_num = 5;
String str1 = "FF 55 00 00 02 05 04 00 00 00 00 91 A3";
str1 = str1.Remove(21, 11);
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 button5_Click(object sender, EventArgs e)
{
txtSend.Text = "";
status_num = 6;
String str1 = "FF 55 00 00 02 06 04 00 00 00 00 A2 A3";
str1 = str1.Remove(21, 11);
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 button6_Click(object sender, EventArgs e)
{
txtSend.Text = "";
status_num = 7;
//-------------0---------1---------2---------3--
//-------------012345678901234567890123456789
String str1 = "FF 55 00 00 03 05 05 01 00 00 00 00 B3 23";
str1 = str1.Remove(21, 2);
str1 = str1.Remove(24, 11); //获取密码
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 button7_Click(object sender, EventArgs e)
{
try
{
txtSend.Text = "";
status_num = 8;
String str1 = "FF 55 00 00 03 06 09 01 00 00 00 00 00 00 00 00 2C 4E";
//---------------------------------------------------------------
str1 = str1.Remove(21, 2);
str1 = str1.Remove(24, 11); //获取密码
str1 = str1.Remove(36, 11); //获取数据
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);//显示
}
catch
{
MessageBox.Show("输入为空","提示"+MessageBoxIcon.Error);
}
}
private void button10_Click(object sender, EventArgs e) //高频卡获取信息
{
txtSend.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;
}
private void button11_Click(object sender, EventArgs e) //打开天线
{
txtSend.Text = "";
txtInformation.Text = "";
status_num = 12;
String str1 = "FF 55 00 00 02 03 00 30 85";
byte[] data = getBytesFromString(str1);
com.Write(data, 0, data.Length);
txtSend.Text = str1;
}
public void Delay(int miliSecond)//延时函数,sleep会影响UI界面,故用此函数来延时(单位ms)
{
int start = Environment.TickCount;
while (Math.Abs(Environment.TickCount - start) < miliSecond)
{
Application.DoEvents();
}
timeover++;//已在全局变量中声明
}
private void button18_Click(object sender, EventArgs e)
{
string k, time;
if (File.Exists(path) == true) //判断文件是否创建
{
tk = File.ReadAllText(path);
k = txtInformation.Text;
if (tk.Contains(k)==true)//判断卡号
{
MessageBox.Show("卡号已入场");
}
else
{
StreamWriter write = new StreamWriter(path, true);
textBox19.Text = Convert.ToString(DateTime.Now);
textBox21.Text = "200元";
time = textBox19.Text;
write.WriteLine(k);
write.WriteLine(time);
write.Close();
}
}
else
{
FileStream logon1 = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
logon1.Close();
}
}
private void button19_Click(object sender, EventArgs e)
{
string[] t_k = new string[50];
string c_k, ct, t,k,o,s;
int ch, cm, cs, th, tm, ts, timer;
if (File.Exists(path) == true)
{
tk = File.ReadAllText(path);
c_k = txtInformation.Text;
if (tk.Contains(c_k) == true)
{
ct = textBox20.Text = Convert.ToString(DateTime.Now);
StreamReader reader = new StreamReader(path);
for (int i = 0; i < 50; i++)
{
t_k[i] += reader.ReadLine();
}
reader.Close();
for (int i = 0; i < 50; i++)
{
if (t_k[i] == c_k)
{
k = t_k[i]+"\r\n";
o = t_k[i+1]+ "\r\n";
t = t_k[i + 1];
t = t.Remove(0, 9).Replace(":", "");
th = Convert.ToInt32(t.Substring(0, 2)) * 3600;
tm = Convert.ToInt32(t.Substring(2, 2)) * 60;
ts = Convert.ToInt32(t.Substring(4, 2));
ct = ct.Remove(0, 9).Replace(":", "");
ch = Convert.ToInt32(ct.Substring(0, 2)) * 3600;
cm = Convert.ToInt32(ct.Substring(2, 2)) * 60;
cs = Convert.ToInt32(ct.Substring(4, 2));
t = (th + tm + ts).ToString();
ct = (ch + cm + cs).ToString();
timer = int.Parse(ct) - int.Parse(t);
cm = timer * 2;
money = 200-cm;
textBox21.Text = money.ToString() + "元";
textBox22.Text = timer.ToString() + "秒";
textBox23.Text = cm.ToString() + "元";
s = tk.Replace(k+o,"");
File.WriteAllText(path,s);
}
}
}
else
{
MessageBox.Show("卡号不存在");
}
}
else
{
MessageBox.Show("文件不存在");
}
}
}
}