Example #1
0
        protected CommBase.ModemStatus GetModemStatus()
        {
            CheckOnline();
            uint lpModemStat;

            if (!Win32Com.GetCommModemStatus(hPort, out lpModemStat))
            {
                ThrowException("Unexpected failure");
            }
            return(new CommBase.ModemStatus(lpModemStat));
        }
Example #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);
            }
        }