// this method is used by SerialPort upon SerialStream's creation
        internal SerialStream(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int readTimeout, int writeTimeout, Handshake handshake,
                              bool dtrEnable, bool rtsEnable, bool discardNull, byte parityReplace)
        {
            if (portName == null)
            {
                throw new ArgumentNullException(nameof(portName));
            }

            CheckBaudRate(baudRate);

            // Error checking done in SerialPort.

            SafeSerialDeviceHandle tempHandle = SafeSerialDeviceHandle.Open(portName);

            try
            {
                _handle = tempHandle;
                // set properties of the stream that exist as members in SerialStream
                _portName     = portName;
                _handshake    = handshake;
                _parity       = parity;
                _readTimeout  = readTimeout;
                _writeTimeout = writeTimeout;
                _baudRate     = baudRate;
                _stopBits     = stopBits;
                _dataBits     = dataBits;

                if (Interop.Termios.TermiosReset(_handle, _baudRate, _dataBits, _stopBits, _parity, _handshake) != 0)
                {
                    throw new ArgumentException();
                }

                DtrEnable = dtrEnable;
                BaudRate  = baudRate;

                // now set this.RtsEnable to the specified value.
                // Handshake takes precedence, this will be a nop if
                // handshake is either RequestToSend or RequestToSendXOnXOff
                if ((handshake != Handshake.RequestToSend && handshake != Handshake.RequestToSendXOnXOff))
                {
                    // query and cache the initial RtsEnable value
                    // so that set_RtsEnable can do the (value != rtsEnable) optimization
                    _rtsEnable = RtsEnabledNative();
                    RtsEnable  = rtsEnable;
                }
            }
            catch
            {
                // if there are any exceptions after the call to CreateFile, we need to be sure to close the
                // handle before we let them continue up.
                tempHandle.Dispose();
                _handle = null;
                throw;
            }

            _processReadDelegate     = ProcessRead;
            _processWriteDelegate    = ProcessWrite;
            _lastTotalBytesAvailable = TotalBytesAvailable;
        }
Esempio n. 2
0
        internal static SafeSerialDeviceHandle Open(string portName)
        {
            Debug.Assert(portName != null);
            SafeSerialDeviceHandle handle = Interop.Serial.SerialPortOpen(portName);

            if (handle.IsInvalid)
            {
                // exception type is matching Windows
                throw new UnauthorizedAccessException(
                          SR.Format(SR.UnauthorizedAccess_IODenied_Port, portName),
                          Interop.GetIOException(Interop.Sys.GetLastErrorInfo()));
            }

            return(handle);
        }
Esempio n. 3
0
        protected override void Dispose(bool disposing)
        {
            _ioLoopFinished = true;

            if (disposing)
            {
                _ioLoop?.GetAwaiter().GetResult();
                _ioLoop = null;

                FinishPendingIORequests();

                if (_handle != null)
                {
                    _handle.Dispose();
                    _handle = null;
                }
            }

            base.Dispose(disposing);
        }
Esempio n. 4
0
        // this method is used by SerialPort upon SerialStream's creation
        internal SerialStream(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int readTimeout, int writeTimeout, Handshake handshake,
                              bool dtrEnable, bool rtsEnable, bool discardNull, byte parityReplace)
        {
            ArgumentNullException.ThrowIfNull(portName);

            CheckBaudRate(baudRate);

            // Error checking done in SerialPort.

            SafeSerialDeviceHandle tempHandle = SafeSerialDeviceHandle.Open(portName);

            try
            {
                _handle = tempHandle;
                // set properties of the stream that exist as members in SerialStream
                _portName     = portName;
                _handshake    = handshake;
                _parity       = parity;
                _readTimeout  = readTimeout;
                _writeTimeout = writeTimeout;
                _baudRate     = baudRate;
                _stopBits     = stopBits;
                _dataBits     = dataBits;

                if (Interop.Termios.TermiosReset(_handle, _baudRate, _dataBits, _stopBits, _parity, _handshake) != 0)
                {
                    throw new ArgumentException();
                }

                try
                {
                    DtrEnable = dtrEnable;
                }
                catch (IOException) when(dtrEnable == false)
                {
                    // An IOException can be thrown when using a virtual port from eg. socat, which doesn't implement
                    // the required termios command for setting DtrEnable, but it still works without setting the value
                    // so we ignore this error in the constructor only if being set to false (which is the default).
                    // When the property is set manually the exception is still thrown.
                }

                BaudRate = baudRate;

                // now set this.RtsEnable to the specified value.
                // Handshake takes precedence, this will be a nop if
                // handshake is either RequestToSend or RequestToSendXOnXOff
                if ((handshake != Handshake.RequestToSend && handshake != Handshake.RequestToSendXOnXOff))
                {
                    try
                    {
                        // query and cache the initial RtsEnable value
                        // so that set_RtsEnable can do the (value != rtsEnable) optimization
                        _rtsEnable = RtsEnabledNative();
                        RtsEnable  = rtsEnable;
                    }
                    catch (IOException) when(rtsEnable == false)
                    {
                        // An IOException can be thrown when using a virtual port from eg. socat, which doesn't implement
                        // the required termios command for setting RtsEnable, but it still works without setting the value
                        // so we ignore this error in the constructor only if being set to false (which is the default).
                        // When the property is set manually the exception is still thrown.
                    }
                }
            }
            catch
            {
                // if there are any exceptions after the call to CreateFile, we need to be sure to close the
                // handle before we let them continue up.
                tempHandle.Dispose();
                _handle = null;
                throw;
            }

            _processReadDelegate     = ProcessRead;
            _processWriteDelegate    = ProcessWrite;
            _lastTotalBytesAvailable = TotalBytesAvailable;
        }