private void GetCommStateNative(ref SerialPortFixer.Dcb lpDcb) { int num = 0; SerialPortFixer.Comstat comstat = new SerialPortFixer.Comstat(); int num1 = 0; while (num1 < 10) { if (!SerialPortFixer.ClearCommError(this.m_Handle, ref num, ref comstat)) { SerialPortFixer.WinIoError(); } if (!SerialPortFixer.GetCommState(this.m_Handle, ref lpDcb)) { if (num1 == 9) { SerialPortFixer.WinIoError(); } num1++; } else { return; } } }
private SerialPortFixer(string portName) { if (portName == null || !portName.StartsWith("COM", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("Invalid Serial Port", "portName"); } else { SafeFileHandle safeFileHandle = SerialPortFixer.CreateFile(string.Concat("\\\\.\\", portName), -1073741824, 0, IntPtr.Zero, 3, 1073741824, IntPtr.Zero); if (safeFileHandle.IsInvalid) { SerialPortFixer.WinIoError(); } try { int fileType = SerialPortFixer.GetFileType(safeFileHandle); if (fileType == 2 || fileType == 0) { this.m_Handle = safeFileHandle; this.InitializeDcb(); } else { throw new ArgumentException("Invalid Serial Port", "portName"); } } catch { safeFileHandle.Close(); this.m_Handle = null; throw; } return; } }
private static string GetMessage(int errorCode) { StringBuilder stringBuilder = new StringBuilder(512); if (SerialPortFixer.FormatMessage(12800, new HandleRef(null, IntPtr.Zero), errorCode, 0, stringBuilder, stringBuilder.Capacity, IntPtr.Zero) == 0) { return("Unknown Error"); } else { return(stringBuilder.ToString()); } }
private static extern bool GetCommState(SafeFileHandle hFile, ref SerialPortFixer.Dcb lpDcb);
public static void Execute(string portName) { using (SerialPortFixer serialPortFixer = new SerialPortFixer(portName)) { } }
private static extern bool ClearCommError(SafeFileHandle hFile, ref int lpErrors, ref SerialPortFixer.Comstat lpStat);
private static void WinIoError() { int lastWin32Error = Marshal.GetLastWin32Error(); throw new IOException(SerialPortFixer.GetMessage(lastWin32Error), SerialPortFixer.MakeHrFromErrorCode(lastWin32Error)); }