串口控件可以直接拖拉IDE組件下拉中的SerialPort控件 或者手動輸入 using System.IO.Ports; SerialPort RS1 = new SerialPort(); 獲取串口列表 private void btnCPSX_Click(object sender, EventArgs e) { string[] ports = SerialPort.GetPortNames(); Array.Sort(ports); CPList.Items.Clear(); CPList.Items.AddRange(ports); CPList.SelectedIndex = CPList.Items.Count > 0 ? 0 : -1; } List<string> comPostList = System.IO.Ports.SerialPort.GetPortNames().ToList(); foreach (var cp in comPostList) { CPList.Items.Add(cp); } |
|