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

📄 commapi.cs

📁 用C#实现的取得CellID和LAC的程序源代码!
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Data;
using System.Runtime.InteropServices;

namespace NiceTracker.Serial
{
	[StructLayout(LayoutKind.Sequential)]
	internal class CommTimeouts 
	{
		public UInt32 ReadIntervalTimeout;
		public UInt32 ReadTotalTimeoutMultiplier;
		public UInt32 ReadTotalTimeoutConstant;
		public UInt32 WriteTotalTimeoutMultiplier;
		public UInt32 WriteTotalTimeoutConstant;
	}

	#region API structs and enums
	[Flags]
	public enum CommEventFlags : uint
	{
		NONE        = 0x0000, //
		RXCHAR      = 0x0001, // Any Character received
		RXFLAG      = 0x0002, // Received specified flag character
		TXEMPTY     = 0x0004, // Tx buffer Empty
		CTS         = 0x0008, // CTS changed
		DSR         = 0x0010, // DSR changed
		RLSD        = 0x0020, // RLSD changed
		BREAK       = 0x0040, // BREAK received
		ERR         = 0x0080, // Line status error
		RING        = 0x0100, // ring detected
		PERR        = 0x0200, // printer error
		RX80FULL    = 0x0400, // rx buffer is at 80%
		EVENT1      = 0x0800, // provider event
		EVENT2      = 0x1000, // provider event
		POWER       = 0x2000, // wince power notification
		ALL			= 0x3FFF  // mask of all flags 
	}

	internal enum EventFlags
	{
		EVENT_PULSE     = 1,
		EVENT_RESET     = 2,
		EVENT_SET       = 3
	}

	[Flags]
	public enum CommErrorFlags : uint
	{
		RXOVER = 0x0001,
		OVERRUN = 0x0002,
		RXPARITY = 0x0004,
		FRAME = 0x0008,
		BREAK = 0x0010,
		TXFULL = 0x0100,
		PTO = 0x0200,
		IOE = 0x0400,
		DNS = 0x0800,
		OOP = 0x1000,
		MODE = 0x8000
	}

	[Flags]
	public enum CommModemStatusFlags : uint
	{		
		MS_CTS_ON	= 0x0010,	// The CTS (Clear To Send) signal is on. 
		MS_DSR_ON	= 0x0020,	// The DSR (Data Set Ready) signal is on. 
		MS_RING_ON	= 0x0040,	// The ring indicator signal is on. 
		MS_RLSD_ON	= 0x0080	// The RLSD (Receive Line Signal Detect) signal is on. 
	}

	public enum CommEscapes : uint
	{
		SETXOFF		= 1,
		SETXON		= 2,
		SETRTS		= 3,
		CLRRTS		= 4,
		SETDTR		= 5,
		CLRDTR		= 6,
		RESETDEV	= 7,
		SETBREAK	= 8,
		CLRBREAK	= 9
	}

	public enum APIErrors : int
	{
		ERROR_FILE_NOT_FOUND	= 2,
		ERROR_INVALID_NAME		= 123,
		ERROR_ACCESS_DENIED		= 5,
		ERROR_INVALID_HANDLE	= 6,
		ERROR_IO_PENDING		= 997
	}

	public enum APIConstants : uint
	{
		WAIT_OBJECT_0   	= 0x00000000,
		WAIT_ABANDONED  	= 0x00000080,
		WAIT_ABANDONED_0	= 0x00000080,
		WAIT_FAILED         = 0xffffffff,
		INFINITE            = 0xffffffff	
	}
	#endregion

	[StructLayout(LayoutKind.Sequential)]
	internal class CommStat 
	{
		//
		// Since the structure contains a bit-field, use a UInt32 to contain
		// the bit field and then use properties to expose the individual
		// bits as a bool.
		//
		private UInt32 bitfield;
		public UInt32 cbInQue	= 0;
		public UInt32 cbOutQue	= 0;

