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

📄 commpingpong.cs

📁 C#串口编程。详细讲解
💻 CS
字号:
using System;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using JH.CommBase;

namespace JH.CommBase
{
	/// <summary>
	/// Overlays CommBase to provide byte-level ping-pong communications were each transmitted byte
	/// illicits a single byte response which must be absorbed before sending the next byte.
	/// There is a default response timeout of 500ms after which a Timeout exception will be raised.
	/// This timeout can be changed by changing the transactTimeout parameter in the settings object.
	/// Use the Transact method for all communications.
	/// </summary>
	public abstract class CommPingPong : CommBase 
	{
		private byte[] RxByte;
		private ManualResetEvent TransFlag = new ManualResetEvent(true);
		private uint TransTimeout;

		/// <summary>
		/// Extends CommBaseSettings to add the settings used by CommLine.
		/// </summary>
		public class CommPingPongSettings : CommBase.CommBaseSettings 
		{
			/// <summary>
			/// Maximum time (ms) for the Transact method to complete (default: 500)
			/// </summary>
			public int transactTimeout = 500;

			public static new CommPingPongSettings LoadFromXML(Stream s)
			{
				return (CommPingPongSettings)LoadFromXML(s, typeof(CommPingPongSettings));
			}
		}
	
		/// <summary>
		/// Transmits a byte and waits for and returns the response byte.
		/// </summary>
		/// <param name="toSend">The byte to be sent.</param>
		/// <returns>The response byte.</returns>
		protected byte Transact(byte toSend) 
		{
			if (RxByte == null) RxByte = new byte[1];
			Send(toSend);
			TransFlag.Reset();
			if (!TransFlag.WaitOne((int)TransTimeout, false)) ThrowException("Timeout");
			byte s;
			lock(RxByte) {s = RxByte[0];}
			return s;
		}
		
		/// <summary>
		/// If a derived class overrides ComSettings(), it must call this prior to returning the settings to
		/// the base class.
		/// </summary>
		/// <param name="s">Class containing the appropriate settings.</param>
		protected void Setup(CommPingPongSettings s) 
		{
			TransTimeout = (uint)s.transactTimeout;
		}

		protected override void OnRxChar(byte ch) 
		{
			lock(RxByte) {RxByte[0] = ch;}
			if (!TransFlag.WaitOne(0,false))
			{
				TransFlag.Set();
			}
		}
	}

}

⌨️ 快捷键说明

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