Ejemplo n.º 1
0
 internal static extern bool SetCommTimeouts(IntPtr hFile, ref Win32Com.COMMTIMEOUTS lpCommTimeouts);
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);
            }
        }
Ejemplo n.º 3
0
 internal static extern bool BuildCommDCBAndTimeouts(string lpDef, ref Win32Com.DCB lpDCB, ref Win32Com.COMMTIMEOUTS lpCommTimeouts);