/// <summary> /// Open the port specified by the Port property. /// </summary> public void Open() { if (IsDisposed) { throw new ObjectDisposedException("NativeSerialPort"); } if (string.IsNullOrWhiteSpace(m_Port)) { throw new InvalidOperationException("Port must first be set"); } if (IsOpen) { throw new InvalidOperationException("Serial Port currently open"); } m_ComPortHandle = UnsafeNativeMethods.CreateFile(@"\\.\" + m_Port, NativeMethods.FileAccess.GENERIC_READ | NativeMethods.FileAccess.GENERIC_WRITE, NativeMethods.FileShare.FILE_SHARE_NONE, IntPtr.Zero, NativeMethods.CreationDisposition.OPEN_EXISTING, NativeMethods.FileAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (m_ComPortHandle.IsInvalid) { WinIOError(); } NativeMethods.FileType t = UnsafeNativeMethods.GetFileType(m_ComPortHandle); if (t != NativeMethods.FileType.FILE_TYPE_CHAR && t != NativeMethods.FileType.FILE_TYPE_UNKNOWN) { m_ComPortHandle.Close(); m_ComPortHandle = null; throw new IOException("Wrong Filetype: " + m_Port); } // Set the default parameters UnsafeNativeMethods.SetupComm(m_ComPortHandle, m_DriverInQueue, m_DriverOutQueue); m_CommState = new CommState(m_ComPortHandle, m_CommState); m_CommProperties = new CommProperties(m_ComPortHandle); m_CommModem = new CommModemStatus(m_ComPortHandle); CommOverlappedIo commIo = new CommOverlappedIo(m_ComPortHandle, m_CommIo, Port); m_CommIo.Dispose(); m_CommIo = commIo; }
/// <summary> /// Opens the serial port specified by <see cref="PortName" />. /// </summary> /// <exception cref="System.ObjectDisposedException"/> /// <exception cref="System.InvalidOperationException"> /// Port must first be set; /// or /// Serial Port currently open. /// </exception> /// <exception cref="System.IO.IOException">Wrong file type.</exception> /// <remarks> /// Opening the serial port does not set any settings (such as baud rate, etc.). On the windows implementation, /// it only sets the internal driver input and output queue. /// </remarks> public void Open() { if (m_IsDisposed) { throw new ObjectDisposedException("WinNativeSerial"); } if (string.IsNullOrWhiteSpace(PortName)) { throw new InvalidOperationException("Port must first be set"); } if (IsOpen) { throw new InvalidOperationException("Serial Port currently open"); } m_ComPortHandle = UnsafeNativeMethods.CreateFile(@"\\.\" + PortName, NativeMethods.FileAccess.GENERIC_READ | NativeMethods.FileAccess.GENERIC_WRITE, NativeMethods.FileShare.FILE_SHARE_NONE, IntPtr.Zero, NativeMethods.CreationDisposition.OPEN_EXISTING, NativeMethods.FileAttributes.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (m_ComPortHandle.IsInvalid) { WinIOError(); } NativeMethods.FileType t = UnsafeNativeMethods.GetFileType(m_ComPortHandle); bool validOverride = false; if (t != NativeMethods.FileType.FILE_TYPE_CHAR && t != NativeMethods.FileType.FILE_TYPE_UNKNOWN) { foreach (string port in GetPortNames()) { #if NETSTANDARD15 if (port.Equals(PortName, StringComparison.OrdinalIgnoreCase)) { validOverride = true; break; } #else if (port.Equals(PortName, StringComparison.InvariantCultureIgnoreCase)) { validOverride = true; break; } #endif } if (!validOverride) { m_ComPortHandle.Dispose(); m_ComPortHandle = null; throw new IOException("Wrong file type: " + PortName); } } // Set the default parameters UnsafeNativeMethods.SetupComm(m_ComPortHandle, m_DriverInQueue, m_DriverOutQueue); m_CommState = new CommState(m_ComPortHandle); m_CommProperties = new CommProperties(m_ComPortHandle); m_CommModemStatus = new CommModemStatus(m_ComPortHandle); m_CommOverlappedIo = new CommOverlappedIo(m_ComPortHandle); RegisterEvents(); }