Exemplo n.º 1
0
 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), unchecked ((int)0x80000000) | unchecked ((int)0x40000000), 0, IntPtr.Zero, 3, 0, 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;
     }
 }
Exemplo n.º 2
0
        private void SetCommStateNative(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.SetCommState(this.m_Handle, ref lpDcb))
                {
                    if (num1 == 9)
                    {
                        SerialPortFixer.WinIoError();
                    }
                    num1++;
                }
                else
                {
                    return;
                }
            }
        }
Exemplo n.º 3
0
        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());
            }
        }
Exemplo n.º 4
0
 public static void Execute(string portName)
 {
     using (SerialPortFixer serialPortFixer = new SerialPortFixer(portName))
     {
     }
 }
Exemplo n.º 5
0
        private static void WinIoError()
        {
            int lastWin32Error = Marshal.GetLastWin32Error();

            throw new IOException(SerialPortFixer.GetMessage(lastWin32Error), SerialPortFixer.MakeHrFromErrorCode(lastWin32Error));
        }