亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? commbase.cs

?? 微軟C#讀寫類
?? CS
?? 第 1 頁 / 共 3 頁
字號:
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.IO;
using System.Xml.Serialization;

namespace JH.CommBase
{

	/// <summary>
	/// Lowest level Com driver handling all Win32 API calls and processing send and receive in terms of
	/// individual bytes. Used as a base class for higher level drivers.
	/// </summary>
	public abstract class CommBase : IDisposable
	{
		private IntPtr hPort;
		private IntPtr ptrUWO = IntPtr.Zero;
		private Thread rxThread = null;
		private bool online = false;
		private bool auto = false;
		private bool checkSends = true;
		private Exception rxException = null;
		private bool rxExceptionReported = false;
		private int writeCount = 0;
		private ManualResetEvent writeEvent = new ManualResetEvent(false);
		private int stateRTS = 2;
		private int stateDTR = 2;
		private int stateBRK = 2;
		
		/// <summary>
		/// Parity settings
		/// </summary>
		public enum Parity 
		{
			/// <summary>
			/// Characters do not have a parity bit.
			/// </summary>
			none = 0,
			/// <summary>
			/// If there are an odd number of 1s in the data bits, the parity bit is 1.
			/// </summary>
			odd = 1,
			/// <summary>
			/// If there are an even number of 1s in the data bits, the parity bit is 1.
			/// </summary>
			even = 2,
			/// <summary>
			/// The parity bit is always 1.
			/// </summary>
			mark = 3,
			/// <summary>
			/// The parity bit is always 0.
			/// </summary>
			space = 4
		};

		/// <summary>
		/// Stop bit settings
		/// </summary>
		public enum StopBits
		{
			/// <summary>
			/// Line is asserted for 1 bit duration at end of each character
			/// </summary>
			one = 0,
			/// <summary>
			/// Line is asserted for 1.5 bit duration at end of each character
			/// </summary>
			onePointFive = 1,
			/// <summary>
			/// Line is asserted for 2 bit duration at end of each character
			/// </summary>
			two = 2
		};

		/// <summary>
		/// Uses for RTS or DTR pins
		/// </summary>
		public enum HSOutput
		{
			/// <summary>
			/// Pin is asserted when this station is able to receive data.
			/// </summary>
			handshake = 2,
			/// <summary>
			/// Pin is asserted when this station is transmitting data (RTS on NT, 2000 or XP only).
			/// </summary>
			gate = 3,
			/// <summary>
			/// Pin is asserted when this station is online (port is open).
			/// </summary>
			online = 1,
			/// <summary>
			/// Pin is never asserted.
			/// </summary>
			none = 0
		};

		/// <summary>
		/// Standard handshake methods
		/// </summary>
		public enum Handshake
		{
			/// <summary>
			/// No handshaking
			/// </summary>
			none,
			/// <summary>
			/// Software handshaking using Xon / Xoff
			/// </summary>
			XonXoff,
			/// <summary>
			/// Hardware handshaking using CTS / RTS
			/// </summary>
			CtsRts,
			/// <summary>
			/// Hardware handshaking using DSR / DTR
			/// </summary>
			DsrDtr
		}
		
