Example #1
0
        protected ModemStatus GetModemStatus()
        {
            this.CheckOnline();
            uint num;

            if (!Win32Com.GetCommModemStatus(this.hPort, out num))
            {
                this.ThrowException("Unexpected failure");
            }
            return(new ModemStatus(num));
        }
Example #2
0
        private void ReceiveThread()
        {
            byte[]         lpBuffer     = new byte[1];
            AutoResetEvent event2       = new AutoResetEvent(false);
            OVERLAPPED     overlapped   = default(OVERLAPPED);
            IntPtr         lpOverlapped = Marshal.AllocHGlobal(Marshal.SizeOf(overlapped));

            overlapped.Offset     = 0u;
            overlapped.OffsetHigh = 0u;
            overlapped.hEvent     = event2.SafeWaitHandle.DangerousGetHandle();
            Marshal.StructureToPtr(overlapped, lpOverlapped, true);
            uint   num2      = 0u;
            IntPtr lpEvtMask = Marshal.AllocHGlobal(Marshal.SizeOf(num2));

            try
            {
                while (Win32Com.SetCommMask(this.hPort, 509u))
                {
                    Marshal.WriteInt32(lpEvtMask, 0);
                    if (!Win32Com.WaitCommEvent(this.hPort, lpEvtMask, lpOverlapped))
                    {
                        if ((long)Marshal.GetLastWin32Error() != 997L)
                        {
                            throw new CommPortException("IO Error [002]");
                        }
                        event2.WaitOne();
                    }
                    num2 = (uint)Marshal.ReadInt32(lpEvtMask);
                    if ((num2 & 128u) != 0u)
                    {
                        uint num3;
                        if (!Win32Com.ClearCommError(this.hPort, out num3, IntPtr.Zero))
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        StringBuilder builder = new StringBuilder("UART Error: ", 40);
                        if ((num3 & 8u) != 0u)
                        {
                            builder = builder.Append("Framing,");
                        }
                        if ((num3 & 1024u) != 0u)
                        {
                            builder = builder.Append("IO,");
                        }
                        if ((num3 & 2u) != 0u)
                        {
                            builder = builder.Append("Overrun,");
                        }
                        if ((num3 & 1u) != 0u)
                        {
                            builder = builder.Append("Receive Cverflow,");
                        }
                        if ((num3 & 4u) != 0u)
                        {
                            builder = builder.Append("Parity,");
                        }
                        if ((num3 & 256u) != 0u)
                        {
                            builder = builder.Append("Transmit Overflow,");
                        }
                        builder.Length--;
                        throw new CommPortException(builder.ToString());
                    }
                    else
                    {
                        if ((num2 & 1u) != 0u)
                        {
                            while (true)
                            {
                                uint num4 = 0u;
                                if (!Win32Com.ReadFile(this.hPort, lpBuffer, 1u, out num4, lpOverlapped))
                                {
                                    if ((long)Marshal.GetLastWin32Error() != 997L)
                                    {
                                        break;
                                    }
                                    Win32Com.CancelIo(this.hPort);
                                    num4 = 0u;
                                }
                                if (num4 == 1u)
                                {
                                    this.OnRxChar(lpBuffer[0]);
                                }
                                if (num4 <= 0u)
                                {
                                    goto IL_1F7;
                                }
                            }
                            throw new CommPortException("IO Error [004]");
                        }
IL_1F7:
                        if ((num2 & 4u) != 0u)
                        {
                            this.OnTxDone();
                        }
                        if ((num2 & 64u) != 0u)
                        {
                            this.OnBreak();
                        }
                        uint val = 0u;
                        if ((num2 & 8u) != 0u)
                        {
                            val |= 16u;
                        }
                        if ((num2 & 16u) != 0u)
                        {
                            val |= 32u;
                        }
                        if ((num2 & 32u) != 0u)
                        {
                            val |= 128u;
                        }
                        if ((num2 & 256u) != 0u)
                        {
                            val |= 64u;
                        }
                        uint num5;
                        if (!Win32Com.GetCommModemStatus(this.hPort, out num5))
                        {
                            throw new CommPortException("IO Error [005]");
                        }
                        this.OnStatusChange(new ModemStatus(val), new ModemStatus(num5));
                    }
                }
                throw new CommPortException("IO Error [001]");
            }
            catch (Exception exception)
            {
                if (lpEvtMask != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpEvtMask);
                }
                if (lpOverlapped != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpOverlapped);
                }
                if (!(exception is ThreadAbortException))
                {
                    this.rxException = exception;
                    this.OnRxException(exception);
                }
            }
        }