/// <summary> /// Opens the port. /// </summary> /// <exception cref="IOException"> /// Raises IOException if cannot open port, or if some error happened during reading or writing port settings. Read the exception message to clarify. /// </exception> public void Open() { serialHandle = NativeMethods.CreateFile("\\\\.\\" + this.PortName, (uint)(EFileAccess.FILE_GENERIC_READ | EFileAccess.FILE_GENERIC_WRITE), 0, IntPtr.Zero, (uint)ECreationDisposition.OpenExisting, (uint)EFileAttributes.Normal, IntPtr.Zero); if (serialHandle.IsInvalid) { throw new IOException("Cannot open " + this.PortName); } // Flush the input and output buffer and abort all outstanding overlapped read and write operations Debug.WriteLine("Before :" + BytesToRead.ToString()); COMMTIMEOUTS timeout = new COMMTIMEOUTS(); timeout.ReadIntervalTimeout = 0; timeout.ReadTotalTimeoutConstant = 0; timeout.ReadTotalTimeoutMultiplier = 0; timeout.WriteTotalTimeoutConstant = 0; timeout.WriteTotalTimeoutMultiplier = 0; if (!NativeMethods.SetCommTimeouts(serialHandle, ref timeout)) { throw new IOException("SetCommTimeouts error!"); } SetParams(); IsOpen = true; }
/// <summary> /// Opens the port. /// </summary> /// <exception cref="IOException"> /// Raises IOException if cannot open port, or if some error happened during reading or writing port settings. Read the exception message to clarify. /// </exception> public void Open() { serialHandle = NativeMethods.CreateFile("\\\\.\\" + this.PortName, (uint)(EFileAccess.FILE_GENERIC_READ | EFileAccess.FILE_GENERIC_WRITE), 0, IntPtr.Zero, (uint)ECreationDisposition.OpenExisting, (uint)EFileAttributes.Normal, IntPtr.Zero); if (serialHandle.IsInvalid) throw new IOException("Cannot open " + this.PortName); // Flush the input and output buffer and abort all outstanding overlapped read and write operations Debug.WriteLine("Before :" + BytesToRead.ToString()); COMMTIMEOUTS timeout = new COMMTIMEOUTS(); timeout.ReadIntervalTimeout = 0; timeout.ReadTotalTimeoutConstant = 0; timeout.ReadTotalTimeoutMultiplier = 0; timeout.WriteTotalTimeoutConstant = 0; timeout.WriteTotalTimeoutMultiplier = 0; if (!NativeMethods.SetCommTimeouts(serialHandle, ref timeout)) throw new IOException("SetCommTimeouts error!"); SetParams(); IsOpen = true; }
internal static extern bool SetCommTimeouts(SafeFileHandle hFile, [In] ref COMMTIMEOUTS lpCommTimeouts);