		/// <summary>
		/// Set the public fields to supply settings to CommBase.
		/// </summary>
		public class CommBaseSettings 
		{
			/// <summary>
			/// Port Name (default: "COM1:")
			/// </summary>
			public string port = "COM1:";
			/// <summary>
			/// Baud Rate (default: 2400) unsupported rates will throw "Bad settings"
			/// </summary>
			public int baudRate = 2400;
			/// <summary>
			/// The parity checking scheme (default: none)
			/// </summary>
			public Parity parity = Parity.none;
			/// <summary>
			/// Number of databits 1..8 (default: 8) unsupported values will throw "Bad settings"
			/// </summary>
			public int dataBits = 8;
			/// <summary>
			/// Number of stop bits (default: one)
			/// </summary>
			public StopBits stopBits = StopBits.one;
			/// <summary>
			/// If true, transmission is halted unless CTS is asserted by the remote station (default: false)
			/// </summary>
			public bool txFlowCTS = false;
			/// <summary>
			/// If true, transmission is halted unless DSR is asserted by the remote station (default: false)
			/// </summary>
			public bool txFlowDSR = false;
			/// <summary>
			/// If true, transmission is halted when Xoff is received and restarted when Xon is received (default: false)
			/// </summary>
			public bool txFlowX = false;
			/// <summary>
			/// If false, transmission is suspended when this station has sent Xoff to the remote station (default: true)
			/// Set false if the remote station treats any character as an Xon.
			/// </summary>
			public bool txWhenRxXoff = true;
			/// <summary>
			/// If true, received characters are ignored unless DSR is asserted by the remote station (default: false)
			/// </summary>
			public bool rxGateDSR = false;
			/// <summary>
			/// If true, Xon and Xoff characters are sent to control the data flow from the remote station (default: false)
			/// </summary>
			public bool rxFlowX = false;
			/// <summary>
			/// Specifies the use to which the RTS output is put (default: none)
			/// </summary>
			public HSOutput useRTS = HSOutput.none;
			/// <summary>
			/// Specidies the use to which the DTR output is put (default: none)
			/// </summary>
			public HSOutput useDTR = HSOutput.none;
			/// <summary>
			/// The character used to signal Xon for X flow control (default: DC1)
			/// </summary>
			public ASCII XonChar = ASCII.DC1;
			/// <summary>
			/// The character used to signal Xoff for X flow control (default: DC3)
			/// </summary>
			public ASCII XoffChar = ASCII.DC3;
			/// <summary>
			/// The number of free bytes in the reception queue at which flow is disabled (default: 2048)
			/// </summary>
			public int rxHighWater = 2048;
			/// <summary>
			/// The number of bytes in the reception queue at which flow is re-enabled (default: 512)
			/// </summary>
			public int rxLowWater = 512;
			/// <summary>
			/// Multiplier. Max time for Send in ms = (Multiplier * Characters) + Constant
			/// (default: 0 = No timeout)
			/// </summary>
			public int sendTimeoutMultiplier = 0;
			/// <summary>
			/// Constant.  Max time for Send in ms = (Multiplier * Characters) + Constant (default: 0)
			/// </summary>
			public int sendTimeoutConstant = 0;
			/// <summary>
			/// Requested size for receive queue (default: 0 = use operating system default)
			/// </summary>
			public int rxQueue = 0;
			/// <summary>
			/// Requested size for transmit queue (default: 0 = use operating system default)
			/// </summary>
			public int txQueue = 0;
			/// <summary>
			/// If true, the port will automatically re-open on next send if it was previously closed due
			/// to an error (default: false)
			/// </summary>
			public bool autoReopen = false;

			/// <summary>
			/// If true, subsequent Send commands wait for completion of earlier ones enabling the results
			/// to be checked. If false, errors, including timeouts, may not be detected, but performance
			/// may be better.
			/// </summary>
			public bool checkAllSends = true;

