public void Close() { if (!isOpen) throw new InvalidOperationException("Serial Port - port already closed!"); if (internalSerialStream != null) { internalSerialStream.Close(); internalSerialStream = null; } isOpen = false; }
public void Open() { if (isOpen) throw new InvalidOperationException("Serial Port - port already open!"); if (parity < Parity.None || parity > Parity.Space) throw new ArgumentOutOfRangeException("parity", Resources.GetResourceString("ArgumentOutOfRange_Enum")); if (dataBits < minDataBits || dataBits > maxDataBits) throw new ArgumentOutOfRangeException("dataBits", Resources.GetResourceString("ArgumentOutOfRange_Bounds_Lower_Upper", minDataBits, maxDataBits)); if (stopBits < StopBits.One || stopBits > StopBits.OnePointFive) throw new ArgumentOutOfRangeException("stopBits", Resources.GetResourceString("ArgumentOutOfRange_Enum")); if (baudRate <= 0) throw new ArgumentOutOfRangeException("baudRate", Resources.GetResourceString("ArgumentOutOfRange_NeedPosNum")); if (portName == null) throw new ArgumentNullException("resource", Resources.GetResourceString("ArgumentNull_String")); if (handshake < Handshake.None || handshake > Handshake.RequestToSendXOnXOff) throw new ArgumentOutOfRangeException("handshake", Resources.GetResourceString("ArgumentOutOfRange_Enum")); if (readTimeout < 0 && readTimeout != SerialPort.InfiniteTimeout) throw new ArgumentOutOfRangeException("readTimeout", Resources.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (writeTimeout <= 0 && writeTimeout != SerialPort.InfiniteTimeout) throw new ArgumentOutOfRangeException("writeTimeout", Resources.GetResourceString("ArgumentOutOfRange_NeedPosNum")); if (portName.StartsWith("\\\\")) throw new ArgumentException("resource", Resources.GetResourceString("Arg_SecurityException")); internalSerialStream = new SerialStream(portName, baudRate, parity, dataBits, stopBits, readTimeout, writeTimeout, handshake, dtrEnable, rtsEnable, discardNull, parityReplace); internalSerialStream.ErrorEvent += new SerialEventHandler(CatchErrorEvents); internalSerialStream.PinChangedEvent += new SerialEventHandler(CatchPinChangedEvents); internalSerialStream.ReceivedEvent += new SerialEventHandler(CatchReceivedEvents); isOpen = true; }