123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.IO.Ports;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Data.SqlClient;
- using System.Runtime.Serialization;
- using MySql.Data.MySqlClient;
- using System.Threading;
- using System.Text.RegularExpressions;
- using Excel = Microsoft.Office.Interop.Excel;
- using NAudio.CoreAudioApi;
- using Emgu.CV;
- using Emgu.CV.Structure;
- namespace Bird_tool
- {
- public partial class bird_tool : Form
- {
- private SerialPort _uart = new SerialPort();//定义串口
- private BindingList<string> _portList = new BindingList<string>();
- private string _lastSelectedPort = ""; // 保存上次选择的串口
- // 是否加载了配置
- private bool flag_load_config = false;
- // 是否选择串口
- private bool flag_selected_serial = false;
- public bird_tool()
- {
- InitializeComponent();
- //serialTimer.Interval = 1000;
- //serialTimer.Enabled = true;
- }
- private void Log(string message)
- {
- System.Diagnostics.Debug.WriteLine(message);
- }
-
- private void Tool_load(object sender, EventArgs e)
- {
- SerialPort_Load();
- }
- private void SerialPort_Load()
- {
- // 设置ComboBox数据源
- el_serialList.DataSource = _portList;
- // 添加事件处理程序,用于保存当前选择
- el_serialList.SelectedIndexChanged += (s, ev) =>
- {
- if (el_serialList.SelectedItem != null)
- _lastSelectedPort = el_serialList.SelectedItem.ToString();
- };
- // 初始加载串口列表
- RefreshPortList();
- }
- // 刷新串口列表并保持选择
- public void RefreshPortList()
- {
- // 获取当前选中的端口(如果尚未选择,则使用上次保存的值)
- string currentSelection = el_serialList.SelectedItem != null
- ? el_serialList.SelectedItem.ToString()
- : _lastSelectedPort;
- // 获取最新串口列表
- string[] newPorts = SerialPort.GetPortNames();
- // 检查是否有串口
- if (newPorts.Length == 0)
- {
- // 没有串口时的处理
- _portList.Clear();
- _portList.Add("");
- el_serialList.SelectedIndex = 0;
- return;
- }
- // 保存当前选择状态
- bool wasEmpty = string.IsNullOrEmpty(currentSelection);
- // 更新绑定列表
- _portList.Clear();
- // 添加空选项
- _portList.Add("");
- // 添加所有串口
- foreach (var port in newPorts)
- {
- _portList.Add(port);
- }
- // 尝试恢复之前的选中项
- if (!wasEmpty && _portList.Contains(currentSelection))
- {
- el_serialList.SelectedItem = currentSelection;
- }
- else
- {
- // 如果之前的选项不存在,选择第一个有效串口(非空)
- el_serialList.SelectedIndex = 0;
- }
- // 更新保存的选择
- if (el_serialList.SelectedItem != null)
- _lastSelectedPort = el_serialList.SelectedItem.ToString();
- }
- // 示例刷新按钮事件
- private void Evt_refresh(object sender, EventArgs e)
- {
- RefreshPortList();
- }
- private void el_serialList_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- }
- }
- }
|