Exemplo n.º 1
0
        public bool Open()
        {
            Win32Com.DCB          lpDCB          = new Win32Com.DCB();
            Win32Com.COMMTIMEOUTS lpCommTimeouts = new Win32Com.COMMTIMEOUTS();
            Win32Com.OVERLAPPED   overlapped     = new Win32Com.OVERLAPPED();
            if (m_online)
            {
                return(false);
            }
            CommBase.CommBaseSettings commBaseSettings = CommSettings();
            m_hPort = Win32Com.CreateFile(commBaseSettings.Port, 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
            if (m_hPort == (IntPtr)(-1))
            {
                if ((long)Marshal.GetLastWin32Error() == 5L)
                {
                    return(false);
                }
                m_hPort = Win32Com.CreateFile(AltName(commBaseSettings.Port), 3221225472U, 0U, IntPtr.Zero, 3U, 1073741824U, IntPtr.Zero);
                if (m_hPort == (IntPtr)(-1))
                {
                    if ((long)Marshal.GetLastWin32Error() == 5L)
                    {
                        return(false);
                    }
                    else
                    {
                        throw new CommPortException("Port Open Failure");
                    }
                }
            }
            m_online = true;

            lpCommTimeouts.ReadIntervalTimeout         = uint.MaxValue;
            lpCommTimeouts.ReadTotalTimeoutConstant    = 0U;
            lpCommTimeouts.ReadTotalTimeoutMultiplier  = 0U;
            lpCommTimeouts.WriteTotalTimeoutMultiplier =
                commBaseSettings.SendTimeoutMultiplier != 0
                                ? (uint)commBaseSettings.SendTimeoutMultiplier
                                : (Environment.OSVersion.Platform != PlatformID.Win32NT ? 10000U : 0U);
            lpCommTimeouts.WriteTotalTimeoutConstant = (uint)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(m_hPort, (uint)commBaseSettings.RxQueue, (uint)commBaseSettings.TxQueue)
                )
            {
                ThrowException("Bad queue settings");
            }

            if (commBaseSettings.RxLowWater == 0 || commBaseSettings.RxHighWater == 0)
            {
                Win32Com.COMMPROP cp;
                if (!Win32Com.GetCommProperties(m_hPort, out cp))
                {
                    cp.dwCurrentRxQueue = 0;
                }
                lpDCB.XoffLim =
                    (cp.dwCurrentRxQueue <= 0U)
                                        ? (lpDCB.XonLim = 8)
                                        : (lpDCB.XonLim = (short)((int)cp.dwCurrentRxQueue / 10)
                                           );
            }
            else
            {
                lpDCB.XoffLim = (short)commBaseSettings.RxHighWater;
                lpDCB.XonLim  = (short)commBaseSettings.RxLowWater;
            }

            if (!Win32Com.SetCommState(m_hPort, ref lpDCB))
            {
                ThrowException("Bad com settings");
            }

            if (!Win32Com.SetCommTimeouts(m_hPort, ref lpCommTimeouts))
            {
                ThrowException("Bad timeout settings");
            }

            m_checkSends          = commBaseSettings.CheckAllSends;
            overlapped.Offset     = 0U;
            overlapped.OffsetHigh = 0U;
            overlapped.hEvent     = !m_checkSends ? IntPtr.Zero : m_writeEvent.SafeWaitHandle.DangerousGetHandle();
            m_ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf((object)overlapped));
            Marshal.StructureToPtr((object)overlapped, m_ptrUWO, true);
            m_writeCount          = 0;
            m_empty[0]            = true;
            m_RxException         = (Exception)null;
            m_RxExceptionReported = false;
            m_RxThread            = new Thread(new ThreadStart(ReceiveThread));
            m_RxThread.Name       = "CommBaseRx";
            m_RxThread.Priority   = ThreadPriority.AboveNormal;
            m_RxThread.Start();
            m_startEvent.WaitOne(500, false);
            m_auto = false;
            if (AfterOpen())
            {
                m_auto = commBaseSettings.AutoReopen;
                return(true);
            }
            else
            {
                Close();
                return(false);
            }
        }