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

📄 mprreader.cs

📁 WJ Communications RFID example code
💻 CS
📖 第 1 页 / 共 5 页
字号:
					if (TagLengthOptionChanged != null)
						TagLengthOptionChanged(this, new EventArgs());
				}
			}
		}

		/// <summary>
		/// A string describing the COM port and baud rate, or "No Comm" 
		/// if the communications settings are not set.
		/// </summary>
		public string CommParams
		{
			get { return (comm != null)?comm.PortSetting:"No Comm"; }
		}

		private TimeSpan persistTime = new TimeSpan(0,0,0,1,0);
		/// <summary>
		/// How long a tag that has been read will persist in the inventory,
		/// without being read, while an inventory is running.
		/// 
		/// If an inventory is stopped, tags do not expire.
		/// If a tag is re-read, it will live at least another peristTime.
		/// </summary>
		public TimeSpan PersistTime
		{ 
			get { return persistTime; }
			set 
			{ 
				if (value != persistTime) 
				{
					persistTime = value;

					if (PersistTimeChanged != null)
						PersistTimeChanged(this, new EventArgs());
				}
			}
		}

		private TimeSpan invUpdateGap = new TimeSpan(0,0,0,0,250);
		/// <summary>
		/// How much time to wait between calls to UpdateInventory when
		/// an inventory loop is running.
		/// </summary>
		public int InvUpdateGap
		{ 
			get { return (int)invUpdateGap.TotalMilliseconds; }
			set 
			{ 
				if (value != (int)invUpdateGap.TotalMilliseconds) 
				{
					invUpdateGap = (value == int.MaxValue) ?
						TimeSpan.MaxValue : new TimeSpan(0, 0, 0, value / 1000, value % 1000);

					KickInvTimer();

					if (InvUpdateGapChanged != null)
						InvUpdateGapChanged(this, new EventArgs());
				}
			}
		}


		private bool invTimerEnabled = false;
		/// <summary>
		/// Whether an inventory is actively running.
		/// When set to true, a timer is started that will expire after InvUpdateGap and call UpdateInventory()
		/// </summary>
		public bool InvTimerEnabled
		{
			get { return invTimerEnabled; }
			set 
			{
				if (value != invTimerEnabled)
				{
					invTimerEnabled = value;
					if (InvTimerEnabledChanged != null)
						InvTimerEnabledChanged(this, new EventArgs());

					if (invTimerEnabled) // If enabling, have timer expire immediately
					{
						InventoryUpdateTimer.Change(0, Timeout.Infinite);

						Thread.Sleep(0);
					}
					else
						InventoryUpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
				}
			}
		}
	
		/// <summary>
		/// Returns the inventory (list of tags) that have been read by the reader,
		/// but not expired.
		/// </summary>
		public Inventory TagInventory
		{
			get { return inventory; }
		}

		#region Manufacturing Information

		private string _DeviceModelNumber;
		private string _DeviceSerialNumber;
		private string _HardwareVersion;
		private string _ManufactureDate;
		private string _ManufacturerName;
		private string _FirmwareVersion;
		private int _FirmwareMinorVersion;
		private int _FirmwareMajorVersion;
		private string _BootLoaderVersion;
		private int _BootLoaderMinorVersion;
		private int _BootLoaderMajorVersion;

		/// <summary>
		/// Device Model Number is read and returned by the MPR "Get Information" Command.
		/// A string of 8 ASCII bytes.
		/// </summary>
		public string DeviceModelNumber { get { return _DeviceModelNumber; } }
		/// <summary>
		/// Device Serial Number is read and returned by the MPR "Get Information" Command.
		/// A string of 12 ASCII bytes.
		/// </summary>
		public string DeviceSerialNumber { get { return _DeviceSerialNumber; } }
		/// <summary>
		/// Hardware Version Number is read and returned by the MPR "Get Information" Command.
		/// A string of 8 ASCII bytes.
		/// </summary>
		public string HardwareVersion  { get { return _HardwareVersion; } }
		/// <summary>
		/// Manufacture Date is read and returned by the MPR "Get Information" Command.
		/// A string of 8 ASCII bytes.
		/// </summary>
		public string ManufactureDate  { get { return _ManufactureDate; } }
		/// <summary>
		/// Manufacturer Name is read and returned by the MPR "Get Information" Command.
		/// A string of 16 ASCII bytes.
		/// </summary>
		public string ManufacturerName  { get { return _ManufacturerName; } }
		/// <summary>
		/// Firmware Version Number is read and returned by the MPR "Get Information" Command as 2 bytes.
		/// This is converted to a string with dotted decimal notation.
		/// </summary>
		public string FirmwareVersion	{ get { return _FirmwareVersion; } }

		/// <summary>
		/// Firmware Major Version is MSB of the FirmwareVersion
		/// </summary>
		public int FirmwareMajorVersion	{ get { return _FirmwareMajorVersion; } }
		/// <summary>
		/// Firmware Major Version is LSB of the FirmwareVersion
		/// </summary>
		public int FirmwareMinorVersion	{ get { return _FirmwareMinorVersion; } }

		/// <summary>
		/// Bootloader Version Number is read and returned by the MPR "Get Information" Command as 2 bytes.
		/// This is converted to a string with dotted decimal notation.
		/// </summary>
		public string BootLoaderVersion  { get { return _BootLoaderVersion; } }

		/// <summary>
		/// Bootloader Major Version is MSB of the BootloaderVersion
		/// </summary>
		public int BootloaderMajorVersion	{ get { return _BootLoaderMajorVersion; } }
		/// <summary>
		/// Bootloader Major Version is LSB of the BootloaderVersion
		/// </summary>
		public int BootloaderMinorVersion	{ get { return _BootLoaderMinorVersion; } }
        
		/// <summary>
		/// Update the Reader's Manufacturing Information
		/// </summary>
		/// <returns>Whether the reader's manu info read was successful</returns>
		public bool UpdateReaderInfo()
		{
			MPRMsg aMsg = comm.Send(CmdCode.ReaderInfo, null);

			bool CouldParseMsg = false;

			if (!ProcMsgStat(aMsg)) 
			{
				_DeviceModelNumber = "unknown";
				_DeviceSerialNumber = "unknown";
				_HardwareVersion = "unknown";
				_ManufactureDate = "unknown";
				_ManufacturerName = "unknown";
				_FirmwareVersion = "unknown";
				_BootLoaderVersion = "unknown";

				CouldParseMsg = false;
			} 
			else if (aMsg.DataBytes.Count == 10)	// For Pre-production firmware
			{
				_DeviceModelNumber = "";
				_DeviceSerialNumber = aMsg.DataBytes.subList(0,8).ToHexString();
				_HardwareVersion = "";
				_ManufactureDate = "";
				_ManufacturerName = "";
				_FirmwareVersion = aMsg.DataBytes.subList(8,10).ToHexString(".");
				try 
				{
					string[] splits = _FirmwareVersion.Split(new char[] {'.'});
					_FirmwareMinorVersion = int.Parse(splits[1], System.Globalization.NumberStyles.AllowHexSpecifier);
					_FirmwareMajorVersion = int.Parse(splits[0], System.Globalization.NumberStyles.AllowHexSpecifier);
				} 
				catch 
				{
					_FirmwareMinorVersion = 0;
					_FirmwareMajorVersion = 0;
				}
				_BootLoaderVersion = "";
				CouldParseMsg = true;
			}
			else if (aMsg.DataBytes.Count == 56)	// Production firmware ( >= 01.00)
			{
				_DeviceModelNumber = aMsg.DataBytes.subList(0,8).ToString();
				_DeviceSerialNumber = aMsg.DataBytes.subList(8,20).ToString();
				_HardwareVersion = aMsg.DataBytes.subList(20,28).ToString();
				_ManufactureDate = aMsg.DataBytes.subList(28,36).ToString();
				_ManufacturerName = aMsg.DataBytes.subList(36,52).ToString();
				_FirmwareVersion = aMsg.DataBytes.subList(52,54).ToHexString(".");
				_BootLoaderVersion = aMsg.DataBytes.subList(54,56).ToHexString(".");
				try 
				{
					string[] splits = _FirmwareVersion.Split(new char[] {'.'});
					_FirmwareMinorVersion = int.Parse(splits[1], System.Globalization.NumberStyles.AllowHexSpecifier);
					_FirmwareMajorVersion = int.Parse(splits[0], System.Globalization.NumberStyles.AllowHexSpecifier);
					splits = _BootLoaderVersion.Split(new char[] {'.'});
					_BootLoaderMinorVersion = int.Parse(splits[1], System.Globalization.NumberStyles.AllowHexSpecifier);
					_BootLoaderMajorVersion = int.Parse(splits[0], System.Globalization.NumberStyles.AllowHexSpecifier);
				} 
				catch 
				{
					_BootLoaderMinorVersion = 0;
					_BootLoaderMajorVersion = 0;
				}
				_BootLoaderVersion = aMsg.DataBytes.subList(54,56).ToHexString(".");
				CouldParseMsg = true;
			}

			RecomputeTxPowerLimits();

			if (ManufacturingInformationChanged != null)
				ManufacturingInformationChanged(this, new EventArgs());
			
			return CouldParseMsg;
		}

		/// <summary>
		/// Tx Power Limits are a function of the model type, as reported by the MPR
		/// during a "Read Information" Command.
		/// </summary>
		private void RecomputeTxPowerLimits() 
		{
			if ((FirmwareMajorVersion >= 1) || (FirmwareMinorVersion >= 24)) 
			{
				if ((_DeviceModelNumber.Trim() == "MPR5000") || (_DeviceModelNumber.Trim() == "MPR6000"))
				{
					txPowerMin = 15;
					txPowerMax = 27;
				} 
				else if ((_DeviceModelNumber.Trim() == "MPR7000") || (_DeviceModelNumber.Trim() == "MPR7100"))
				{
					txPowerMin = 18;
					txPowerMax = 30;
				}
				else 
				{
					txPowerMin = 0;
					txPowerMax = 255;
				}
			}
		}

		#endregion

		#region Inventories
		/// <summary>
		/// Request the EPC Gen 1 Class 0 Inventory from the MPR.
		/// </summary>
		public Inventory Class0Inventory
		{
			get 
			{
				byteList InvParams;

				// If unknown FWVersion, just return an empty inventory

				if (this.FirmwareVersion == "unknown")
					return new Inventory();

				// >= Rev 8 FW Class0Inventory parameter change:
				// Removed "Data Bit Flag/Cnt" field
				// byteList InvParams = new byteList(new byte[] {ActiveAntenna, TxPower, SingulationField, 0});

				if ((FirmwareMajorVersion > 0) || (FirmwareMinorVersion <= 7))
					InvParams = new byteList(new byte[] {ActiveAntenna, TxPower, Class0SingulationField});
				else
					InvParams = new byteList(new byte[] {ActiveAntenna, TxPower, Class0SingulationField, 0});

				InvParams.Add(tagFilter.Length);	// Add the filter bit count
				InvParams.Add(tagFilter.Bits);			// Add the filter bytes

				MPRMsg aMsg = comm.Send(CmdCode.Class0Read, InvParams);
				if (!ProcMsgStat(aMsg))
					return new Inventory();

				return InventoryParse(aMsg.DataBytes, RFIDProtocol.EPCClass0);
			}
		}


		/// <summary>
		/// Request the EPC Gen 1 Class 1 Inventory from the MPR.
		/// </summary>
		public Inventory Class1Inventory
		{
			get
			{
				byteList InvParams = new byteList(new byte[] {ActiveAntenna, TxPower});
				CmdCode C1InvCmd;
				switch (class1InventoryType) 
				{
					case 0:
						C1InvCmd = CmdCode.Class1ReadTagsWP;
						break;
					case 1:
						C1InvCmd = CmdCode.Class1Read;
						break;
					case 2:
						C1InvCmd = CmdCode.Class1ReadSingleTag;
						InvParams.Add(10);	// Add Single Tag read try count
						break;
					default:
						C1InvCmd = CmdCode.Class1ReadTagsWP;
						break;
				}

				InvParams.Add(tagFilter.Length);	// Add the filter bit count
				InvParams.Add(tagFilter.Bits);			// Add the filter bytes

				if (tagLengthOption != TagLengthOptionEnum.EPC_NoParam)
				{
					InvParams.Add((byte)tagLengthOption);
				}

				MPRMsg aMsg = comm.Send(C1InvCmd, InvParams);
				
				if (!ProcMsgStat(aMsg))
					return new Inventory();

				return InventoryParse(aMsg.DataBytes, RFIDProtocol.EPCClass1, tagLengthOption);
			}
		}

		private Gen2InventorySettingsStruct gen2InventorySettings = new Gen2InventorySettingsStruct(0,1,0,0);
		/// <summary>
		/// Settings for Gen 2 Inventories.
		/// </summary>
		public Gen2InventorySettingsStruct Gen2InventorySettings
		{
			get { return gen2InventorySettings; }
			set { gen2InventorySettings = value; }
		}

		/// <summary>
		/// Request the UHF Gen 2 Class 1 Inventory from the MPR.
		/// </summary>
		public Inventory Gen2Inventory
		{
			get 
			{
				byteList InvParams = new byteList(new byte[] {(byte)Gen2Subcommands.Inventory, ActiveAntenna, TxPower});
				InvParams.Add(gen2InventorySettings.sel);
				InvParams.Add(gen2InventorySettings.session);
				InvParams.Add(gen2InventorySettings.target);
				InvParams.Add(gen2InventorySettings.startingQ);

				MPRMsg aMsg = comm.Send(CmdCode.Gen2Command, InvParams);
				if (!ProcMsgStat(aMsg))
					return new Inventory();

				return G2InventoryParse(aMsg.DataBytes);
			}
		}

		#endregion

		/// <summary>
		/// The method called when the InventoryUpdateTimer expires.
		/// Updates the Inventories,
		/// Fires an InvPollEvent,
		/// and restarts the InventoryUpdateTimer, if still enabled.
		/// </summary>
		/// <param name="o"></param>
		private void InventoryUpdateCallback(object o)
		{
			try

⌨️ 快捷键说明

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