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

📄 commapi.cs

📁 win CE .net Serial port class lib
💻 CS
📖 第 1 页 / 共 2 页
字号:
		internal virtual bool GetCommModemStatus(IntPtr hPort, ref uint lpModemStat){return false;}
		internal virtual bool SetCommMask(IntPtr hPort, CommEventFlags dwEvtMask) {return false;}
		internal virtual bool ReadFile(IntPtr hPort, byte[] buffer, int cbToRead, ref Int32 cbRead, IntPtr lpOverlapped) {return false;}
		internal virtual bool WriteFile(IntPtr hPort, byte[] buffer, Int32 cbToWrite, ref Int32 cbWritten, IntPtr lpOverlapped) {return false;}
		internal virtual bool CloseHandle(IntPtr hPort) {return false;}
		internal virtual bool SetupComm(IntPtr hPort, Int32 dwInQueue, Int32 dwOutQueue) {return false;}
		internal virtual bool SetCommState(IntPtr hPort, DCB dcb){return false;} 
		internal virtual bool GetCommState(IntPtr hPort, DCB dcb){return false;}
		internal virtual bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) {return false;}
		internal virtual  bool EscapeCommFunction(IntPtr hPort, CommEscapes escape){return false;}
		internal virtual  IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName) {return (IntPtr)0L;}
		internal virtual  bool SetEvent(IntPtr hEvent) {return false;}
		internal virtual  bool ResetEvent(IntPtr hEvent) {return false;}
		internal virtual  bool PulseEvent(IntPtr hEvent) {return false;}
		internal virtual  int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds) {return 0;}
		internal virtual bool GetCommProperties(IntPtr hPort, CommCapabilities commcap) {return false;} 
		#endregion

		#region Helper Property
		
		static internal bool FullFramework
		{
			get{return bFullFramework;}
		}
		#endregion

		#region API Constants
		internal const Int32 INVALID_HANDLE_VALUE = -1;
		internal const UInt32 OPEN_EXISTING = 3;
		internal const UInt32 GENERIC_READ = 0x80000000;
		internal const UInt32 GENERIC_WRITE = 0x40000000;
		internal const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000;
		internal const UInt32 CreateAccess = GENERIC_WRITE | GENERIC_READ;
		#endregion	
		
	}
	#endregion
	
	#region CE CompactFramework (cf) implementation for CommAPI
	internal class CECommAPI : CommAPI
	{
		override internal  IntPtr CreateFile(string FileName)
		{		
			return CECreateFileW(FileName, CreateAccess, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
		}

		// setting AccessMask to 0 returns a handle we can use to query the device without accessing it
		override internal  IntPtr QueryFile(string FileName)
		{		
			return CECreateFileW(FileName, 0, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
		}

		override internal  bool WaitCommEvent(IntPtr hPort, ref CommEventFlags flags) 
		{
			return Convert.ToBoolean(CEWaitCommEvent(hPort, ref flags, IntPtr.Zero));
		}

		override internal  bool ClearCommError(IntPtr hPort, ref CommErrorFlags flags, CommStat stat) 
		{
			return Convert.ToBoolean(CEClearCommError(hPort, ref flags, stat));
		}

		override internal  bool GetCommModemStatus(IntPtr hPort, ref uint lpModemStat)
		{			
			return Convert.ToBoolean(CEGetCommModemStatus(hPort, ref lpModemStat));
		}

		override internal  bool SetCommMask(IntPtr hPort, CommEventFlags dwEvtMask) 
		{
			return Convert.ToBoolean(CESetCommMask(hPort, dwEvtMask));
		}	

		override internal  bool ReadFile(IntPtr hPort, byte[] buffer, int cbToRead, ref Int32 cbRead, IntPtr lpOverlapped) 
		{
			return Convert.ToBoolean(CEReadFile(hPort, buffer, cbToRead, ref cbRead, IntPtr.Zero));
		}		

		override internal  bool WriteFile(IntPtr hPort, byte[] buffer, Int32 cbToWrite, ref Int32 cbWritten, IntPtr lpOverlapped) 
		{
			return Convert.ToBoolean(CEWriteFile(hPort, buffer, cbToWrite, ref cbWritten, IntPtr.Zero));
		}

		override internal  bool CloseHandle(IntPtr hPort) 
		{
			return Convert.ToBoolean(CECloseHandle(hPort));
		}

		override internal  bool SetupComm(IntPtr hPort, Int32 dwInQueue, Int32 dwOutQueue)
		{
			return Convert.ToBoolean(CESetupComm(hPort, dwInQueue, dwOutQueue));
		}

		override internal  bool SetCommState(IntPtr hPort, DCB dcb) 
		{
			return Convert.ToBoolean(CESetCommState(hPort, dcb));
		}

		override internal  bool GetCommState(IntPtr hPort, DCB dcb) 
		{
			return Convert.ToBoolean(CEGetCommState(hPort, dcb));
		}

		override internal  bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) 
		{ 
			return Convert.ToBoolean(CESetCommTimeouts(hPort, timeouts));
		}
		
		override internal  bool EscapeCommFunction(IntPtr hPort, CommEscapes escape)
		{
			return Convert.ToBoolean(CEEscapeCommFunction(hPort, (uint)escape));
		}

		override internal  IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName)
		{
			return CECreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
		}

		override internal  bool SetEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_SET));
		}

		override internal  bool ResetEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_RESET));
		}

		override internal bool PulseEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(CEEventModify(hEvent, (uint)EventFlags.EVENT_PULSE));
		}

		override internal int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds)
		{
			return CEWaitForSingleObject(hHandle, dwMilliseconds);
		}

		override internal  bool GetCommProperties(IntPtr hPort, CommCapabilities commcap) 
		{
			return Convert.ToBoolean(CEGetCommProperties(hPort, commcap));
		}
		#region Windows CE API imports

		[DllImport("coredll.dll", EntryPoint="WaitForSingleObject", SetLastError = true)]
		private static extern int CEWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds); 

		[DllImport("coredll.dll", EntryPoint="EventModify", SetLastError = true)]
		private static extern int CEEventModify(IntPtr hEvent, uint function); 

		[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
		private static extern IntPtr CECreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName); 

		[DllImport("coredll.dll", EntryPoint="EscapeCommFunction", SetLastError = true)]
		private static extern int CEEscapeCommFunction(IntPtr hFile, UInt32 dwFunc);

		[DllImport("coredll.dll", EntryPoint="SetCommTimeouts", SetLastError = true)]
		private static extern int CESetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);

		[DllImport("coredll.dll", EntryPoint="GetCommState", SetLastError = true)]
		private static extern int CEGetCommState(IntPtr hFile, DCB dcb);

		[DllImport("coredll.dll", EntryPoint="SetCommState", SetLastError = true)]
		private static extern int CESetCommState(IntPtr hFile, DCB dcb);

		[DllImport("coredll.dll", EntryPoint="SetupComm", SetLastError = true)]
		private static extern int CESetupComm(IntPtr hFile, Int32 dwInQueue, Int32 dwOutQueue);

		[DllImport("coredll.dll", EntryPoint="CloseHandle", SetLastError = true)]
		private static extern int CECloseHandle(IntPtr hObject);

		[DllImport("coredll.dll", EntryPoint="WriteFile", SetLastError = true)]
		private static extern int CEWriteFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);

		[DllImport("coredll.dll", EntryPoint="ReadFile", SetLastError = true)]
		private static extern int CEReadFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);

		[DllImport("coredll.dll", EntryPoint="SetCommMask", SetLastError = true)]
		private static extern int CESetCommMask(IntPtr handle, CommEventFlags dwEvtMask);

		[DllImport("coredll.dll", EntryPoint="GetCommModemStatus", SetLastError = true)]
		private static extern int CEGetCommModemStatus(IntPtr hFile, ref uint lpModemStat);

		[DllImport("coredll.dll", EntryPoint="ClearCommError", SetLastError = true)]
		private static extern int CEClearCommError(IntPtr hFile, ref CommErrorFlags lpErrors, CommStat lpStat);

		[DllImport("coredll.dll", EntryPoint="WaitCommEvent", SetLastError = true)]
		private static extern int CEWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);
		
		[DllImport("coredll.dll", EntryPoint="CreateFileW", SetLastError = true)]
		private static extern IntPtr CECreateFileW(
			String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
			IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
			IntPtr hTemplateFile);

		[DllImport("coredll.dll", EntryPoint="GetCommProperties", SetLastError=true)]
		private static extern int CEGetCommProperties(IntPtr hFile, CommCapabilities commcap);
		#endregion

	}
	# endregion

	#region Full Framework (aka Win) implementation for CommAPI
	internal class WinCommAPI:CommAPI
	{
		override internal IntPtr CreateFile(string FileName)
		{
			return WinCreateFileW(FileName, CreateAccess, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
		}

		// setting AccessMask to 0 returns a handle we can use to query the device without accessing it
		override internal IntPtr QueryFile(string FileName)
		{
			return WinCreateFileW(FileName, 0, 0, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero);
		}

		override internal bool WaitCommEvent(IntPtr hPort, ref CommEventFlags flags) 
		{
			return Convert.ToBoolean(WinWaitCommEvent(hPort, ref flags, IntPtr.Zero));
		}

		override internal bool ClearCommError(IntPtr hPort, ref CommErrorFlags flags, CommStat stat) 
		{
			return Convert.ToBoolean(WinClearCommError(hPort, ref flags, stat));
		}

		override internal bool GetCommModemStatus(IntPtr hPort, ref uint lpModemStat)
		{
			return Convert.ToBoolean(WinGetCommModemStatus(hPort, ref lpModemStat));
		}

		override internal bool SetCommMask(IntPtr hPort, CommEventFlags dwEvtMask) 
		{
			return Convert.ToBoolean(WinSetCommMask(hPort, dwEvtMask));
		}	

		override internal bool ReadFile(IntPtr hPort, byte[] buffer, int cbToRead, ref Int32 cbRead, IntPtr lpOverlapped) 
		{
			return Convert.ToBoolean(WinReadFile(hPort, buffer, cbToRead, ref cbRead, lpOverlapped));
		}		

		override internal bool WriteFile(IntPtr hPort, byte[] buffer, Int32 cbToWrite, ref Int32 cbWritten, IntPtr lpOverlapped) 
		{
			return Convert.ToBoolean(WinWriteFile(hPort, buffer, cbToWrite, ref cbWritten, lpOverlapped));
		}

		override internal bool CloseHandle(IntPtr hPort) 
		{
			return Convert.ToBoolean(WinCloseHandle(hPort));
		}

		override internal bool SetupComm(IntPtr hPort, Int32 dwInQueue, Int32 dwOutQueue)
		{
			return Convert.ToBoolean(WinSetupComm(hPort, dwInQueue, dwOutQueue));
		}

		override internal bool SetCommState(IntPtr hPort, DCB dcb) 
		{
			return Convert.ToBoolean(WinSetCommState(hPort, dcb));
		}

		override internal bool GetCommState(IntPtr hPort, DCB dcb) 
		{
			return Convert.ToBoolean(WinGetCommState(hPort, dcb));
		}

		override internal bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) 
		{
			return Convert.ToBoolean(WinSetCommTimeouts(hPort, timeouts));
		}
		
		override internal bool EscapeCommFunction(IntPtr hPort, CommEscapes escape)
		{
			return Convert.ToBoolean(WinEscapeCommFunction(hPort, (uint)escape));
		}

		override internal IntPtr CreateEvent(bool bManualReset, bool bInitialState, string lpName)
		{
			return WinCreateEvent(IntPtr.Zero, Convert.ToInt32(bManualReset), Convert.ToInt32(bInitialState), lpName);
		}

		override internal bool SetEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(WinSetEvent(hEvent));
		}

		override internal bool ResetEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(WinResetEvent(hEvent));
		}

		override internal bool PulseEvent(IntPtr hEvent)
		{
			return Convert.ToBoolean(WinPulseEvent(hEvent));
		}

		override internal int WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds)
		{
			return WinWaitForSingleObject(hHandle, dwMilliseconds);
		}

		override internal  bool GetCommProperties(IntPtr hPort, CommCapabilities commcap) 
		{
			return Convert.ToBoolean(WinGetCommProperties(hPort, commcap));
		}

		#region Desktop Windows API imports

		[DllImport("kernel32.dll", EntryPoint="WaitForSingleObject", SetLastError = true)]
		private static extern int WinWaitForSingleObject(IntPtr hHandle, uint dwMilliseconds); 

		[DllImport("kernel32.dll", EntryPoint="SetEvent", SetLastError = true)]
		private static extern int WinSetEvent(IntPtr hEvent); 

		[DllImport("kernel32.dll", EntryPoint="ResetEvent", SetLastError = true)]
		private static extern int WinResetEvent(IntPtr hEvent); 

		[DllImport("kernel32.dll", EntryPoint="PulseEvent", SetLastError = true)]
		private static extern int WinPulseEvent(IntPtr hEvent); 

		[DllImport("kernel32.dll", EntryPoint="CreateEvent", SetLastError = true)]
		private static extern IntPtr WinCreateEvent(IntPtr lpEventAttributes, int bManualReset, int bInitialState, string lpName); 

		[DllImport("kernel32.dll", EntryPoint="EscapeCommFunction", SetLastError = true)]
		private static extern int WinEscapeCommFunction(IntPtr hFile, UInt32 dwFunc);

		[DllImport("kernel32.dll", EntryPoint="SetCommTimeouts", SetLastError = true)]
		private static extern int WinSetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);

		[DllImport("kernel32.dll", EntryPoint="GetCommState", SetLastError = true)]
		private static extern int WinGetCommState(IntPtr hFile, DCB dcb);

		[DllImport("kernel32.dll", EntryPoint="SetCommState", SetLastError = true)]
		private static extern int WinSetCommState(IntPtr hFile, DCB dcb);

		[DllImport("kernel32.dll", EntryPoint="SetupComm", SetLastError = true)]
		private static extern int WinSetupComm(IntPtr hFile, Int32 dwInQueue, Int32 dwOutQueue);

		[DllImport("kernel32.dll", EntryPoint="CloseHandle", SetLastError = true)]
		private static extern int WinCloseHandle(IntPtr hObject);

		[DllImport("kernel32.dll", EntryPoint="WriteFile", SetLastError = true)]
		private  static extern int WinWriteFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);

		[DllImport("kernel32.dll", EntryPoint="ReadFile", SetLastError = true)]
		private static extern int WinReadFile(IntPtr hFile, byte[] lpBuffer, Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, IntPtr lpOverlapped);

		[DllImport("kernel32.dll", EntryPoint="SetCommMask", SetLastError = true)]
		private static extern int WinSetCommMask(IntPtr handle, CommEventFlags dwEvtMask);

		[DllImport("kernel32.dll", EntryPoint="GetCommModemStatus", SetLastError = true)]
		private  static extern int WinGetCommModemStatus(IntPtr hFile, ref uint lpModemStat);

		[DllImport("kernel32.dll", EntryPoint="ClearCommError", SetLastError = true)]
		private  static extern int WinClearCommError(IntPtr hFile, ref CommErrorFlags lpErrors, CommStat lpStat);

		[DllImport("kernel32.dll", EntryPoint="CreateFileW", SetLastError = true, CharSet = CharSet.Unicode)]
		private static extern IntPtr WinCreateFileW(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
			IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
			IntPtr hTemplateFile);

		[DllImport("kernel32.dll", EntryPoint="WaitCommEvent", SetLastError = true)]
		private static extern int WinWaitCommEvent(IntPtr hFile, ref CommEventFlags lpEvtMask, IntPtr lpOverlapped);

		[DllImport("kernel32.dll", EntryPoint="GetCommProperties", SetLastError=true)]
		private static extern int WinGetCommProperties(IntPtr hFile, CommCapabilities commcap);

		#endregion
	}
	#endregion
}


⌨️ 快捷键说明

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