www.pudn.com > CS_ActiveRFID-DEMO_20140107A.rar > ActiveRFID.cs, change:2014-01-07,size:7522b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
 
/* 
 *  
 * This project is for active RFID demo test; 
 * Created by Bill.Zhang 
 * Copyright reserved 2014; 
 *  
 */ 
 
namespace CS_ActiveRFID_DEMO 
{ 
    public partial class ActiveRFID : Form 
    { 
        int WindowTotalWidth; 
        const int InventoryTIDBufferLen = 0x04; 
        const int InventoryEPCBufferLen=0x18; 
        const int InventoryBufferLen = 0x04; 
         
        int intervaltime; 
        int power; 
        public ActiveRFID() 
        { 
            InitializeComponent(); 
            WindowTotalWidth=this.Width ; 
            this.Width = WindowTotalWidth / 2; 
            this.toolStripStatusLabel1.Text = ""; 
            this.toolStripStatusLabel2.Text = ""; 
            this.comboBox1.Text = "TID"; 
            this.textBox3.Text = "5"; 
            this.textBox4.Text = "20"; 
            this.closeToolStripMenuItem.Enabled = false; 
            this.debugModeToolStripMenuItem.Checked = false; 
            this.buttoninventory.Enabled = false; 
            this.copyToolStripMenuItem.Enabled = false; 
            this.pasteToolStripMenuItem.Enabled = false; 
            this.deleteToolStripMenuItem.Enabled = false; 
        } 
 
        private void aboutThisToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            AboutThis AboutThisApp=new AboutThis(); 
            AboutThisApp.ShowDialog(); 
        } 
 
        private void settingToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            Setting SettingApp = new Setting(this); 
            SettingApp.ShowDialog(); 
        } 
 
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
        { 
            this.Invoke(new MethodInvoker(delegate() 
            { 
                if (this.debugModeToolStripMenuItem.Checked == false) 
                { 
                    // parse the inventory response; 
                    // 0xFD+TID(4 bytes)/EPC(24 bytes)+CRC7(TID/EPC); 
                    //string tempstr = serialPort1.ReadExisting(); 
                    //byte[] tempchar = new byte[tempstr.Length]; 
                    //tempchar = System.Text.Encoding.Default.GetBytes(tempstr); 
                    //if (tempstr.Length > 4) 
                    //{ 
 
                    //} 
                    int n = serialPort1.BytesToRead; 
                    if (n > 4) 
                    { 
                        Byte[] tagresponsebytebuffer = new Byte[n]; 
                        //string teststr = ""; 
                        for (int i = 0; i < n; i++) 
                        { 
                            int tempreadbyte = serialPort1.ReadByte(); 
                            tagresponsebytebuffer[i] = (byte)tempreadbyte; 
                            //teststr += Convert.ToString(tempreadbyte, 16).PadLeft(2, '0').ToUpper() + " "; 
                        } 
                        // parse the tag response; 
                        if ((tagresponsebytebuffer[0] == 0xFD) && (CRC.CRC7(tagresponsebytebuffer, InventoryTIDBufferLen) == tagresponsebytebuffer[5])) 
                        { 
                            // show on the listbox; 
                        } 
                    } 
                } 
                else 
                { 
                    // debug mode; 
                } 
                //if (SerialPortUsingStatus != 1) 
                //{ 
                //    textBox1.AppendText("\r\n-> ");//这里用多行文本进行显示 
                //    SerialPortUsingStatus = 1; 
                //} 
                //textBox1.AppendText(teststr);//这里用多行文本进行显示 
                //textBox1.ScrollToCaret(); 
            })); 
        } 
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                serialPort1.Open(); 
            } 
            catch (Exception) 
            { 
                MessageBox.Show("串口不存在"); 
                return; 
            } 
            serialPort1.ReadTimeout = 500; 
            this.closeToolStripMenuItem.Enabled = true; 
            this.openToolStripMenuItem.Enabled = false; 
            this.settingToolStripMenuItem.Enabled = false; 
            this.buttoninventory.Enabled = true; 
        } 
 
        private void closeToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            if (serialPort1.IsOpen == true) 
            { 
                serialPort1.Close(); 
            } 
            this.openToolStripMenuItem.Enabled = true; 
            this.closeToolStripMenuItem.Enabled = false; 
            this.settingToolStripMenuItem.Enabled = true; 
            this.buttoninventory.Enabled = false; 
        } 
 
        private void debugModeToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            if (this.debugModeToolStripMenuItem.Checked == false) 
            { 
                this.Width = WindowTotalWidth; 
                this.debugModeToolStripMenuItem.Checked = true; 
            } 
            else 
            { 
                this.Width = WindowTotalWidth/2; 
                this.debugModeToolStripMenuItem.Checked = false; 
            } 
        } 
 
        private void buttoninventory_Click(object sender, EventArgs e) 
        { 
            byte[] InventoryBuffer = new byte[InventoryBufferLen]; 
            InventoryBuffer[0] = 0xFE;// packet head; 
            if (this.comboBox1.Text=="TID") 
            { 
                InventoryBuffer[1] = 0xE0;// TID packet format; 
            } 
            else 
            { 
                InventoryBuffer[1] = 0xC0;// EPC packet format; 
            } 
            try 
            { 
                InventoryBuffer[2] = byte.Parse(this.textBox3.Text);// Q value; 
            } 
            catch (Exception) 
            { 
                MessageBox.Show("Content Error!"); 
                return; 
            } 
            byte[] InventoryBuffertemparr = new byte[2]; 
            InventoryBuffertemparr[0] = InventoryBuffer[1]; 
            InventoryBuffertemparr[1] = InventoryBuffer[2]; 
            InventoryBuffer[3] = CRC.CRC7(InventoryBuffertemparr, 2);// packet crc7; 
            if (serialPort1.IsOpen == true) 
            { 
                this.serialPort1.Write(InventoryBuffer, 0, InventoryBufferLen); 
            } 
            try 
            { 
                intervaltime = int.Parse(this.textBox4.Text); 
                power = (int)System.Math.Pow(2,int.Parse(this.textBox3.Text)); 
            } 
            catch (Exception) 
            { 
                MessageBox.Show("Content Error!"); 
                return; 
            } 
            this.timer1.Interval = intervaltime * power; 
            this.timer1.Start(); 
            this.buttoninventory.Enabled = false; 
        } 
 
        private void timer1_Tick(object sender, EventArgs e) 
        { 
            this.timer1.Stop(); 
            this.buttoninventory.Enabled = true; 
            if (this.checkBox2.Checked == true) 
            { 
                Thread.Sleep(100); 
                buttoninventory_Click(sender, e); 
            } 
        } 
    } 
}