System.IO.Ports.SerialPort.Open C# (CSharp) Méthode

Open() public méthode

public Open ( ) : void
Résultat void
		public void Open ()
		{
			if (is_open)
				throw new InvalidOperationException ("Port is already open");
			
#if !TARGET_JVM
			if (IsWindows) // Use windows kernel32 backend
				stream = new WinSerialStream (port_name, baud_rate, data_bits, parity, stop_bits, dtr_enable,
					rts_enable, handshake, read_timeout, write_timeout, readBufferSize, writeBufferSize);
			else // Use standard unix backend
#endif
				stream = new SerialPortStream (port_name, baud_rate, data_bits, parity, stop_bits, dtr_enable,
					rts_enable, handshake, read_timeout, write_timeout, readBufferSize, writeBufferSize);
			
			is_open = true;
		}

Usage Example

        public SerialDevice(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, string returnToken = "\n")
        {
            _dataReading = string.Empty;
            _dataQueue = new ConcurrentQueue<string>();

            _timeCounter = new Stopwatch();
            _timeCounter.Start();

            _COMPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits);

            _returnToken = returnToken;

            _COMPort.NewLine = _returnToken;
            _COMPort.RtsEnable = true;
            _COMPort.DtrEnable = true;

            _COMPort.ReadTimeout = SerialPort.InfiniteTimeout;
            _COMPort.WriteTimeout = SerialPort.InfiniteTimeout;

            _COMPort.Open();

            if (!_COMPort.IsOpen)
                _COMPort.Open();
            if (!_COMPort.IsOpen)
                throw new Exception("Can't connect to the COM port!");

            _serialThread = new Thread(new ThreadStart(GetSerialDataContinious));
            _serialThread.Priority = ThreadPriority.Normal;
            _serialThread.Name = string.Format("SerialHandle{0}", _serialThread.ManagedThreadId);

            _communicatyionIsActive = true;
            _serialThread.Start();
        }
All Usage Examples Of System.IO.Ports.SerialPort::Open