public bool Open() { DCB lpDCB = default(DCB); COMMTIMEOUTS lpCommTimeouts = default(COMMTIMEOUTS); OVERLAPPED overlapped = default(OVERLAPPED); if (!this.online) { CommBaseSettings settings = this.CommSettings(); this.hPort = Win32Com.CreateFile(settings.port, 3221225472u, 0u, IntPtr.Zero, 3u, 1073741824u, IntPtr.Zero); if (this.hPort == (IntPtr)(-1)) { if ((long)Marshal.GetLastWin32Error() != 5L) { throw new CommPortException("Port Open Failure"); } return(false); } else { this.online = true; lpCommTimeouts.ReadIntervalTimeout = 0; lpCommTimeouts.ReadTotalTimeoutConstant = 0; lpCommTimeouts.ReadTotalTimeoutMultiplier = 0; lpCommTimeouts.WriteTotalTimeoutConstant = settings.sendTimeoutConstant; lpCommTimeouts.WriteTotalTimeoutMultiplier = settings.sendTimeoutMultiplier; lpDCB.init(settings.parity == Parity.odd || settings.parity == Parity.even, settings.txFlowCTS, settings.txFlowDSR, (int)settings.useDTR, settings.rxGateDSR, !settings.txWhenRxXoff, settings.txFlowX, settings.rxFlowX, (int)settings.useRTS); lpDCB.BaudRate = settings.baudRate; lpDCB.ByteSize = (byte)settings.dataBits; lpDCB.Parity = (byte)settings.parity; lpDCB.StopBits = (byte)settings.stopBits; lpDCB.XoffChar = (byte)settings.XoffChar; lpDCB.XonChar = (byte)settings.XonChar; lpDCB.XoffLim = (short)settings.rxHighWater; lpDCB.XonLim = (short)settings.rxLowWater; if ((settings.rxQueue != 0 || settings.txQueue != 0) && !Win32Com.SetupComm(this.hPort, (uint)settings.rxQueue, (uint)settings.txQueue)) { this.ThrowException("Bad queue settings"); } if (!Win32Com.SetCommState(this.hPort, ref lpDCB)) { this.ThrowException("Bad com settings"); } if (!Win32Com.SetCommTimeouts(this.hPort, ref lpCommTimeouts)) { this.ThrowException("Bad timeout settings"); } this.stateBRK = 0; if (settings.useDTR == HSOutput.none) { this.stateDTR = 0; } if (settings.useDTR == HSOutput.online) { this.stateDTR = 1; } if (settings.useRTS == HSOutput.none) { this.stateRTS = 0; } if (settings.useRTS == HSOutput.online) { this.stateRTS = 1; } this.checkSends = settings.checkAllSends; overlapped.Offset = 0u; overlapped.OffsetHigh = 0u; if (this.checkSends) { overlapped.hEvent = this.writeEvent.SafeWaitHandle.DangerousGetHandle(); } else { overlapped.hEvent = IntPtr.Zero; } this.ptrUWO = Marshal.AllocHGlobal(Marshal.SizeOf(overlapped)); Marshal.StructureToPtr(overlapped, this.ptrUWO, true); this.writeCount = 0; 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(); Thread.Sleep(1); this.auto = false; if (this.AfterOpen()) { this.auto = settings.autoReopen; return(true); } this.Close(); } } return(false); }
internal static extern bool GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts);
internal static extern bool SetCommTimeouts(IntPtr hFile, [In] ref COMMTIMEOUTS lpCommTimeouts);
internal static extern bool BuildCommDCBAndTimeouts(string lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts);