コード例 #1
0
        public void OpenPort()
        {
            try
            {
                _serialPort               = new SerialPort();
                _serialPort.PortName      = SelectedPort;
                _serialPort.BaudRate      = 115200;
                _serialPort.Handshake     = System.IO.Ports.Handshake.None;
                _serialPort.Parity        = Parity.None;
                _serialPort.DataBits      = 8;
                _serialPort.StopBits      = StopBits.One;
                _serialPort.ReadTimeout   = 200;
                _serialPort.WriteTimeout  = 50;
                _serialPort.DataReceived += _serialPort_DataReceived;
                _serialPort.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Could not open serial port: {ex.Message}");
                _serialPort.Close();
            }

            OpenPortCommand.RaiseCanExecuteChanged();
            ClosePortCommand.RaiseCanExecuteChanged();
            SendTextCommand.RaiseCanExecuteChanged();
            NextTextCommand.RaiseCanExecuteChanged();
            SendResetCommand.RaiseCanExecuteChanged();
            PreviousTextCommand.RaiseCanExecuteChanged();
            SendZeroCommand.RaiseCanExecuteChanged();
            IncrementStepsCommand.RaiseCanExecuteChanged();
            DecrementStepsCommand.RaiseCanExecuteChanged();
            SendCoefficientsCommand.RaiseCanExecuteChanged();
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsPortClosed)));
        }
コード例 #2
0
 public void ClosePort()
 {
     if (_serialPort != null)
     {
         _serialPort.Close();
         _serialPort.Dispose();
         _serialPort = null;
         OpenPortCommand.RaiseCanExecuteChanged();
         ClosePortCommand.RaiseCanExecuteChanged();
         SendTextCommand.RaiseCanExecuteChanged();
         NextTextCommand.RaiseCanExecuteChanged();
         SendResetCommand.RaiseCanExecuteChanged();
         PreviousTextCommand.RaiseCanExecuteChanged();
         SendZeroCommand.RaiseCanExecuteChanged();
         IncrementStepsCommand.RaiseCanExecuteChanged();
         DecrementStepsCommand.RaiseCanExecuteChanged();
         SendCoefficientsCommand.RaiseCanExecuteChanged();
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsPortClosed)));
     }
 }