		// Helper constants for manipulating the bit fields.
		private readonly UInt32 fCtsHoldMask    = 0x00000001;
		private readonly Int32 fCtsHoldShift    = 0;
		private readonly UInt32 fDsrHoldMask    = 0x00000002;
		private readonly Int32 fDsrHoldShift    = 1;
		private readonly UInt32 fRlsdHoldMask   = 0x00000004;
		private readonly Int32 fRlsdHoldShift   = 2;
		private readonly UInt32 fXoffHoldMask   = 0x00000008;
		private readonly Int32 fXoffHoldShift   = 3;
		private readonly UInt32 fXoffSentMask   = 0x00000010;
		private readonly Int32 fXoffSentShift   = 4;
		private readonly UInt32 fEofMask        = 0x00000020;
		private readonly Int32 fEofShift        = 5;        
		private readonly UInt32 fTximMask       = 0x00000040;
		private readonly Int32 fTximShift       = 6;

		public bool fCtsHold 
		{
			get { return ((bitfield & fCtsHoldMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fCtsHoldShift); }
		}
		public bool fDsrHold 
		{
			get { return ((bitfield & fDsrHoldMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fDsrHoldShift); }
		}
		public bool fRlsdHold 
		{
			get { return ((bitfield & fRlsdHoldMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fRlsdHoldShift); }
		}
		public bool fXoffHold 
		{
			get { return ((bitfield & fXoffHoldMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fXoffHoldShift); }
		}
		public bool fXoffSent 
		{
			get { return ((bitfield & fXoffSentMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fXoffSentShift); }
		}
		public bool fEof 
		{
			get { return ((bitfield & fEofMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fEofShift); }
		}
		public bool fTxim 
		{
			get { return ((bitfield & fTximMask) != 0); }
			set { bitfield |= (Convert.ToUInt32(value) << fTximShift); }
		}
	}

	public class CommAPI
	{
		// These functions wrap the P/Invoked API calls and:
		// - make the correct call based on whether we're running under the full or compact framework
		// - eliminate empty parameters and defaults
		//		
		internal static IntPtr CreateFile(string FileName)
		{
			uint access = GENERIC_WRITE | GENERIC_READ;

			if(FullFramework)
			{
				return WinCreateFileW(FileName, access, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
			}
			else
			{
				return CECreateFileW(FileName, access, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
			}
		}

		internal static bool WaitCommEvent(IntPtr hPort, ref CommEventFlags flags) 
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinWaitCommEvent(hPort, ref flags, IntPtr.Zero));
			} 
			else 
			{
				return Convert.ToBoolean(CEWaitCommEvent(hPort, ref flags, IntPtr.Zero));
			}
		}

		internal static bool ClearCommError(IntPtr hPort, ref CommErrorFlags flags, CommStat stat) 
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinClearCommError(hPort, ref flags, stat));
			} 
			else 
			{
				return Convert.ToBoolean(CEClearCommError(hPort, ref flags, stat));
			}
		}

		internal static bool GetCommModemStatus(IntPtr hPort, ref uint lpModemStat)
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinGetCommModemStatus(hPort, ref lpModemStat));
			} 
			else 
			{
				return Convert.ToBoolean(CEGetCommModemStatus(hPort, ref lpModemStat));
			}
		}

		internal static bool SetCommMask(IntPtr hPort, CommEventFlags dwEvtMask) 
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinSetCommMask(hPort, dwEvtMask));
			} 
			else 
			{
				return Convert.ToBoolean(CESetCommMask(hPort, dwEvtMask));
			}
		}	

		internal static bool ReadFile(IntPtr hPort, byte[] buffer, uint cbToRead, ref Int32 cbRead) 
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinReadFile(hPort, buffer, cbToRead, ref cbRead, IntPtr.Zero));
			} 
			else 
			{
				return Convert.ToBoolean(CEReadFile(hPort, buffer, cbToRead, ref cbRead, IntPtr.Zero));
			}
		}		

		internal static bool WriteFile(IntPtr hPort, byte[] buffer, UInt32 cbToWrite, ref Int32 cbWritten) 
		{
			if (FullFramework) 
			{
				return Convert.ToBoolean(WinWriteFile(hPort, buffer, cbToWrite, ref cbWritten, IntPtr.Zero));
			} 
			else 
			{
				return Convert.ToBoolean(CEWriteFile(hPort, buffer, cbToWrite, ref cbWritten, IntPtr.Zero));
			}
		}

		internal static bool CloseHandle(IntPtr hPort) 

⌨️ 快捷键说明

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