Esempio n. 1
0
 private void InternalClose()
 {
     Win32Com.CancelIo(this.hPort);
     if (this.rxThread != null)
     {
         this.rxThread.Abort();
         this.rxThread.Join(100);
         this.rxThread = null;
     }
     Win32Com.CloseHandle(this.hPort);
     if (this.ptrUWO != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(this.ptrUWO);
     }
     this.stateRTS = 2;
     this.stateDTR = 2;
     this.stateBRK = 2;
     this.online   = false;
 }
Esempio n. 2
0
        private void ReceiveThread()
        {
            byte[]         lpBuffer = new byte[1];
            bool           flag     = true;
            AutoResetEvent event2   = new AutoResetEvent(false);

            Win32Com.OVERLAPPED structure = new Win32Com.OVERLAPPED();
            uint   num2 = 0;
            IntPtr ptr  = Marshal.AllocHGlobal(Marshal.SizeOf(structure));
            IntPtr ptr2 = Marshal.AllocHGlobal(Marshal.SizeOf(num2));

            structure.Offset     = 0;
            structure.OffsetHigh = 0;
            structure.hEvent     = event2.Handle;
            Marshal.StructureToPtr(structure, ptr, true);
            try
            {
                while (true)
                {
                    if (!Win32Com.SetCommMask(this.hPort, 0x1fd))
                    {
                        throw new CommPortException("IO Error [001]");
                    }
                    Marshal.WriteInt32(ptr2, 0);
                    if (flag)
                    {
                        this.startEvent.Set();
                        flag = false;
                    }
                    if (!Win32Com.WaitCommEvent(this.hPort, ptr2, ptr))
                    {
                        if (Marshal.GetLastWin32Error() != 0x3e5L)
                        {
                            throw new CommPortException("IO Error [002]");
                        }
                        event2.WaitOne();
                    }
                    num2 = (uint)Marshal.ReadInt32(ptr2);
                    if ((num2 & 0x80) != 0)
                    {
                        uint num3;
                        if (!Win32Com.ClearCommError(this.hPort, out num3, IntPtr.Zero))
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        int           num4    = 0;
                        StringBuilder builder = new StringBuilder("UART Error: ", 40);
                        if ((num3 & 8) != 0)
                        {
                            builder = builder.Append("Framing,");
                            num4++;
                        }
                        if ((num3 & 0x400) != 0)
                        {
                            builder = builder.Append("IO,");
                            num4++;
                        }
                        if ((num3 & 2) != 0)
                        {
                            builder = builder.Append("Overrun,");
                            num4++;
                        }
                        if ((num3 & 1) != 0)
                        {
                            builder = builder.Append("Receive Cverflow,");
                            num4++;
                        }
                        if ((num3 & 4) != 0)
                        {
                            builder = builder.Append("Parity,");
                            num4++;
                        }
                        if ((num3 & 0x100) != 0)
                        {
                            builder = builder.Append("Transmit Overflow,");
                            num4++;
                        }
                        if (num4 > 0)
                        {
                            builder.Length--;
                            throw new CommPortException(builder.ToString());
                        }
                        if (num3 != 0x10)
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        num2 |= 0x40;
                    }
                    if ((num2 & 1) != 0)
                    {
                        uint num;
                        do
                        {
                            num = 0;
                            if (!Win32Com.ReadFile(this.hPort, lpBuffer, 1, out num, ptr))
                            {
                                int num5 = Marshal.GetLastWin32Error();
                                throw new CommPortException("IO Error [004]");
                            }
                            if (num == 1)
                            {
                                this.OnRxChar(lpBuffer[0]);//这个就是触发事件
                            }
                        }while (num > 0);
                    }
                    if ((num2 & 4) != 0)
                    {
                        lock (this.empty)
                        {
                            this.empty[0] = true;
                        }
                        this.OnTxDone();
                    }
                    if ((num2 & 0x40) != 0)
                    {
                        this.OnBreak();
                    }
                    uint val = 0;
                    if ((num2 & 8) != 0)
                    {
                        val |= 0x10;
                    }
                    if ((num2 & 0x10) != 0)
                    {
                        val |= 0x20;
                    }
                    if ((num2 & 0x20) != 0)
                    {
                        val |= 0x80;
                    }
                    if ((num2 & 0x100) != 0)
                    {
                        val |= 0x40;
                    }
                    if (val != 0)
                    {
                        uint num7;
                        if (!Win32Com.GetCommModemStatus(this.hPort, out num7))
                        {
                            throw new CommPortException("IO Error [005]");
                        }
                        this.OnStatusChange(new ModemStatus(val), new ModemStatus(num7));
                    }
                }
            }
            catch (Exception exception)
            {
                Win32Com.CancelIo(this.hPort);
                if (ptr2 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr2);
                }
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
                if (!(exception is ThreadAbortException))
                {
                    this.rxException = exception;
                    this.OnRxException(exception);
                }
            }
        }