FrmMain.cs 4.9 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFID_Debug
{
    public partial class FrmMain : Form
    {

        private bool[] _open;
        private string[] _ip;

        private Asa.RFID.HFReader[] reader;
        private Asa.RFID.HFReaderAll readerAll;

        private int sum = 0;
        private string currIP = "";
        //private Dictionary<string, string> buffer = new Dictionary<string, string>();
        private System.Threading.Thread tSave;
        private System.Collections.Concurrent.ConcurrentQueue<string> info = new System.Collections.Concurrent.ConcurrentQueue<string>();

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            CboLocal.Items.AddRange(Asa.RFID.IP.GetLocal());
            if (CboLocal.Items.Count > 0)
                CboLocal.SelectedIndex = 0;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tSave != null)
                tSave.Abort();

            if (reader != null)
            {
                for (int i = 0; i < reader.Length; i++)
                {
                    reader[i].Close();
                    System.Threading.Thread.Sleep(10);
                }
            }

            if (readerAll != null)
            {
                readerAll.Close();
                readerAll = null;
            }
        }

        private void BtnGetIP_Click(object sender, EventArgs e)
        {
            LstIP.Items.Clear();
            _ip = Asa.RFID.IP.Find(CboLocal.Text);
            LstIP.Items.AddRange(_ip);
            _open = new bool[_ip.Length];
        }

        private void BtnChangeIP_Click(object sender, EventArgs e)
        {
            if (LstIP.SelectedIndex == -1) return;
            bool rtn = Asa.RFID.IP.Modify(CboLocal.Text, LstIP.Text, textBox1.Text);
            if (rtn)
                MessageBox.Show("修改完成");
            else
                MessageBox.Show("修改失败", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void BtnOpen_Click(object sender, EventArgs e)
        {
            if (LstIP.SelectedIndex == -1) return;
            //if (_open[LstIP.SelectedIndex]) return;

            TabPage tab = new TabPage { Text = LstIP.Text };
            UserControl1 ctl = new UserControl1(LstIP.Text);
            tab.Controls.Add(ctl);
            tabControl1.Controls.Add(tab);
            //_open[LstIP.SelectedIndex] = true;
        }

        private void LstIP_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = LstIP.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] ip = new string[LstIP.Items.Count];
            for (int i = 0; i < ip.Length; i++)
                ip[i] = LstIP.Items[i].ToString();

            readerAll = new Asa.RFID.HFReaderAll();
            readerAll.LogPath = Application.StartupPath;
            readerAll.GetValue += Ra_GetValue;
            readerAll.Open(ip);
            //readerAll.Open("192.168.210.114", "192.168.210.117");
        }

        private void Ra_GetValue(string s)
        {
            Invoke(new Action(() =>
            {
                if (s.Contains(LstIP.Items[0].ToString()))
                    textBox2.AppendText("======\r\n");
                textBox2.AppendText(s + "\r\n");

            }));
        }

        private void Reader_Received(string ip, byte[] buff)
        {
            try
            {
                //string val = (char)buff[1] + buff[2].ToString();
                //if (ip == currIP) return;

                //currIP = ip;



                //string s = "";
                //if (ip == LstIP.Items[0].ToString())
                //{
                //    s = "========== ";
                //    sum = 0;
                //}
                //s += (++sum) + " " + ip + " ";
                //s += val;
                //info.Enqueue(s);

                string s = "";
                for (int i = 0; i < buff.Length; i++)
                    s += buff[i].ToString("X2") + " ";

                Invoke(new Action(() => { textBox2.AppendText(s); }));
            }
            catch (Exception)
            { }
        }


        private void SaveLog()
        {
            string _logFile = string.Format("{0}\\RFID_{1:yyyy-MM-dd}.log", Application.StartupPath, DateTime.Now);
            while (true)
            {
                System.Threading.Thread.Sleep(100);
                if (info.TryDequeue(out string result))
                    System.IO.File.AppendAllText(_logFile, string.Format("[{0:HH:mm:ss.fff}] {1}\r\n", DateTime.Now, result), Encoding.UTF8);
            }
        }


    }
}