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

📄 programtagsform.cs

📁 RFID Reader SampleCode
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Symbol.RFID;

namespace CS_RFIDSample2
{
	/// <summary>
	/// Summary description for ProgramTagsForm.
	/// </summary>
	public class ProgramTagsForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label labelTagData;
		private System.Windows.Forms.Label labelBits;
		private System.Windows.Forms.Label labelTagType;
		private System.Windows.Forms.TextBox textBoxTagType;
		private System.Windows.Forms.Label labelMemoryArea;
		private System.Windows.Forms.TextBox textBoxBits;
		private System.Windows.Forms.ComboBox comboBoxMemoryArea;
		private System.Windows.Forms.TextBox textBoxTagData;
		private System.Windows.Forms.Button buttonOK;
		private Reader theReader;
		private Symbol.RFID.Tag.Types tagType;
		private byte[] tagID;
		private byte tagIDLength;
		//Retain previous settings
		private Gen2Tags.MemoryBank prevMemoryBank;
		private System.Windows.Forms.Button buttonProgram;
		private int prevMemOffset;
		private Symbol.RFID.Tag.Types prevType;

		public ProgramTagsForm(Reader r, Symbol.RFID.Tag.Types type, byte [] TID, byte TIDLength )
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			theReader = r;
			tagType = type;
			tagID = TID;
			tagIDLength = TIDLength;
            this.buttonOK.Focus();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonOK = new System.Windows.Forms.Button();
			this.labelTagData = new System.Windows.Forms.Label();
			this.textBoxTagData = new System.Windows.Forms.TextBox();
			this.labelBits = new System.Windows.Forms.Label();
			this.textBoxBits = new System.Windows.Forms.TextBox();
			this.labelTagType = new System.Windows.Forms.Label();
			this.textBoxTagType = new System.Windows.Forms.TextBox();
			this.labelMemoryArea = new System.Windows.Forms.Label();
			this.comboBoxMemoryArea = new System.Windows.Forms.ComboBox();
			this.buttonProgram = new System.Windows.Forms.Button();
            this.SuspendLayout();
			// 
			// buttonOK
			// 
			this.buttonOK.Location = new System.Drawing.Point(80, 232);
            this.buttonOK.Name = "buttonOK";
            this.buttonOK.Size = new System.Drawing.Size(72, 20);
            this.buttonOK.TabIndex = 0;
			this.buttonOK.Text = "Done";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// labelTagData
			// 
			this.labelTagData.Location = new System.Drawing.Point(16, 32);
            this.labelTagData.Name = "labelTagData";
            this.labelTagData.Size = new System.Drawing.Size(100, 20);
			this.labelTagData.Text = "Tag Data:";
			// 
			// textBoxTagData
			// 
			this.textBoxTagData.Location = new System.Drawing.Point(16, 56);
			this.textBoxTagData.Multiline = true;
            this.textBoxTagData.Name = "textBoxTagData";
			this.textBoxTagData.Size = new System.Drawing.Size(208, 40);
            this.textBoxTagData.TabIndex = 2;
			// 
			// labelBits
			// 
			this.labelBits.Location = new System.Drawing.Point(16, 104);
            this.labelBits.Name = "labelBits";
            this.labelBits.Size = new System.Drawing.Size(100, 20);
			this.labelBits.Text = "Number of Bytes";
			// 
			// textBoxBits
			// 
			this.textBoxBits.Location = new System.Drawing.Point(160, 104);
			this.textBoxBits.MaxLength = 10;
            this.textBoxBits.Name = "textBoxBits";
			this.textBoxBits.ReadOnly = true;
            this.textBoxBits.Size = new System.Drawing.Size(64, 23);
            this.textBoxBits.TabIndex = 3;
			// 
			// labelTagType
			// 
			this.labelTagType.Location = new System.Drawing.Point(16, 8);
            this.labelTagType.Name = "labelTagType";
            this.labelTagType.Size = new System.Drawing.Size(100, 20);
			this.labelTagType.Text = "Tag Type";
			// 
			// textBoxTagType
			// 
			this.textBoxTagType.Location = new System.Drawing.Point(120, 8);
            this.textBoxTagType.Name = "textBoxTagType";
			this.textBoxTagType.ReadOnly = true;
            this.textBoxTagType.Size = new System.Drawing.Size(100, 23);
            this.textBoxTagType.TabIndex = 1;
			// 
			// labelMemoryArea
			// 
			this.labelMemoryArea.Location = new System.Drawing.Point(16, 136);
            this.labelMemoryArea.Name = "labelMemoryArea";
            this.labelMemoryArea.Size = new System.Drawing.Size(100, 20);
			this.labelMemoryArea.Text = "Memory Area";
			// 
			// comboBoxMemoryArea
			// 
			this.comboBoxMemoryArea.Location = new System.Drawing.Point(128, 136);
            this.comboBoxMemoryArea.Name = "comboBoxMemoryArea";
            this.comboBoxMemoryArea.Size = new System.Drawing.Size(100, 23);
            this.comboBoxMemoryArea.TabIndex = 4;
			// 
			// buttonProgram
			// 
			this.buttonProgram.Location = new System.Drawing.Point(72, 192);
            this.buttonProgram.Name = "buttonProgram";
			this.buttonProgram.Size = new System.Drawing.Size(96, 20);
            this.buttonProgram.TabIndex = 5;
			this.buttonProgram.Text = "Program Tag";
			this.buttonProgram.Click += new System.EventHandler(this.buttonProgram_Click);
            this.buttonProgram.KeyDown += new System.Windows.Forms.KeyEventHandler(this.buttonProgram_KeyDown);
			// 
			// ProgramTagsForm
			// 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
			this.ClientSize = new System.Drawing.Size(240, 288);
			this.ControlBox = false;
			this.Controls.Add(this.buttonProgram);
			this.Controls.Add(this.comboBoxMemoryArea);
			this.Controls.Add(this.labelMemoryArea);
			this.Controls.Add(this.textBoxTagType);
			this.Controls.Add(this.labelTagType);
			this.Controls.Add(this.textBoxBits);
			this.Controls.Add(this.labelBits);
			this.Controls.Add(this.textBoxTagData);
			this.Controls.Add(this.labelTagData);
			this.Controls.Add(this.buttonOK);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
            this.Name = "ProgramTagsForm";
			this.Text = "Program Tags";
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.ProgramTagsForm_KeyUp);
			this.Load += new System.EventHandler(this.ProgramTagsForm_Load);
            this.ResumeLayout(false);

		}
		#endregion

		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			//Restore previous settings
			theReader.Parameters.Gen2Tags.MemOffset = prevMemOffset;
			theReader.Parameters.Gen2Tags.MemBank = prevMemoryBank;
			theReader.Parameters.TagType = prevType;
			this.Close();
		}

        private void buttonOK_KeyDown(object sender, KeyEventArgs e)
        {
            // Checks if the key pressed was an enter button (character code 13)
            if (e.KeyValue == (char)13)
                buttonOK_Click(this, e);
        }

		private void ProgramTagsForm_Load(object sender, System.EventArgs e)
		{
			//Store previous settings
			prevMemOffset = theReader.Parameters.Gen2Tags.MemOffset;
			prevMemoryBank = theReader.Parameters.Gen2Tags.MemBank;
			prevType = theReader.Parameters.TagType;
			//Set the programmable tag type to type of the currently selected tag
			theReader.Parameters.TagType = tagType;

			textBoxTagType.Text = tagType.ToString();
			
			//Initialize textBoxTagData to show the selected TagID
			string tagData = "";
			for(int i = 0 ; i < tagIDLength ; i++)
			{
				tagData += tagID[i].ToString("X2");
			}
			textBoxTagData.Text = tagData;
			//initialize textBoxBits to show the bit length of the tagID
			textBoxBits.Text = tagIDLength.ToString();
			
			//Add Items to the comboboxMemoryArea
			comboBoxMemoryArea.Items.Add("EPC");
			if (theReader.Parameters.TagType == Symbol.RFID.Tag.Types.EPC_CLASSG2) 
			{
				comboBoxMemoryArea.Items.Add("User Data");
				comboBoxMemoryArea.Items.Add("TID");
				comboBoxMemoryArea.Items.Add("Access Password");
				comboBoxMemoryArea.Items.Add("Kill Password");
			}
			//Select "EPC" memory by default
			comboBoxMemoryArea.SelectedIndex = 0;
		}

		private void buttonProgram_Click(object sender, System.EventArgs e)
		{
			int byteCount = textBoxTagData.Text.Length / 2 ;
			if (textBoxTagData.Text.Length % 2 != 0) 
			{
				//Do some custom manipulations to handle an error 
				MessageBox.Show("Odd number of bits to program : Ignoring Last bit");
			}
			textBoxBits.Text = byteCount.ToString();
			//Set the memory bank and any offset
			setMemoryBank();
			//Take each two bytes and convert to hex bytes
			byte [] dataBytes = new byte[byteCount];
			int i = 0;
			string s;

			for(int j = 0; j < byteCount ; j++) 
			{
				s = textBoxTagData.Text[i].ToString() + textBoxTagData.Text[i + 1].ToString();
				dataBytes[j] =  byte.Parse(s,System.Globalization.NumberStyles.AllowHexSpecifier);
				i += 2;
			}
			
			try 
			{
				// Program the selected tag 
				byte veryfyAttempts = 3;
				byte eraseAttempts = 3;
				byte programAttempts = 3;
				theReader.Actions.ProgramTags( dataBytes, 
					(byte) dataBytes.Length, 
					veryfyAttempts, eraseAttempts, programAttempts );
			}
			catch(Exception ex)
			{
				System.Windows.Forms.MessageBox.Show(ex.Message, "CS_RFIDSample2");
			}
			finally
			{
				//reset MemOffset that is set during setMemoryBank()
				theReader.Parameters.Gen2Tags.MemOffset = prevMemOffset;
			}
		}

        private void buttonProgram_KeyDown(object sender, KeyEventArgs e)
        {
            // Checks if the key pressed was an enter button (character code 13)
            if (e.KeyValue == (char)13)
                buttonProgram_Click(this, e);
        }

		//set the memory Bank and any offset corresponding to the  selected memory area
		private void setMemoryBank()
		{
			switch (comboBoxMemoryArea.SelectedIndex) 
			{
				case 0: //EPC memory is selected
					theReader.Parameters.Gen2Tags.MemBank = Gen2Tags.MemoryBank.EPC;
					break;
				case 1: //User memory is selected
					theReader.Parameters.Gen2Tags.MemBank = Gen2Tags.MemoryBank.User;
					break;
				case 2: //TID memory is selected
					theReader.Parameters.Gen2Tags.MemBank = Gen2Tags.MemoryBank.TID;
					break;
				case 3: //Reserved memory is selected for Access Password
					theReader.Parameters.Gen2Tags.MemBank = Gen2Tags.MemoryBank.Reserved;
					theReader.Parameters.Gen2Tags.MemOffset = (int) Gen2Tags.MemOffsets.AccessPassword;
					break;
				case 4: //Reserver memory is selected for Kill Password
					theReader.Parameters.Gen2Tags.MemBank = Gen2Tags.MemoryBank.Reserved;
					theReader.Parameters.Gen2Tags.MemOffset = (int) Gen2Tags.MemOffsets.KillPassword;
					break;
			}
		}
        private void ProgramTagsForm_KeyUp(object sender, KeyEventArgs e)
        {
            this.buttonOK.Focus();
        }
	}
}

⌨️ 快捷键说明

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