internal void init(bool parity, bool outCTS, bool outDSR, int dtr, bool inDSR, bool txc, bool xOut, bool xIn, int rts) { this.DCBlength = 28; this.PackedValues = 0x4001; if (parity) { Win32Com.DCB packedValues = this; packedValues.PackedValues = packedValues.PackedValues | 2; } if (outCTS) { Win32Com.DCB dCB = this; dCB.PackedValues = dCB.PackedValues | 4; } if (outDSR) { Win32Com.DCB packedValues1 = this; packedValues1.PackedValues = packedValues1.PackedValues | 8; } Win32Com.DCB dCB1 = this; dCB1.PackedValues = dCB1.PackedValues | (dtr & 3) << 4; if (inDSR) { Win32Com.DCB packedValues2 = this; packedValues2.PackedValues = packedValues2.PackedValues | 64; } if (txc) { Win32Com.DCB dCB2 = this; dCB2.PackedValues = dCB2.PackedValues | 128; } if (xOut) { Win32Com.DCB packedValues3 = this; packedValues3.PackedValues = packedValues3.PackedValues | 0x100; } if (xIn) { Win32Com.DCB dCB3 = this; dCB3.PackedValues = dCB3.PackedValues | 0x200; } Win32Com.DCB packedValues4 = this; packedValues4.PackedValues = packedValues4.PackedValues | (rts & 3) << 12; }
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); } }
internal static extern bool SetCommState(IntPtr hFile, ref Win32Com.DCB lpDCB);
internal static extern bool BuildCommDCBAndTimeouts(string lpDef, ref Win32Com.DCB lpDCB, ref Win32Com.COMMTIMEOUTS lpCommTimeouts);