Exemplo n.º 1
0
 private void OpenCloseSerialPort(object sender, RoutedEventArgs e)
 {
     if (string.IsNullOrWhiteSpace(CmbComPort.Text))
     {
         LblPortStatus.Content = "知道什么叫打开串口吗?要先有串口,懂吗?";
         return;
     }
     if (!SerialPortHelper.IsSerialPortOpened())
     {
         SerialPortHelper.OpenSerialPort(CmbComPort.Text,
                                         ((BaundRateSelectItem)CmbBoundRate.SelectedItem).Value,
                                         ((DataBitSelectItem)CmbDataBit.SelectedItem).Value,
                                         ((StopBitSelectItem)CmbStopBit.SelectedItem).Value,
                                         ((ParitySelectItem)CmbParity.SelectionBoxItem).Value);
     }
     else
     {
         SerialPortHelper.CloseSerialPort();
     }
     LblPortStatus.Content = SerialPortHelper.GetOperateMessage();
 }
Exemplo n.º 2
0
 private void SwitchSerialPortStatus(object sender, RoutedEventArgs e)
 {
     if (!_portHelper.SerialPortIsOpen)
     {
         Dispatcher.Invoke(() =>
         {
             LockSerialPortParamControls(false);
             _portHelper.OpenSerialPort(CmbComPort.Text
                                        , (int)CmbBoundRate.SelectedValue
                                        , (int)CmbDataBit.SelectedValue
                                        , (StopBits)CmbStopBit.SelectedValue
                                        , (Parity)CmbParity.SelectedValue);
             if (_portHelper.SerialPortIsOpen)
             {
                 BtnSwitchSerialPortStatus.Content = "关闭串口";
                 _messageBarMessage = "串口已经打开";
             }
             else
             {
                 LockSerialPortParamControls(true);
                 _messageBarMessage = "串口打开失败,请检查是否被占用";
             }
         });
     }
     else
     {
         Dispatcher.Invoke(() =>
         {
             _portHelper.CloseSerialPort();
             if (!_portHelper.SerialPortIsOpen)
             {
                 BtnSwitchSerialPortStatus.Content = "打开串口";
                 LockSerialPortParamControls(true);
                 _messageBarMessage = "串口已经关闭";
             }
         });
     }
 }