Ejemplo n.º 1
0
        private void ReceiveThread()
        {
            uint num;
            uint num1 = 0;
            uint num2 = 0;

            byte[]         numArray       = new byte[1];
            bool           flag           = true;
            AutoResetEvent autoResetEvent = new AutoResetEvent(false);

            Win32Com.OVERLAPPED handle = new Win32Com.OVERLAPPED();
            uint   num3    = 0;
            IntPtr intPtr  = Marshal.AllocHGlobal(Marshal.SizeOf(handle));
            IntPtr intPtr1 = Marshal.AllocHGlobal(Marshal.SizeOf(num3));

            handle.Offset     = 0;
            handle.OffsetHigh = 0;
            handle.hEvent     = autoResetEvent.Handle;
            Marshal.StructureToPtr(handle, intPtr, true);
            try
            {
                while (Win32Com.SetCommMask(this.hPort, 0x1fd))
                {
                    Marshal.WriteInt32(intPtr1, 0);
                    if (flag)
                    {
                        this.startEvent.Set();
                        flag = false;
                    }
                    if (!Win32Com.WaitCommEvent(this.hPort, intPtr1, intPtr))
                    {
                        if ((long)Marshal.GetLastWin32Error() != (long)0x3e5)
                        {
                            throw new CommPortException("IO Error [002]");
                        }
                        else
                        {
                            autoResetEvent.WaitOne();
                        }
                    }
                    num3 = (uint)Marshal.ReadInt32(intPtr1);
                    if ((num3 & 128) != 0)
                    {
                        if (!Win32Com.ClearCommError(this.hPort, out num1, IntPtr.Zero))
                        {
                            throw new CommPortException("IO Error [003]");
                        }
                        else
                        {
                            int           num4          = 0;
                            StringBuilder stringBuilder = new StringBuilder("UART Error: ", 40);
                            if ((num1 & 8) != 0)
                            {
                                stringBuilder = stringBuilder.Append("Framing,");
                                num4++;
                            }
                            if ((num1 & 0x400) != 0)
                            {
                                stringBuilder = stringBuilder.Append("IO,");
                                num4++;
                            }
                            if ((num1 & 2) != 0)
                            {
                                stringBuilder = stringBuilder.Append("Overrun,");
                                num4++;
                            }
                            if ((num1 & 1) != 0)
                            {
                                stringBuilder = stringBuilder.Append("Receive Cverflow,");
                                num4++;
                            }
                            if ((num1 & 4) != 0)
                            {
                                stringBuilder = stringBuilder.Append("Parity,");
                                num4++;
                            }
                            if ((num1 & 0x100) != 0)
                            {
                                stringBuilder = stringBuilder.Append("Transmit Overflow,");
                                num4++;
                            }
                            if (num4 <= 0)
                            {
                                if (num1 != 16)
                                {
                                    throw new CommPortException("IO Error [003]");
                                }
                                else
                                {
                                    num3 = num3 | 64;
                                }
                            }
                            else
                            {
                                stringBuilder.Length = stringBuilder.Length - 1;
                                throw new CommPortException(stringBuilder.ToString());
                            }
                        }
                    }
                    if ((num3 & 1) != 0)
                    {
                        do
                        {
                            num = 0;
                            if (Win32Com.ReadFile(this.hPort, numArray, 1, out num, intPtr))
                            {
                                if (num != 1)
                                {
                                    continue;
                                }
                                this.OnRxChar(numArray[0]);
                            }
                            else
                            {
                                Marshal.GetLastWin32Error();
                                throw new CommPortException("IO Error [004]");
                            }
                        }while (num > 0);
                    }
                    if ((num3 & 4) != 0)
                    {
                        lock (this.empty)
                        {
                            this.empty[0] = true;
                        }
                        this.OnTxDone();
                    }
                    if ((num3 & 64) != 0)
                    {
                        this.OnBreak();
                    }
                    uint num5 = 0;
                    if ((num3 & 8) != 0)
                    {
                        num5 = num5 | 16;
                    }
                    if ((num3 & 16) != 0)
                    {
                        num5 = num5 | 32;
                    }
                    if ((num3 & 32) != 0)
                    {
                        num5 = num5 | 128;
                    }
                    if ((num3 & 0x100) != 0)
                    {
                        num5 = num5 | 64;
                    }
                    if (num5 == 0)
                    {
                        continue;
                    }
                    if (Win32Com.GetCommModemStatus(this.hPort, out num2))
                    {
                        this.OnStatusChange(new CommBase.ModemStatus(num5), new CommBase.ModemStatus(num2));
                    }
                    else
                    {
                        throw new CommPortException("IO Error [005]");
                    }
                }
                throw new CommPortException("IO Error [001]");
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                Win32Com.CancelIo(this.hPort);
                if (intPtr1 != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr1);
                }
                if (intPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(intPtr);
                }
                if (exception as ThreadAbortException == null)
                {
                    this.rxException = exception;
                    this.OnRxException(exception);
                }
            }
        }
