bird_tool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.IO.Ports;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.IO;
  12. using System.Net;
  13. using System.Data.SqlClient;
  14. using System.Runtime.Serialization;
  15. using MySql.Data.MySqlClient;
  16. using System.Threading;
  17. using System.Text.RegularExpressions;
  18. using Excel = Microsoft.Office.Interop.Excel;
  19. using NAudio.CoreAudioApi;
  20. using Emgu.CV;
  21. using Emgu.CV.Structure;
  22. namespace Bird_tool
  23. {
  24. public partial class bird_tool : Form
  25. {
  26. private SerialPort _uart = new SerialPort();//定义串口
  27. private BindingList<string> _portList = new BindingList<string>();
  28. private string _lastSelectedPort = ""; // 保存上次选择的串口
  29. // 是否加载了配置
  30. private bool flag_load_config = false;
  31. // 是否选择串口
  32. private bool flag_selected_serial = false;
  33. public bird_tool()
  34. {
  35. InitializeComponent();
  36. //serialTimer.Interval = 1000;
  37. //serialTimer.Enabled = true;
  38. }
  39. private void Log(string message)
  40. {
  41. System.Diagnostics.Debug.WriteLine(message);
  42. }
  43. private void Tool_load(object sender, EventArgs e)
  44. {
  45. SerialPort_Load();
  46. }
  47. private void SerialPort_Load()
  48. {
  49. // 设置ComboBox数据源
  50. el_serialList.DataSource = _portList;
  51. // 添加事件处理程序,用于保存当前选择
  52. el_serialList.SelectedIndexChanged += (s, ev) =>
  53. {
  54. if (el_serialList.SelectedItem != null)
  55. _lastSelectedPort = el_serialList.SelectedItem.ToString();
  56. };
  57. // 初始加载串口列表
  58. RefreshPortList();
  59. }
  60. // 刷新串口列表并保持选择
  61. public void RefreshPortList()
  62. {
  63. // 获取当前选中的端口(如果尚未选择,则使用上次保存的值)
  64. string currentSelection = el_serialList.SelectedItem != null
  65. ? el_serialList.SelectedItem.ToString()
  66. : _lastSelectedPort;
  67. // 获取最新串口列表
  68. string[] newPorts = SerialPort.GetPortNames();
  69. // 检查是否有串口
  70. if (newPorts.Length == 0)
  71. {
  72. // 没有串口时的处理
  73. _portList.Clear();
  74. _portList.Add("");
  75. el_serialList.SelectedIndex = 0;
  76. return;
  77. }
  78. // 保存当前选择状态
  79. bool wasEmpty = string.IsNullOrEmpty(currentSelection);
  80. // 更新绑定列表
  81. _portList.Clear();
  82. // 添加空选项
  83. _portList.Add("");
  84. // 添加所有串口
  85. foreach (var port in newPorts)
  86. {
  87. _portList.Add(port);
  88. }
  89. // 尝试恢复之前的选中项
  90. if (!wasEmpty && _portList.Contains(currentSelection))
  91. {
  92. el_serialList.SelectedItem = currentSelection;
  93. }
  94. else
  95. {
  96. // 如果之前的选项不存在,选择第一个有效串口(非空)
  97. el_serialList.SelectedIndex = 0;
  98. }
  99. // 更新保存的选择
  100. if (el_serialList.SelectedItem != null)
  101. _lastSelectedPort = el_serialList.SelectedItem.ToString();
  102. }
  103. // 示例刷新按钮事件
  104. private void Evt_refresh(object sender, EventArgs e)
  105. {
  106. RefreshPortList();
  107. }
  108. private void el_serialList_SelectedIndexChanged(object sender, EventArgs e)
  109. {
  110. }
  111. }
  112. }