UserControl1.cs 3.9 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RFID_Debug
{
    public partial class UserControl1 : UserControl
    {
        private Asa.RFID.HFReader read;

        public UserControl1()
        {
            InitializeComponent();
        }

        public UserControl1(string ip) : this()
        {
            CboType.SelectedIndex = 0;
            read = new Asa.RFID.HFReader(ip);
            read.Received += Read_Received;

            read.Open(false);
            if (read.IsConn)
            {
                read.AutoScanMode(false);
                groupBox2.Enabled = true;
                groupBox3.Enabled = true;
                BtnFind.Enabled = true;
            }
            else
            {
                groupBox2.Enabled = false;
                groupBox3.Enabled = false;
                BtnFind.Enabled = false;
                LblID.Text = "无法连接";
            }
        }

        private void Read_Received(string ip, byte[] buff)
        {
            string s = (char)buff[1] + buff[2].ToString();
            this.Invoke(new Action(() => { TxtRead.AppendText(s); }));
        }

        private void BtnFind_Click(object sender, EventArgs e)
        {
            bool rtn = read.FindRFID();
            if (!rtn)
                LblID.Text = "无法连接";
            else
                LblID.Text = "ID: " + read.ID;
        }

        private void BtnReadAll_Click(object sender, EventArgs e)
        {
            byte[] bb = read.Read();
            if (bb == null)
            {
                TxtRead.Text = "";
            }
            else
            {
                string s = "";
                for (int i = 0; i < bb.Length; i++)
                    s += bb[i].ToString("X2") + " ";
                TxtRead.Text = s;
            }
        }

        private void BtnReadOne_Click(object sender, EventArgs e)
        {
            byte[] bb = read.Read();
            if (bb == null)
            {
                TxtRead.Text = "";
            }
            else
            {
                if (bb[0] == 0x5A && bb[5] == 0x4A)
                {
                    TxtRead.Text = ((char)bb[1]).ToString() + bb[2];
                }
                else
                {
                    TxtRead.Text = "格式不正确";
                }
            }

        }

        private void BtnWrite_Click(object sender, EventArgs e)
        {
            byte[] bb = new byte[8];
            bb[0] = 0x5A;
            bb[1] = Convert.ToByte(65 + CboType.SelectedIndex);
            bb[2] = Convert.ToByte(NudNumber.Value);
            bb[5] = 0x4A;

            int sum = 0;
            for (int i = 0; i < 6; i++)
                sum += bb[i];
            byte[] cc = BitConverter.GetBytes(sum);
            bb[6] = cc[0];
            bb[7] = cc[1];

            bool rtn = read.Write(bb);
            if (rtn)
                label3.Text = ((char)bb[1]).ToString() + bb[2] + " OK";
            else
                label3.Text = "NG";
        }

        private void BtnNumMinus_Click(object sender, EventArgs e)
        {
            if (NudNumber.Value > NudNumber.Minimum)
                NudNumber.Value--;
        }

        private void BtnNumAdd_Click(object sender, EventArgs e)
        {
            if (NudNumber.Value < NudNumber.Maximum)
                NudNumber.Value++;
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            read.Close();

            Parent.Parent.Controls.Remove(Parent);
            Parent.Dispose();
            Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            read.OpenCloseRF(false);
            System.Threading.Thread.Sleep(100);
            read.OpenCloseRF(true);
        }
    }
}