Ejemplo n.º 2
0
        public bool Open()
        {
            Win32Com.COMMPROP cOMMPROP;
            bool flag;

            Win32Com.DCB          xoffChar    = new Win32Com.DCB();
            Win32Com.COMMTIMEOUTS cOMMTIMEOUT = new Win32Com.COMMTIMEOUTS();
            Win32Com.OVERLAPPED   zero        = new Win32Com.OVERLAPPED();
            if (!this.online)
            {
                CommBase.CommBaseSettings commBaseSetting = this.CommSettings();
                this.hPort = Win32Com.CreateFile(commBaseSetting.port, 0xc0000000, 0, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
                if (this.hPort == (IntPtr)(-1))
                {
                    if ((long)Marshal.GetLastWin32Error() != (long)5)
                    {
                        this.hPort = Win32Com.CreateFile(this.AltName(commBaseSetting.port), 0xc0000000, 0, IntPtr.Zero, 3, 0x40000000, IntPtr.Zero);
                        if (this.hPort == (IntPtr)(-1))
                        {
                            if ((long)Marshal.GetLastWin32Error() != (long)5)
                            {
                                throw new CommPortException("Port Open Failure");
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                this.online = true;
                cOMMTIMEOUT.ReadIntervalTimeout        = 0xffffffff;
                cOMMTIMEOUT.ReadTotalTimeoutConstant   = 0;
                cOMMTIMEOUT.ReadTotalTimeoutMultiplier = 0;
                if (commBaseSetting.sendTimeoutMultiplier != 0)
                {
                    cOMMTIMEOUT.WriteTotalTimeoutMultiplier = commBaseSetting.sendTimeoutMultiplier;
                }
                else
                {
                    if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                    {
                        cOMMTIMEOUT.WriteTotalTimeoutMultiplier = 0x2710;
                    }
                    else
                    {
                        cOMMTIMEOUT.WriteTotalTimeoutMultiplier = 0;
                    }
                }
                cOMMTIMEOUT.WriteTotalTimeoutConstant = commBaseSetting.sendTimeoutConstant;
                Win32Com.DCB dCBPointer;
                dCBPointer = xoffChar;
                if (commBaseSetting.parity == CommBase.Parity.odd)
                {
                    flag = true;
                }
                else
                {
                    flag = commBaseSetting.parity == CommBase.Parity.even;
                }
                dCBPointer.init(flag, commBaseSetting.txFlowCTS, commBaseSetting.txFlowDSR, (int)commBaseSetting.useDTR, commBaseSetting.rxGateDSR, !commBaseSetting.txWhenRxXoff, commBaseSetting.txFlowX, commBaseSetting.rxFlowX, (int)commBaseSetting.useRTS);
                xoffChar.BaudRate = commBaseSetting.baudRate;
                xoffChar.ByteSize = (byte)commBaseSetting.dataBits;
                xoffChar.Parity   = (byte)commBaseSetting.parity;
                xoffChar.StopBits = (byte)commBaseSetting.stopBits;
                xoffChar.XoffChar = (byte)commBaseSetting.XoffChar;
                xoffChar.XonChar  = (byte)commBaseSetting.XonChar;
                if ((commBaseSetting.rxQueue != 0 || commBaseSetting.txQueue != 0) && !Win32Com.SetupComm(this.hPort, (uint)commBaseSetting.rxQueue, (uint)commBaseSetting.txQueue))
                {
                    this.ThrowException("Bad queue settings");
                }
                if (commBaseSetting.rxLowWater == 0 || commBaseSetting.rxHighWater == 0)
                {
                    if (!Win32Com.GetCommProperties(this.hPort, out cOMMPROP))
                    {
                        cOMMPROP.dwCurrentRxQueue = 0;
                    }
                    if (cOMMPROP.dwCurrentRxQueue <= 0)
                    {
                        byte  num  = 8;
                        short num1 = (short)num;
                        xoffChar.XonLim  = (short)num;
                        xoffChar.XoffLim = num1;
                    }
                    else
                    {
                        short num2 = (short)(cOMMPROP.dwCurrentRxQueue / 10);
                        short num3 = num2;
                        xoffChar.XonLim  = num2;
                        xoffChar.XoffLim = num3;
                    }
                }
                else
                {
                    xoffChar.XoffLim = (short)commBaseSetting.rxHighWater;
                    xoffChar.XonLim  = (short)commBaseSetting.rxLowWater;
                }
                if (!Win32Com.SetCommState(this.hPort, ref xoffChar))
                {
                    this.ThrowException("Bad com settings");
                }
                if (!Win32Com.SetCommTimeouts(this.hPort, ref cOMMTIMEOUT))
                {
                    this.ThrowException("Bad timeout settings");
                }
                this.stateBRK = 0;
                if (commBaseSetting.useDTR == CommBase.HSOutput.none)
                {
                    this.stateDTR = 0;
                }
                if (commBaseSetting.useDTR == CommBase.HSOutput.online)
                {
                    this.stateDTR = 1;
                }
                if (commBaseSetting.useRTS == CommBase.HSOutput.none)
                {
                    this.stateRTS = 0;
                }
                if (commBaseSetting.useRTS == CommBase.HSOutput.online)
                {
                    this.stateRTS = 1;
                }
                this.checkSends = commBaseSetting.checkAllSends;
                zero.Offset     = 0;
                zero.OffsetHigh = 0;
                if (!this.checkSends)
                {
                    zero.hEvent = IntPtr.Zero;
                }
                else
                {
                    zero.hEvent = this.writeEvent.Handle;
                    // zero.hEvent = this.writeEvent.SafeWaitHandle;
                }
                this.ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf(zero));
                Marshal.StructureToPtr(zero, this.ptrUWO, true);
                this.writeCount          = 0;
                this.empty[0]            = true;
                this.dataQueued          = false;
                this.rxException         = null;
                this.rxExceptionReported = false;
                this.rxThread            = new Thread(new ThreadStart(this.ReceiveThread));
                this.rxThread.Name       = "CommBaseRx";
                this.rxThread.Priority   = ThreadPriority.AboveNormal;
                this.rxThread.Start();
                this.startEvent.WaitOne(0x1f4, false);
                this.auto = false;
                if (!this.AfterOpen())
                {
                    this.Close();
                    return(false);
                }
                else
                {
                    this.auto = commBaseSetting.autoReopen;
                    return(true);
                }
            }
            else
            {
                return(false);
            }
        }