⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 RFID reader 原始碼 CF2E
💻 CS
字号:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SYRD245Demo_PDA
{
    public partial class Form1 : Form
    {
        string TAG_Data1 = "";
        string TAG_Data2 = "";
        int TAG_Total = 0;
        bool Processing = true;

        public Form1()
        {
            InitializeComponent();
            for (int i = 1; i <= 16; i++)
            {
                comboBoxPort.Items.Add("COM" + i.ToString());
            }
            comboBoxPort.SelectedIndex = 3;
            // 
            // dataGridTextBoxColumn1
            // 
            dataGrid1.TableStyles.Add(this.dataGridTableStyle1);
            dataGridTableStyle1.GridColumnStyles.Add(this.dataGridTextBoxColumn1);
            dataGridTextBoxColumn1.Width = 320;
        }


        private void button_Start_Stop_Click(object sender, EventArgs e)
        {
            if (button_Start_Stop.Text == "Stop")
            {
                button_Start_Stop.Text = "Start";
                Processing = false;

                pnl_Setup.Visible = false;
                button_Exit.Enabled = false;
                button_Setup.Enabled = false;
                button_Clear.Enabled = false;
                button_Start_Stop.Enabled = false;
                System.Threading.Thread.Sleep(1000);

                Processing = false;
                TAG_Data1 = "";
                TAG_Data2 = "";

                button_Exit.Enabled = true;
                button_Setup.Enabled = true;
                button_Clear.Enabled = true;
                button_Start_Stop.Enabled = true;
                return;
            }

            pnl_Setup.Visible = false;
            button_Exit.Enabled = false;
            button_Setup.Enabled = false;
            button_Clear.Enabled = false;
            button_Start_Stop.Enabled = false;
            try
            {
                if (serialPort1.IsOpen == true)
                {
                    serialPort1.Close();
                }
                serialPort1.PortName = comboBoxPort.Text;
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SerialPort Open Error!!");

                button_Exit.Enabled = true;
                button_Setup.Enabled = true;
                button_Clear.Enabled = true;
                button_Start_Stop.Enabled = true;

                return;
            }

            try
            {
                int i, Single_Length = 0;
                char[] com = new char[3];
                bool empty;
                string TAG_ID, TAG_RSSI, TAG_LQI, TAG_DI, TAG_T1, TAG_T2;

                Processing = true;
                com[0] = 'A';
                com[1] = '3';
                com[2] = Convert.ToChar(0x0D);
                serialPort1.Write(com, 0, 3);
                System.Threading.Thread.Sleep(100);

                button_Exit.Enabled  = true;
                button_Setup.Enabled = false;
                button_Clear.Enabled = true;
                button_Start_Stop.Enabled = true;

                TAG_Data1 = "";
                TAG_Data2 = "";
                button_Start_Stop.Text = "Stop";
                while (button_Start_Stop.Text == "Stop")
                {
                    Processing = false;
                    TAG_Data1 = "";
                    Single_Length = 0;
                    if ((TAG_Data2.Length > 22) && (button_Start_Stop.Text == "Stop"))
                    {
                        TAG_Data1 = "";
                        Single_Length = 0;
                        while (Single_Length >= 0)
                        {
                            Single_Length = TAG_Data2.IndexOf(Convert.ToChar(0x0D));
                            if (Single_Length >= 0)
                            {
                                TAG_Data1 = TAG_Data1 + TAG_Data2.Substring(0, Single_Length + 1);
                                TAG_Data2 = TAG_Data2.Substring(Single_Length + 1);
                            }
                        }
                        Processing = true;
                        Application.DoEvents();

                        Single_Length = 0;
                        while( (TAG_Data1.Length>22) && (button_Start_Stop.Text=="Stop") ) 
                        {
                            Single_Length = TAG_Data1.IndexOf(Convert.ToChar(0x0D));
                            if( (Single_Length >= 22) && (Single_Length < TAG_Data1.Length) )
                            {
                                TAG_ID = TAG_Data1.Substring(0, 16);
                                TAG_RSSI = TAG_Data1.Substring(16, 2);
                                TAG_LQI = TAG_Data1.Substring(18, 2);
                                TAG_DI = TAG_Data1.Substring(20, 2);
                                if (Single_Length > 22)
                                {
                                    TAG_T1 = TAG_Data1.Substring(22, 4);
                                }
                                else
                                {
                                    TAG_T1 = "-";
                                }

                                if (Single_Length > 26)
                                {
                                    TAG_T2 = TAG_Data1.Substring(26, 4);
                                }
                                else
                                {
                                    TAG_T2 = "-";
                                }
                                empty = true;
                                for (i = 0; i < TAG_Total; i++)
                                {
                                    if (dataSet1.Tables["TAGMessage"].Rows[i]["ID"].ToString() == TAG_ID)
                                    {
                                        dataSet1.Tables["TAGMessage"].Rows[i]["RSSI"] = TAG_RSSI;
                                        dataSet1.Tables["TAGMessage"].Rows[i]["LQI"] = TAG_LQI;
                                        dataSet1.Tables["TAGMessage"].Rows[i]["DI"] = TAG_DI;
                                        dataSet1.Tables["TAGMessage"].Rows[i]["T1"] = TAG_T1;
                                        dataSet1.Tables["TAGMessage"].Rows[i]["T2"] = TAG_T2;
                                        tAGMessageBindingSource.Position = i;
                                        empty = false;
                                        break;
                                    }
                                }
                                if (empty == true)
                                {
                                    dataSet1.Tables["TAGMessage"].Rows.Add(TAG_ID, TAG_RSSI, TAG_LQI, TAG_DI, TAG_T1, TAG_T2);
                                    TAG_Total++;
                                    textBox1.Text = TAG_Total.ToString();
                                }
                                dataGrid1.Refresh();

                                TAG_Data1 = TAG_Data1.Substring(Single_Length + 1);
                            }
                            else
                            {
                                TAG_Data1 = TAG_Data1.Substring(Single_Length + 1);
                                break;
                            }
                        }
                    }
                    Processing = true;
                    Application.DoEvents();
                }
            }
            catch (Exception ex){}

            try
            {
                TAG_Data1 = "";
                TAG_Data2 = "";

                char[] com = new char[3];

                com[0] = 'A';
                com[1] = '0';
                com[2] = Convert.ToChar(0x0D);
                serialPort1.Write(com, 0, 3);
                System.Threading.Thread.Sleep(100);
                serialPort1.Close();
            }
            catch (Exception ex){}

            button_Exit.Enabled = true;
            button_Setup.Enabled = true;
            button_Clear.Enabled = true;
            button_Start_Stop.Enabled = true;
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            pnl_Setup.Visible = false;

            dataSet1.Clear();
            TAG_Total = 0;
            textBox1.Text = TAG_Total.ToString();
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            pnl_Setup.Visible = false;

            button_Start_Stop.Text = "Start";
            System.Threading.Thread.Sleep(1000);

            Application.Exit();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (Processing == true)
            {
                TAG_Data2 = TAG_Data2 + serialPort1.ReadExisting();
            }
        }

        private void button_Setup_Click(object sender, EventArgs e)
        {
            if (pnl_Setup.Visible == false)
                pnl_Setup.Visible = true;
            else
                pnl_Setup.Visible = false;
        }

        private void hsbGainLevel_ValueChanged(object sender, EventArgs e)
        {
            lblGainLevelValue.Text = (hsbGainLevel.Value).ToString();
        }

        private void hsbRssiLevel_ValueChanged(object sender, EventArgs e)
        {
            lblRssiLevelValue.Text = (hsbRssiLevel.Value).ToString();
        }

        private void btn_SetGainLevel_Click(object sender, EventArgs e)
        {
            btn_SetGainLevel.Enabled = false;
            Processing = true;
            try
            {
                if (serialPort1.IsOpen == true)
                {
                    serialPort1.Close();
                }
                serialPort1.PortName = comboBoxPort.Text;
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SerialPort Open Error!!");
                return;
            }

            try
            {
                TAG_Data1 = "";
                TAG_Data2 = "";

                if (serialPort1.IsOpen == true)
                {
                    char[] com = new char[5];
                    com[0] = 'A';
                    com[1] = '0';
                    com[2] = Convert.ToChar(0x0D);
                    serialPort1.Write(com, 0, 3);
                    Application.DoEvents();

                    System.Threading.Thread.Sleep(500);
                }
            }
            catch (Exception ex) { }

            try
            {
                if (serialPort1.IsOpen == true)
                {
                    char[] com = new char[8];

                    if (hsbGainLevel.Value == 1)
                    {
                        com[5] = '3';
                        com[6] = '8';
                    }
                    else if (hsbGainLevel.Value == 2)
                    {
                        com[5] = '3';
                        com[6] = 'F';
                    }
                    else if (hsbGainLevel.Value == 3)
                    {
                        com[5] = 'B';
                        com[6] = 'F';
                    }
                    else if (hsbGainLevel.Value == 4)
                    {
                        com[5] = '1';
                        com[6] = 'F';
                    }
                    else if (hsbGainLevel.Value == 5)
                    {
                        com[5] = 'D';
                        com[6] = 'F';
                    }
                    else if (hsbGainLevel.Value == 6)
                    {
                        com[5] = '8';
                        com[6] = '7';
                    }
                    else if (hsbGainLevel.Value == 7)
                    {
                        com[5] = 'C';
                        com[6] = '7';
                    }

                    com[0] = 'W';
                    com[1] = '1';
                    com[2] = 'C';
                    com[3] = '0';
                    com[4] = '1';
                    com[7] = Convert.ToChar(0x0D);
                    serialPort1.Write(com, 0, 8);
                    Application.DoEvents();

                    System.Threading.Thread.Sleep(500);
                    serialPort1.Close();
                }
            }
            catch (Exception ex) { }

            btn_SetGainLevel.Enabled = true;
            pnl_Setup.Visible = false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -