Beispiel #1
0
        private void ReceiveThread()
        {
            byte[]         lpBuffer       = new byte[1];
            bool           flag           = true;
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);

            Win32Com.OVERLAPPED overlapped = new Win32Com.OVERLAPPED();
            uint   num1 = 0U;
            IntPtr num2 = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
            IntPtr num3 = Marshal.AllocHGlobal(Marshal.SizeOf((object)num1));

            overlapped.Offset     = 0U;
            overlapped.OffsetHigh = 0U;
            overlapped.hEvent     = autoResetEvent.Handle;
            Marshal.StructureToPtr((object)overlapped, num2, true);
            try
            {
                while (Win32Com.SetCommMask(hPort, 509U))
                {
                    Marshal.WriteInt32(num3, 0);
                    if (flag)
                    {
                        startEvent.Set();
                        flag = false;
                    }
                    if (!Win32Com.WaitCommEvent(hPort, num3, num2))
                    {
                        if ((long)Marshal.GetLastWin32Error() != 997L)
                        {
                            throw new CommPortException("IO Error [002]");
                        }
                        autoResetEvent.WaitOne();
                    }
                    uint num4 = (uint)Marshal.ReadInt32(num3);
                    if (((int)num4 & 128) != 0)
                    {
                        uint lpErrors;
                        if (!Win32Com.ClearCommError(hPort, out lpErrors, IntPtr.Zero))
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        int           num5          = 0;
                        StringBuilder stringBuilder = new StringBuilder("UART Error: ", 40);
                        if (((int)lpErrors & 8) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Framing,");
                            ++num5;
                        }
                        if (((int)lpErrors & 1024) != 0)
                        {
                            stringBuilder = stringBuilder.Append("IO,");
                            ++num5;
                        }
                        if (((int)lpErrors & 2) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Overrun,");
                            ++num5;
                        }
                        if (((int)lpErrors & 1) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Receive Cverflow,");
                            ++num5;
                        }
                        if (((int)lpErrors & 4) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Parity,");
                            ++num5;
                        }
                        if (((int)lpErrors & 256) != 0)
                        {
                            stringBuilder = stringBuilder.Append("Transmit Overflow,");
                            ++num5;
                        }
                        if (num5 > 0)
                        {
                            stringBuilder.Length = stringBuilder.Length - 1;
                            throw new CommPortException(stringBuilder.ToString());
                        }
                        else
                        {
                            if ((int)lpErrors != 16)
                            {
                                throw new CommPortException("IO Error [003]");
                            }
                            num4 |= 64U;
                        }
                    }
                    if (((int)num4 & 1) != 0)
                    {
                        uint nNumberOfBytesRead;
                        do
                        {
                            nNumberOfBytesRead = 0U;
                            if (!Win32Com.ReadFile(hPort, lpBuffer, 1U, out nNumberOfBytesRead, num2))
                            {
                                Marshal.GetLastWin32Error();
                                throw new CommPortException("IO Error [004]");
                            }
                            else if ((int)nNumberOfBytesRead == 1)
                            {
                                OnRxChar(lpBuffer[0]);
                            }
                        }while (nNumberOfBytesRead > 0U);
                    }
                    if (((int)num4 & 4) != 0)
                    {
                        lock (empty)
                            empty[0] = true;
                        OnTxDone();
                    }
                    if (((int)num4 & 64) != 0)
                    {
                        OnBreak();
                    }
                    uint val = 0U;
                    if (((int)num4 & 8) != 0)
                    {
                        val |= 16U;
                    }
                    if (((int)num4 & 16) != 0)
                    {
                        val |= 32U;
                    }
                    if (((int)num4 & 32) != 0)
                    {
                        val |= 128U;
                    }
                    if (((int)num4 & 256) != 0)
                    {
                        val |= 64U;
                    }
                    if ((int)val != 0)
                    {
                        uint lpModemStat;
                        if (!Win32Com.GetCommModemStatus(hPort, out lpModemStat))
                        {
                            throw new CommPortException("IO Error [005]");
                        }
                        OnStatusChange(new CommBase.ModemStatus(val), new CommBase.ModemStatus(lpModemStat));
                    }
                }
                throw new CommPortException("IO Error [001]");
            }
            catch (Exception ex)
            {
                Win32Com.CancelIo(hPort);
                if (num3 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(num3);
                }
                if (num2 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(num2);
                }
                if (ex is ThreadAbortException)
                {
                    return;
                }
                rxException = ex;
                OnRxException(ex);
            }
        }
