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

📄 tagfilter.cs

📁 WJ Communications RFID example code
💻 CS
字号:
//==========================================================================================
//
//	WJ.MPR.Reader.TagFilter
//	Copyright (c) 2006, WJ Communications, Inc.
//
//==========================================================================================
using System;
using WJ.MPR.Util;

namespace WJ.MPR.Reader
{
	/// <summary>
	///	A Class that encapsulates an RFID Tag ID filter.
	///	TagFilters are used to select a subset of the Tag ID space.
	///	They are really just an array of bits.
	///	This class includes a static method to convert a string to an array of bits.
	/// </summary>
	public struct TagFilter
	{
		/// <summary>
		/// The number of bits to use from the Bits array.
		/// </summary>
		public byte Length;

		/// <summary>
		/// A byte array that stores the bits of the filter.
		/// </summary>
		public byte[] Bits;

		/// <summary>
		/// Constructs a TagFilter from a string of hexadecimal digits.
		/// </summary>
		/// <param name="length">The number of bits to store from the string.</param>
		/// <param name="filter">A string of hexadecimal digits from which to form the TagFilter.</param>
		public TagFilter(byte length, string filter)
		{
			this.Length = length; 
			byteList bitList = ParseID(filter);

			if (BitsToBytes(Length) > bitList.Count) 
			{
				// Pad bitList with enough zeros to make length
				bitList.Add(new byte[BitsToBytes(Length) - bitList.Count]);
			}
			else 
			{
				// Remove bonus zeros at end, if there are any
				while (bitList.Count > BitsToBytes(Length))
					bitList.RemoveAt(bitList.Count-1);
			}

			Bits = bitList.ToArray();

			byte LastByteMask = (byte)(0xFF00 >> (Length % 8));

			// Apply last byte mask, assuming there are at least some bits in filter
			if ((Length % 8) != 0)
				Bits[Bits.Length-1] &= LastByteMask;
		}

		/// <summary>
		/// Convert a number of bits to the number of bytes required to store them.
		/// </summary>
		/// <param name="numBits">A number of bits.</param>
		/// <returns>The number of bytes.</returns>
		public static int BitsToBytes(int numBits)
		{
			return ((numBits + 7) / 8);
		}

		/// <summary>
		/// Parse the TagID in a string, and convert it to a byteList.
		/// </summary>
		/// <param name="IDstring"></param>
		/// <returns></returns>
		public static byteList ParseID(string IDstring)
		{
			byteList IDbytes = new byteList();

			// Trim out leading & trailing whitespace, and convert to upper
			IDstring = IDstring.Trim().ToUpper();

			// Add one space at the end to trigger adding the last byte to the list.
			IDstring += " ";

			byte Val = 0;
			bool SecondChar = false;

			for(int i = 0; i < IDstring.Length; i++)
			{
				if (IDstring[i] == ' ')
				{
					if (SecondChar)
					{
						IDbytes.Add(Val);
						Val = 0;
						SecondChar = false;
					}
				}
				else if (IDstring[i] <= '9' && IDstring[i] >= '0' )
				{
					Val = (byte)((Val << 4) | (byte)(IDstring[i] - '0'));
					if (SecondChar)
					{
						IDbytes.Add(Val);
						Val = 0;
					}
					SecondChar = !SecondChar;
				}
				else if (IDstring[i] <= 'F' && IDstring[i] >= 'A')
				{
					Val = (byte)((Val << 4) | (byte)(IDstring[i] - 'A' + 10));
					if (SecondChar) 
					{
						IDbytes.Add(Val);
						Val = 0;
					}
					SecondChar = !SecondChar;
				}
				else // a non-hexdigit, non-space found, get out!
					return IDbytes;
			}
			return IDbytes;
		}
	}
}

⌨️ 快捷键说明

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