/// <summary> /// 串口初始化,打开了串口 /// </summary> private void CallCOMInit() { int callcombaudrate = int.Parse(ConfigurationManager.AppSettings["CallCOMBaudrate"]); int callcomdatabits = int.Parse(ConfigurationManager.AppSettings["CallCOMDataBits"]); string callcomstopbits = ConfigurationManager.AppSettings["CallCOMStopBits"]; string callcomparity = ConfigurationManager.AppSettings["CallCOMParity"]; try { SPComCall = new COM.SerialPortWrapper(GlobalPara.Gcallcomname, callcombaudrate, callcomparity, callcomdatabits, callcomstopbits); SPComCall.OnDataReceived += new SerialDataReceivedEventHandler(SPComCall_OnDataReceived); SPComCall.Open(); btn_Set.IsEnabled = true; } catch (System.UnauthorizedAccessException ex) { if (MessageBoxResult.OK == MessageBox.Show(ex.Message + "请先暂停此系统或其他系统使用当前串口!", "错误", MessageBoxButton.OK, MessageBoxImage.Error)) { this.Close(); } } catch (System.Exception ex) { if (MessageBoxResult.OK == MessageBox.Show(ex.Message + "请在<通信设置>中设置新的串口!", "错误", MessageBoxButton.OK, MessageBoxImage.Error)) { this.Close(); } } }
/// <summary> /// 启动系统 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, RoutedEventArgs e) { try { if (SPComCall == null) { CallCOMInit(); } if (!SPComCall.IsOpen) { SPComCall.portname = GlobalPara.GCallcomname; SPComCall.Open(); } } catch (System.UnauthorizedAccessException ex) { MessageBox.Show(ex.Message + "请先暂停此系统或其他系统使用当前串口!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } catch (System.IO.IOException ex) { MessageBox.Show(ex.Message + "请在<通信设置>中设置新的串口!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (SPComCall.IsOpen) { imgCOM.Source = new BitmapImage(new Uri("pack://application:,,,/Image/Light_Open_24.png")); lblcomstate.Content = "打开"; lblcomstate.Foreground = Brushes.Green; } lblsystemstate.Content = "打开"; lblsystemstate.Foreground = Brushes.Green; imgSystem.Source = new BitmapImage(new Uri("pack://application:,,,/Image/Light_Open_24.png")); btnStart.IsEnabled = false; btnEnd.IsEnabled = true; }