Beispiel #2
0
 private void ReceiveThread()
 {
     byte[] lpBuffer = new byte[1];
     bool flag = true;
     AutoResetEvent autoResetEvent = new AutoResetEvent(false);
     Win32Com.OVERLAPPED overlapped = new Win32Com.OVERLAPPED();
     uint num1 = 0U;
     IntPtr num2 = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
     IntPtr num3 = Marshal.AllocHGlobal(Marshal.SizeOf((object)num1));
     overlapped.Offset = 0U;
     overlapped.OffsetHigh = 0U;
     overlapped.hEvent = autoResetEvent.Handle;
     Marshal.StructureToPtr((object)overlapped, num2, true);
     try
     {
         while (Win32Com.SetCommMask(hPort, 509U))
         {
             Marshal.WriteInt32(num3, 0);
             if (flag)
             {
                 startEvent.Set();
                 flag = false;
             }
             if (!Win32Com.WaitCommEvent(hPort, num3, num2))
             {
                 if ((long)Marshal.GetLastWin32Error() != 997L)
                     throw new CommPortException("IO Error [002]");
                 autoResetEvent.WaitOne();
             }
             uint num4 = (uint)Marshal.ReadInt32(num3);
             if (((int)num4 & 128) != 0)
             {
                 uint lpErrors;
                 if (!Win32Com.ClearCommError(hPort, out lpErrors, IntPtr.Zero))
                     throw new CommPortException("IO Error [003]");
                 int num5 = 0;
                 StringBuilder stringBuilder = new StringBuilder("UART Error: ", 40);
                 if (((int)lpErrors & 8) != 0)
                 {
                     stringBuilder = stringBuilder.Append("Framing,");
                     ++num5;
                 }
                 if (((int)lpErrors & 1024) != 0)
                 {
                     stringBuilder = stringBuilder.Append("IO,");
                     ++num5;
                 }
                 if (((int)lpErrors & 2) != 0)
                 {
                     stringBuilder = stringBuilder.Append("Overrun,");
                     ++num5;
                 }
                 if (((int)lpErrors & 1) != 0)
                 {
                     stringBuilder = stringBuilder.Append("Receive Cverflow,");
                     ++num5;
                 }
                 if (((int)lpErrors & 4) != 0)
                 {
                     stringBuilder = stringBuilder.Append("Parity,");
                     ++num5;
                 }
                 if (((int)lpErrors & 256) != 0)
                 {
                     stringBuilder = stringBuilder.Append("Transmit Overflow,");
                     ++num5;
                 }
                 if (num5 > 0)
                 {
                     stringBuilder.Length = stringBuilder.Length - 1;
                     throw new CommPortException(stringBuilder.ToString());
                 }
                 else
                 {
                     if ((int)lpErrors != 16)
                         throw new CommPortException("IO Error [003]");
                     num4 |= 64U;
                 }
             }
             if (((int)num4 & 1) != 0)
             {
                 uint nNumberOfBytesRead;
                 do
                 {
                     nNumberOfBytesRead = 0U;
                     if (!Win32Com.ReadFile(hPort, lpBuffer, 1U, out nNumberOfBytesRead, num2))
                     {
                         Marshal.GetLastWin32Error();
                         throw new CommPortException("IO Error [004]");
                     }
                     else if ((int)nNumberOfBytesRead == 1)
                         OnRxChar(lpBuffer[0]);
                 }
                 while (nNumberOfBytesRead > 0U);
             }
             if (((int)num4 & 4) != 0)
             {
                 lock (empty)
                     empty[0] = true;
                 OnTxDone();
             }
             if (((int)num4 & 64) != 0)
                 OnBreak();
             uint val = 0U;
             if (((int)num4 & 8) != 0)
                 val |= 16U;
             if (((int)num4 & 16) != 0)
                 val |= 32U;
             if (((int)num4 & 32) != 0)
                 val |= 128U;
             if (((int)num4 & 256) != 0)
                 val |= 64U;
             if ((int)val != 0)
             {
                 uint lpModemStat;
                 if (!Win32Com.GetCommModemStatus(hPort, out lpModemStat))
                     throw new CommPortException("IO Error [005]");
                 OnStatusChange(new CommBase.ModemStatus(val), new CommBase.ModemStatus(lpModemStat));
             }
         }
         throw new CommPortException("IO Error [001]");
     }
     catch (Exception ex)
     {
         Win32Com.CancelIo(hPort);
         if (num3 != IntPtr.Zero)
             Marshal.FreeHGlobal(num3);
         if (num2 != IntPtr.Zero)
             Marshal.FreeHGlobal(num2);
         if (ex is ThreadAbortException)
             return;
         rxException = ex;
         OnRxException(ex);
     }
 }
Beispiel #3
0
 public bool Open()
 {
     Win32Com.DCB          lpDCB          = new Win32Com.DCB();
     Win32Com.COMMTIMEOUTS lpCommTimeouts = new Win32Com.COMMTIMEOUTS();
     Win32Com.OVERLAPPED   overlapped     = new Win32Com.OVERLAPPED();
     if (online)
     {
         return(false);
     }
     CommBase.CommBaseSettings commBaseSettings = CommSettings();
     hPort = Win32Com.CreateFile(commBaseSettings.port, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
     if (hPort == (IntPtr)(-1))
     {
         if ((long)Marshal.GetLastWin32Error() == 5L)
         {
             return(false);
         }
         hPort = Win32Com.CreateFile(AltName(commBaseSettings.port), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
         if (hPort == (IntPtr)(-1))
         {
             if ((long)Marshal.GetLastWin32Error() == 5L)
             {
                 return(false);
             }
             else
             {
                 throw new CommPortException("Port Open Failure");
             }
         }
     }
     online = true;
     lpCommTimeouts.ReadIntervalTimeout         = uint.MaxValue;
     lpCommTimeouts.ReadTotalTimeoutConstant    = 0U;
     lpCommTimeouts.ReadTotalTimeoutMultiplier  = 0U;
     lpCommTimeouts.WriteTotalTimeoutMultiplier = (int)commBaseSettings.sendTimeoutMultiplier != 0 ? commBaseSettings.sendTimeoutMultiplier : (Environment.OSVersion.Platform != PlatformID.Win32NT ? 10000U : 0U);
     lpCommTimeouts.WriteTotalTimeoutConstant   = commBaseSettings.sendTimeoutConstant;
     lpDCB.init(commBaseSettings.parity == CommBase.Parity.odd || commBaseSettings.parity == CommBase.Parity.even, commBaseSettings.txFlowCTS, commBaseSettings.txFlowDSR, (int)commBaseSettings.useDTR, commBaseSettings.rxGateDSR, !commBaseSettings.txWhenRxXoff, commBaseSettings.txFlowX, commBaseSettings.rxFlowX, (int)commBaseSettings.useRTS);
     lpDCB.BaudRate = commBaseSettings.baudRate;
     lpDCB.ByteSize = (byte)commBaseSettings.dataBits;
     lpDCB.Parity   = (byte)commBaseSettings.parity;
     lpDCB.StopBits = (byte)commBaseSettings.stopBits;
     lpDCB.XoffChar = (byte)commBaseSettings.XoffChar;
     lpDCB.XonChar  = (byte)commBaseSettings.XonChar;
     if ((commBaseSettings.rxQueue != 0 || commBaseSettings.txQueue != 0) && !Win32Com.SetupComm(hPort, (uint)commBaseSettings.rxQueue, (uint)commBaseSettings.txQueue))
     {
         ThrowException("Bad queue settings");
     }
     if (commBaseSettings.rxLowWater == 0 || commBaseSettings.rxHighWater == 0)
     {
         Win32Com.COMMPROP cp;
         if (!Win32Com.GetCommProperties(hPort, out cp))
         {
             cp.dwCurrentRxQueue = 0U;
         }
         lpDCB.XoffLim = cp.dwCurrentRxQueue <= 0U ? (lpDCB.XonLim = (short)8) : (lpDCB.XonLim = (short)((int)cp.dwCurrentRxQueue / 10));
     }
     else
     {
         lpDCB.XoffLim = (short)commBaseSettings.rxHighWater;
         lpDCB.XonLim  = (short)commBaseSettings.rxLowWater;
     }
     if (!Win32Com.SetCommState(hPort, ref lpDCB))
     {
         ThrowException("Bad com settings");
     }
     if (!Win32Com.SetCommTimeouts(hPort, ref lpCommTimeouts))
     {
         ThrowException("Bad timeout settings");
     }
     stateBRK = 0;
     if (commBaseSettings.useDTR == CommBase.HSOutput.none)
     {
         stateDTR = 0;
     }
     if (commBaseSettings.useDTR == CommBase.HSOutput.online)
     {
         stateDTR = 1;
     }
     if (commBaseSettings.useRTS == CommBase.HSOutput.none)
     {
         stateRTS = 0;
     }
     if (commBaseSettings.useRTS == CommBase.HSOutput.online)
     {
         stateRTS = 1;
     }
     checkSends            = commBaseSettings.checkAllSends;
     overlapped.Offset     = 0U;
     overlapped.OffsetHigh = 0U;
     overlapped.hEvent     = !checkSends ? IntPtr.Zero : writeEvent.Handle;
     ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
     Marshal.StructureToPtr((object)overlapped, ptrUWO, true);
     writeCount          = 0;
     empty[0]            = true;
     dataQueued          = false;
     rxException         = (Exception)null;
     rxExceptionReported = false;
     rxThread            = new Thread(new ThreadStart(ReceiveThread));
     rxThread.Name       = "CommBaseRx";
     rxThread.Priority   = ThreadPriority.AboveNormal;
     rxThread.Start();
     startEvent.WaitOne(500, false);
     auto = false;
     if (AfterOpen())
     {
         auto = commBaseSettings.autoReopen;
         return(true);
     }
     else
     {
         Close();
         return(false);
     }
 }
Beispiel #4
0
 public bool Open()
 {
     Win32Com.DCB lpDCB = new Win32Com.DCB();
     Win32Com.COMMTIMEOUTS lpCommTimeouts = new Win32Com.COMMTIMEOUTS();
     Win32Com.OVERLAPPED overlapped = new Win32Com.OVERLAPPED();
     if (online)
         return false;
     CommBase.CommBaseSettings commBaseSettings = CommSettings();
     hPort = Win32Com.CreateFile(commBaseSettings.port, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
     if (hPort == (IntPtr)(-1))
     {
         if ((long)Marshal.GetLastWin32Error() == 5L)
             return false;
         hPort = Win32Com.CreateFile(AltName(commBaseSettings.port), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
         if (hPort == (IntPtr)(-1))
         {
             if ((long)Marshal.GetLastWin32Error() == 5L)
                 return false;
             else
                 throw new CommPortException("Port Open Failure");
         }
     }
     online = true;
     lpCommTimeouts.ReadIntervalTimeout = uint.MaxValue;
     lpCommTimeouts.ReadTotalTimeoutConstant = 0U;
     lpCommTimeouts.ReadTotalTimeoutMultiplier = 0U;
     lpCommTimeouts.WriteTotalTimeoutMultiplier = (int)commBaseSettings.sendTimeoutMultiplier != 0 ? commBaseSettings.sendTimeoutMultiplier : (Environment.OSVersion.Platform != PlatformID.Win32NT ? 10000U : 0U);
     lpCommTimeouts.WriteTotalTimeoutConstant = commBaseSettings.sendTimeoutConstant;
     lpDCB.init(commBaseSettings.parity == CommBase.Parity.odd || commBaseSettings.parity == CommBase.Parity.even, commBaseSettings.txFlowCTS, commBaseSettings.txFlowDSR, (int)commBaseSettings.useDTR, commBaseSettings.rxGateDSR, !commBaseSettings.txWhenRxXoff, commBaseSettings.txFlowX, commBaseSettings.rxFlowX, (int)commBaseSettings.useRTS);
     lpDCB.BaudRate = commBaseSettings.baudRate;
     lpDCB.ByteSize = (byte)commBaseSettings.dataBits;
     lpDCB.Parity = (byte)commBaseSettings.parity;
     lpDCB.StopBits = (byte)commBaseSettings.stopBits;
     lpDCB.XoffChar = (byte)commBaseSettings.XoffChar;
     lpDCB.XonChar = (byte)commBaseSettings.XonChar;
     if ((commBaseSettings.rxQueue != 0 || commBaseSettings.txQueue != 0) && !Win32Com.SetupComm(hPort, (uint)commBaseSettings.rxQueue, (uint)commBaseSettings.txQueue))
         ThrowException("Bad queue settings");
     if (commBaseSettings.rxLowWater == 0 || commBaseSettings.rxHighWater == 0)
     {
         Win32Com.COMMPROP cp;
         if (!Win32Com.GetCommProperties(hPort, out cp))
             cp.dwCurrentRxQueue = 0U;
         lpDCB.XoffLim = cp.dwCurrentRxQueue <= 0U ? (lpDCB.XonLim = (short)8) : (lpDCB.XonLim = (short)((int)cp.dwCurrentRxQueue / 10));
     }
     else
     {
         lpDCB.XoffLim = (short)commBaseSettings.rxHighWater;
         lpDCB.XonLim = (short)commBaseSettings.rxLowWater;
     }
     if (!Win32Com.SetCommState(hPort, ref lpDCB))
         ThrowException("Bad com settings");
     if (!Win32Com.SetCommTimeouts(hPort, ref lpCommTimeouts))
         ThrowException("Bad timeout settings");
     stateBRK = 0;
     if (commBaseSettings.useDTR == CommBase.HSOutput.none)
         stateDTR = 0;
     if (commBaseSettings.useDTR == CommBase.HSOutput.online)
         stateDTR = 1;
     if (commBaseSettings.useRTS == CommBase.HSOutput.none)
         stateRTS = 0;
     if (commBaseSettings.useRTS == CommBase.HSOutput.online)
         stateRTS = 1;
     checkSends = commBaseSettings.checkAllSends;
     overlapped.Offset = 0U;
     overlapped.OffsetHigh = 0U;
     overlapped.hEvent = !checkSends ? IntPtr.Zero : writeEvent.Handle;
     ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
     Marshal.StructureToPtr((object)overlapped, ptrUWO, true);
     writeCount = 0;
     empty[0] = true;
     dataQueued = false;
     rxException = (Exception)null;
     rxExceptionReported = false;
     rxThread = new Thread(new ThreadStart(ReceiveThread));
     rxThread.Name = "CommBaseRx";
     rxThread.Priority = ThreadPriority.AboveNormal;
     rxThread.Start();
     startEvent.WaitOne(500, false);
     auto = false;
     if (AfterOpen())
     {
         auto = commBaseSettings.autoReopen;
         return true;
     }
     else
     {
         Close();
         return false;
     }
 }