SettingsWindow.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Windows;
  4. namespace BarrageTransporter
  5. {
  6. /// <summary>
  7. /// SettingsWindow.xaml 的交互逻辑
  8. /// </summary>
  9. public partial class SettingsWindow : Window
  10. {
  11. AppContext appContext = AppContext.getAppContext();
  12. SerialTool serialTool = SerialTool.getSerialTool();
  13. public SettingsWindow()
  14. {
  15. InitializeComponent();
  16. }
  17. private void Window_Loaded(object sender, RoutedEventArgs e)
  18. {
  19. startOpenBarrage.IsChecked = Properties.Settings.Default.startOpenBarrage;
  20. if (appContext.PortOpen)
  21. {
  22. comboBox.Items.Clear();
  23. comboBox.Items.Add(appContext.SerialPort.PortName);
  24. comboBox.SelectedIndex = 0;
  25. comboBox.IsEnabled = false;
  26. connect.Content = "断开连接";
  27. scan.IsEnabled = false;
  28. }
  29. else
  30. {
  31. connect.Content = "连接设备";
  32. scan_Click(null, null);
  33. }
  34. }
  35. private void scan_Click(object sender, RoutedEventArgs e)
  36. {
  37. // 清除当前串口号中的所有串口名称
  38. comboBox.Items.Clear();
  39. List<string> portList = serialTool.findPort();
  40. if (portList.Count == 0)
  41. {
  42. MessageBox.Show("没有找到可用设备!", "提示");
  43. }
  44. else
  45. {
  46. portList.ForEach(
  47. delegate (string portName) {
  48. comboBox.Items.Add(portName);
  49. });
  50. //使 ListBox 显示第1个添加的索引
  51. comboBox.SelectedIndex = 0;
  52. }
  53. }
  54. private void connect_Click(object sender, RoutedEventArgs e)
  55. {
  56. if (appContext.PortOpen)
  57. {
  58. // 关闭串口
  59. if (serialTool.closePort(appContext.SerialPort))
  60. {
  61. comboBox.IsEnabled = true;
  62. scan.IsEnabled = true;
  63. scan_Click(null, null);
  64. connect.Content = "连接设备";
  65. appContext.SerialPort = null;
  66. }
  67. else
  68. {
  69. MessageBox.Show("断开连接失败!", "错误提示");
  70. }
  71. }
  72. else
  73. {
  74. // 检测串口设置
  75. string portName = comboBox.Text.Trim();
  76. if (portName == "")
  77. {
  78. MessageBox.Show("设备设置错误!", "错误提示");
  79. return;
  80. }
  81. // 打开串口
  82. SerialPort serialPort = serialTool.openPort(portName, "9600", "8", "1", "无", appContext.dataReceived);
  83. if (serialPort == null)
  84. {
  85. // 打开串口失败
  86. MessageBox.Show("连接设备失败!", "错误提示");
  87. }
  88. else
  89. {
  90. comboBox.IsEnabled = false;
  91. scan.IsEnabled = false;
  92. connect.Content = "断开连接";
  93. appContext.SerialPort = serialPort;
  94. }
  95. }
  96. }
  97. private void ok_Click(object sender, RoutedEventArgs e)
  98. {
  99. Properties.Settings.Default.startOpenBarrage = (bool)startOpenBarrage.IsChecked;
  100. Properties.Settings.Default.Save();
  101. Properties.Settings.Default.Reload();
  102. this.Close();
  103. }
  104. }
  105. }