《物联网工程关键技术》高频卡综合项目

赵龙威 物联网应用技术二班 20210320052

 代码如下:

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
            //发送命令,高频卡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)        
        //发送命令,获取高频卡信息
        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;
            
        }
        //发送命令,高频卡HALT
        private 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 25
        private 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 91
        private void button8_Click(object sender, EventArgs e)
        {
            txtSend.Text = "";
            txtReceive.Text = "";
            txtInformation.Text = "";
            status_num = 9;
            //-------------0---------1---------2---------3--
            //-------------012345678901234567890123456789
            String 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 05
        private void button90_Click(object sender, EventArgs e)
        {
            txtSend.Text = "";
            txtReceive.Text = "";
            txtInformation.Text = "";
            status_num = 10;
            //-------------0---------1---------2---------3---------4---------5---------6---------7---------
            //-------------01234567890123456789012345678901234567890123456789012345678901234567890123456789
            String 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);//显示
        }
        // 把字节数组转换为十六进制格式的字符串。
        // <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;
        }
        //发送读取高频卡信息命令(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)
        {

        }


        }
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值