internal static bool SetCommTimeouts(IntPtr hPort, CommTimeouts timeouts) { if (FullFramework) { return(Convert.ToBoolean(WinSetCommTimeouts(hPort, timeouts))); } else { return(Convert.ToBoolean(CESetCommTimeouts(hPort, timeouts))); } }
public bool Open() { if (isOpen) { return(false); } hPort = CommAPI.CreateFile(portName); if (hPort == (IntPtr)CommAPI.INVALID_HANDLE_VALUE) { int e = Marshal.GetLastWin32Error(); if (e == (int)APIErrors.ERROR_ACCESS_DENIED) { // port is unavailable return(false); } if (e == (int)APIErrors.ERROR_PORT_UNAVAIL) { // port is unavailable return(false); } // ClearCommError failed! string error = String.Format("CreateFile Failed: {0}", e); throw new CommPortException(error); } isOpen = true; // set queue sizes CommAPI.SetupComm(hPort, rxBufferSize, txBufferSize); // transfer the port settings to a DCB structure dcb.BaudRate = (uint)portSettings.BasicSettings.BaudRate; dcb.ByteSize = portSettings.BasicSettings.ByteSize; dcb.EofChar = (sbyte)portSettings.EOFChar; dcb.ErrorChar = (sbyte)portSettings.ErrorChar; dcb.EvtChar = (sbyte)portSettings.EVTChar; dcb.fAbortOnError = portSettings.AbortOnError; dcb.fBinary = true; dcb.fDsrSensitivity = portSettings.DSRSensitive; dcb.fDtrControl = (DCB.DtrControlFlags)portSettings.DTRControl; dcb.fErrorChar = portSettings.ReplaceErrorChar; dcb.fInX = portSettings.InX; dcb.fNull = portSettings.DiscardNulls; dcb.fOutX = portSettings.OutX; dcb.fOutxCtsFlow = portSettings.OutCTS; dcb.fOutxDsrFlow = portSettings.OutDSR; dcb.fParity = (portSettings.BasicSettings.Parity == Parity.none) ? false : true; dcb.fRtsControl = (DCB.RtsControlFlags)portSettings.RTSControl; dcb.fTXContinueOnXoff = portSettings.TxContinueOnXOff; dcb.Parity = (byte)portSettings.BasicSettings.Parity; dcb.StopBits = (byte)portSettings.BasicSettings.StopBits; dcb.XoffChar = (sbyte)portSettings.XoffChar; dcb.XonChar = (sbyte)portSettings.XonChar; dcb.XonLim = dcb.XoffLim = (ushort)(rxBufferSize / 10); CommAPI.SetCommState(hPort, dcb); // store some state values brk = 0; dtr = dcb.fDtrControl == DCB.DtrControlFlags.Enable ? 1 : 0; rts = dcb.fRtsControl == DCB.RtsControlFlags.Enable ? 1 : 0; // set the Comm timeouts CommTimeouts ct = new CommTimeouts(); // reading we'll return immediately // this doesn't seem to work as documented ct.ReadIntervalTimeout = uint.MaxValue; // this = 0xffffffff // ct.ReadIntervalTimeout = 2; ct.ReadTotalTimeoutConstant = 2; ct.ReadTotalTimeoutMultiplier = uint.MaxValue; // writing we'll give 5 seconds ct.WriteTotalTimeoutConstant = 5; ct.WriteTotalTimeoutMultiplier = 0; CommAPI.SetCommTimeouts(hPort, ct); // start the receive thread eventThread = new Thread(new ThreadStart(CommEventThread)); eventThread.Priority = ThreadPriority.AboveNormal; eventThread.Start(); // wait for the thread to actually get spun up threadStarted.WaitOne(); return(true); }
private static extern int WinSetCommTimeouts(IntPtr hFile, CommTimeouts timeouts);