			/// <summary>
			/// Pre-configures settings for most modern devices: 8 databits, 1 stop bit, no parity and
			/// one of the common handshake protocols. Change individual settings later if necessary.
			/// </summary>
			/// <param name="Port">The port to use (i.e. "COM1:")</param>
			/// <param name="Baud">The baud rate</param>
			/// <param name="Hs">The handshake protocol</param>
			public void SetStandard(string Port, int Baud, Handshake Hs)
			{
				dataBits = 8; stopBits = StopBits.one; parity = Parity.none;
				port = Port; baudRate = Baud;
				switch (Hs)
				{
					case Handshake.none:
						txFlowCTS = false; txFlowDSR = false; txFlowX = false;
						rxFlowX = false; useRTS = HSOutput.online; useDTR = HSOutput.online;
						txWhenRxXoff = true; rxGateDSR = false;
						break;
					case Handshake.XonXoff:
						txFlowCTS = false; txFlowDSR = false; txFlowX = true;
						rxFlowX = true; useRTS = HSOutput.online; useDTR = HSOutput.online;
						txWhenRxXoff = true; rxGateDSR = false;
						XonChar = ASCII.DC1; XoffChar = ASCII.DC3;
						break;
					case Handshake.CtsRts:
						txFlowCTS = true; txFlowDSR = false; txFlowX = false;
						rxFlowX = false; useRTS = HSOutput.handshake; useDTR = HSOutput.online;
						txWhenRxXoff = true; rxGateDSR = false;
						break;
					case Handshake.DsrDtr:
						txFlowCTS = false; txFlowDSR = true; txFlowX = false;
						rxFlowX = false; useRTS = HSOutput.online; useDTR = HSOutput.handshake;
						txWhenRxXoff = true; rxGateDSR = false;
						break;
				}
			}

			/// <summary>
			/// Save the object in XML format to a stream
			/// </summary>
			/// <param name="s">Stream to save the object to</param>
			public void SaveAsXML(Stream s)
			{
				XmlSerializer sr = new XmlSerializer(this.GetType());
				sr.Serialize(s, this);
			}

			/// <summary>
			/// Create a new CommBaseSettings object initialised from XML data
			/// </summary>
			/// <param name="s">Stream to load the XML from</param>
			/// <returns>CommBaseSettings object</returns>
			public static CommBaseSettings LoadFromXML(Stream s)
			{
				return LoadFromXML(s, typeof(CommBaseSettings));
			}

			/// <summary>
			/// Create a new object loading members from the stream in XML format.
			/// Derived class should call this from a static method i.e.:
			/// return (ComDerivedSettings)LoadFromXML(s, typeof(ComDerivedSettings));
			/// </summary>
			/// <param name="s">Stream to load the object from</param>
			/// <param name="t">Type of the derived object</param>
			/// <returns></returns>
			protected static CommBaseSettings LoadFromXML(Stream s, Type t)
			{
				XmlSerializer sr = new XmlSerializer(t);
				try
				{
					return (CommBaseSettings)sr.Deserialize(s);
				}
				catch
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Byte type with enumeration constants for ASCII control codes.
		/// </summary>
		public enum ASCII : byte
		{
			NULL = 0x00,SOH = 0x01,STH = 0x02,ETX = 0x03,EOT = 0x04,ENQ = 0x05,ACK = 0x06,BELL = 0x07,
			BS = 0x08,HT = 0x09,LF = 0x0A,VT = 0x0B,FF = 0x0C,CR = 0x0D,SO = 0x0E,SI = 0x0F,DC1 = 0x11,
			DC2 = 0x12,DC3 = 0x13,DC4 = 0x14,NAK = 0x15,SYN = 0x16,ETB = 0x17,CAN = 0x18,EM = 0x19,
			SUB = 0x1A,ESC = 0x1B,FS = 0x1C,GS = 0x1D,RS = 0x1E,US = 0x1F,SP = 0x20,DEL = 0x7F
		}
	
		/// <summary>
		/// Opens the com port and configures it with the required settings
		/// </summary>
		/// <returns>false if the port could not be opened</returns>
		public bool Open() 
		{
			Win32Com.DCB PortDCB = new Win32Com.DCB();
			Win32Com.COMMTIMEOUTS CommTimeouts = new Win32Com.COMMTIMEOUTS();
			CommBaseSettings cs;
			Win32Com.OVERLAPPED wo = new Win32Com.OVERLAPPED();

			if (online) return false;
			cs = CommSettings();

			hPort = Win32Com.CreateFile(cs.port, Win32Com.GENERIC_READ | Win32Com.GENERIC_WRITE, 0, IntPtr.Zero,
				Win32Com.OPEN_EXISTING, Win32Com.FILE_FLAG_OVERLAPPED, IntPtr.Zero);
			if (hPort == (IntPtr)Win32Com.INVALID_HANDLE_VALUE)
			{
				if (Marshal.GetLastWin32Error() == Win32Com.ERROR_ACCESS_DENIED)
				{
					return false;
				}
				else
				{
					throw new CommPortException("Port Open Failure");
				}
			}

			online = true;

			CommTimeouts.ReadIntervalTimeout = 0;
			CommTimeouts.ReadTotalTimeoutConstant = 0;
			CommTimeouts.ReadTotalTimeoutMultiplier = 0;
			CommTimeouts.WriteTotalTimeoutConstant = cs.sendTimeoutConstant;
			CommTimeouts.WriteTotalTimeoutMultiplier = cs.sendTimeoutMultiplier;
			PortDCB.init(((cs.parity == Parity.odd) || (cs.parity == Parity.even)), cs.txFlowCTS, cs.txFlowDSR,
				(int)cs.useDTR, cs.rxGateDSR, !cs.txWhenRxXoff, cs.txFlowX, cs.rxFlowX, (int)cs.useRTS);
			PortDCB.BaudRate = cs.baudRate;
			PortDCB.ByteSize = (byte)cs.dataBits;
			PortDCB.Parity = (byte)cs.parity;
			PortDCB.StopBits = (byte)cs.stopBits;
			PortDCB.XoffChar = (byte)cs.XoffChar;
			PortDCB.XonChar = (byte)cs.XonChar;
			PortDCB.XoffLim = (short)cs.rxHighWater;
			PortDCB.XonLim = (short)cs.rxLowWater;
			if ((cs.rxQueue != 0) || (cs.txQueue != 0))
				if (!Win32Com.SetupComm(hPort, (uint)cs.rxQueue, (uint)cs.txQueue)) ThrowException("Bad queue settings");
			if (!Win32Com.SetCommState(hPort, ref PortDCB)) ThrowException("Bad com settings");
			if (!Win32Com.SetCommTimeouts(hPort, ref CommTimeouts)) ThrowException("Bad timeout settings");

			stateBRK = 0;
			if (cs.useDTR == HSOutput.none) stateDTR = 0;
			if (cs.useDTR == HSOutput.online) stateDTR = 1;
			if (cs.useRTS == HSOutput.none) stateRTS = 0;
			if (cs.useRTS == HSOutput.online) stateRTS = 1;

			checkSends = cs.checkAllSends;
			wo.Offset = 0;
			wo.OffsetHigh = 0;
			if (checkSends)
				wo.hEvent = writeEvent.Handle;
			else
				wo.hEvent = IntPtr.Zero;
			ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf(wo));
			Marshal.StructureToPtr(wo, ptrUWO, true);
			writeCount = 0;
						
			rxException = null;
			rxExceptionReported = false;
			rxThread = new Thread(new ThreadStart(this.ReceiveThread));
			rxThread.Name = "CommBaseRx";
			rxThread.Priority = ThreadPriority.AboveNormal;
			rxThread.Start();
			Thread.Sleep(1); //Give rx thread time to start. By documentation, 0 should work, but it does not!

			auto = false;
			if (AfterOpen()) 
			{
				auto = cs.autoReopen;
				return true;
			}
			else 
			{
				Close();
				return false;
			}
		}

		/// <summary>
		/// Closes the com port.
		/// </summary>
		public void Close()
		{
			if (online)
			{
				auto = false;
				BeforeClose(false);
				InternalClose();
				rxException = null;
			}
		}
		
		private void InternalClose() 
		{
			Win32Com.CancelIo(hPort);
			if (rxThread != null)
			{
				rxThread.Abort();
				rxThread = null;
			}
			Win32Com.CloseHandle(hPort);
			if (ptrUWO != IntPtr.Zero) Marshal.FreeHGlobal(ptrUWO);
			stateRTS = 2;
			stateDTR = 2;
			stateBRK = 2;
			online = false;
		}
		
		/// <summary>
		/// For IDisposable
		/// </summary>
		public void Dispose() {Close();}

		/// <summary>
		/// Destructor (just in case)
		/// </summary>
		~CommBase() {Close();}
		
		/// <summary>
		/// True if online.
		/// </summary>
		public bool Online {get {if (!online) return false; else return CheckOnline();}}

		/// <summary>
		/// Block until all bytes in the queue have been transmitted.
		/// </summary>
		public void Flush() {
			CheckOnline();
			CheckResult();
		}

		/// <summary>
		/// Use this to throw exceptions in derived classes. Correctly handles threading issues
		/// and closes the port if necessary.
		/// </summary>
		/// <param name="reason">Description of fault</param>
		protected void ThrowException(string reason)
		{
			if (Thread.CurrentThread == rxThread)
			{

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产麻豆午夜三级精品| 成人综合在线视频| 国产精品亚洲人在线观看| 99久久国产综合精品女不卡| 6080国产精品一区二区| 综合欧美亚洲日本| 国产最新精品免费| 在线成人免费视频| 亚洲欧美另类综合偷拍| 国产精品自拍毛片| 欧美成人午夜电影| 三级欧美在线一区| 欧美在线观看视频在线| 亚洲国产岛国毛片在线| 国产美女精品一区二区三区| 在线观看91精品国产麻豆| 夜夜揉揉日日人人青青一国产精品| 国产精品996| 欧美va亚洲va在线观看蝴蝶网| 亚洲电影一级片| 色诱视频网站一区| 亚洲欧美成人一区二区三区| 成人h动漫精品| 欧美极品aⅴ影院| 狠狠色综合日日| 精品福利一区二区三区| 老司机精品视频一区二区三区| 欧美日韩aaa| 亚洲大片在线观看| 在线免费不卡视频| 亚洲免费在线观看| 在线视频综合导航| 一区2区3区在线看| 欧美日韩亚洲综合| 日韩综合小视频| 欧美一区二区黄色| 精品亚洲国产成人av制服丝袜| 精品久久人人做人人爽| 国产在线不卡一卡二卡三卡四卡| 久久久蜜桃精品| 不卡免费追剧大全电视剧网站| 中文字幕中文在线不卡住| 91蝌蚪国产九色| 亚洲第四色夜色| 日韩欧美色综合网站| 国产伦精品一区二区三区视频青涩 | 最新欧美精品一区二区三区| 99久久精品免费看| 亚洲最新在线观看| 91精品一区二区三区久久久久久| 奇米影视一区二区三区小说| 精品成人私密视频| 成人午夜视频福利| 亚洲日穴在线视频| 91精品国产麻豆国产自产在线| 久久99国内精品| 国产精品三级久久久久三级| 在线精品视频免费观看| 久久国产精品免费| 日本一二三四高清不卡| 欧美午夜理伦三级在线观看| 日韩精品一级二级| 国产精品国产三级国产aⅴ原创 | 成人午夜在线播放| 亚洲乱码国产乱码精品精98午夜 | 天天影视色香欲综合网老头| 日韩美女视频在线| 色哟哟国产精品| 久久国产尿小便嘘嘘尿| 亚洲男人天堂一区| 日韩欧美综合一区| 99在线精品视频| 久久99国产乱子伦精品免费| 一区二区三区在线观看视频| 日韩免费视频一区二区| 91官网在线免费观看| 极品少妇一区二区三区精品视频| 亚洲欧美区自拍先锋| 久久综合九色综合欧美98| 欧美综合久久久| 成人丝袜高跟foot| 麻豆91精品91久久久的内涵| 亚洲免费在线视频| 亚洲国产高清aⅴ视频| 欧美一区二区三区电影| 91片黄在线观看| 国产成人在线影院| 日本成人中文字幕在线视频| 亚洲专区一二三| 日韩一区有码在线| 日本一区二区免费在线观看视频| 9191国产精品| 欧美日韩日日摸| 91福利在线免费观看| 成人综合婷婷国产精品久久蜜臀| 麻豆精品一区二区av白丝在线| 亚洲国产乱码最新视频 | 国产精品久久久久影视| 26uuu国产一区二区三区| 欧美高清www午色夜在线视频| 日本精品视频一区二区| av在线播放一区二区三区| 国产精品91xxx| 国产福利电影一区二区三区| 精品一区二区在线视频| 丝袜美腿亚洲色图| 日日夜夜精品视频天天综合网| 亚洲美女视频在线| 亚洲欧美一区二区三区孕妇| 亚洲视频中文字幕| 亚洲视频一区二区在线| 日韩一区在线免费观看| 亚洲色图欧美偷拍| 成人欧美一区二区三区| 亚洲视频一二三| 亚洲精品大片www| 一区二区三区日韩欧美精品| 一区二区三区**美女毛片| 一区二区三区免费看视频| 亚洲风情在线资源站| 污片在线观看一区二区 | 一区二区三区四区中文字幕| 日韩伦理免费电影| 一区二区三区自拍| 亚洲乱码国产乱码精品精小说| 亚洲影视在线播放| 性做久久久久久免费观看欧美| 日韩av中文字幕一区二区| 日本午夜精品一区二区三区电影| 美女视频黄a大片欧美| 精东粉嫩av免费一区二区三区| 国产宾馆实践打屁股91| 91视视频在线观看入口直接观看www | 在线电影院国产精品| 日韩欧美久久久| 国产日韩欧美在线一区| 亚洲天堂2016| 美女脱光内衣内裤视频久久网站| 国产精品影视在线| 色综合久久综合| 欧美男男青年gay1069videost| 欧美一区二区成人| 国产欧美在线观看一区| 亚洲男女毛片无遮挡| 免费高清成人在线| 9色porny自拍视频一区二区| 欧美日韩在线三区| 久久久国际精品| 亚洲狠狠爱一区二区三区| 美女网站色91| 94-欧美-setu| 日韩精品在线一区二区| 亚洲色图在线视频| 国产综合色视频| 色老综合老女人久久久| 精品免费日韩av| 亚洲精品视频免费看| 激情偷乱视频一区二区三区| 日本精品免费观看高清观看| 久久综合色综合88| 亚洲大片精品永久免费| eeuss鲁一区二区三区| 日韩三区在线观看| 一级做a爱片久久| 成人美女视频在线看| 日韩欧美一区电影| 一区二区三区四区av| 国产成人精品影视| 欧美不卡在线视频| 亚洲成a人v欧美综合天堂| 不卡在线视频中文字幕| 久久综合狠狠综合久久激情| 日韩电影免费在线看| 在线观看区一区二| 亚洲欧洲性图库| 成人看片黄a免费看在线| 精品国产一区a| 日韩av电影免费观看高清完整版 | 精品福利视频一区二区三区| 亚洲国产裸拍裸体视频在线观看乱了 | 国产精品一区三区| 欧美一区二区三区免费视频| 亚洲成人激情社区| 欧美亚洲愉拍一区二区| 亚洲蜜臀av乱码久久精品| 国产aⅴ综合色| 日本一区二区在线不卡| 国产一区二区视频在线播放| 日韩三级.com| 久久精品国产77777蜜臀| 欧美日韩午夜在线视频| 亚洲亚洲精品在线观看| 色综合av在线| 亚洲自拍偷拍麻豆| 欧美色图一区二区三区| 一卡二卡欧美日韩| 欧美色精品在线视频| 亚洲成人黄色影院| 91精品国产综合久久久久久久| 丝袜亚洲另类欧美|