tx
2025-04-10 2538101febc78f525945da72c7cdcb2589f9e6ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel; 
using System.Data;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.IO.Ports;
 
namespace TProduct.Common
{
    //COM端口
    public class ComPortHelper
    {
        public ComPortHelper() { }
 
        //波特率列表
        public Int32[] BaudRatesList = {
            100,300,600,1200,2400,4800,9600,14400,19200,
            38400,56000,57600,115200,128000,256000
        };
 
        
 
        //设置当前电脑有哪个串口可用的Combox控件
        //public static bool InitialDispContrl(System.Windows.Forms.ComboBox ctrl)
        //{
        //    ctrl.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        //    if (ctrl.Items.Count == 0)
        //    {
        //        MessageBox.Show("没有COM端口");
        //        return false;
        //    }
        //    ctrl.SelectedIndex = 0;
        //    return true;
        //}
 
        //public static bool InitialDispContrl(DevExpress.XtraEditors.ComboBoxEdit ctrl)
        //{
        //    ctrl.Properties.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        //    if (ctrl.Properties.Items.Count == 0)
        //    {
        //        MessageBox.Show("没有COM端口");
        //        return false;
        //    }
        //    ctrl.SelectedIndex = 0;
        //    return true;
        //}
        //public static bool InitialDispContrl(DevExpress.XtraEditors.ComboBoxEdit ctrl,string defaultComName)
        //{
        //    var list = System.IO.Ports.SerialPort.GetPortNames();
        //    if (list == null || list.Length == 0)
        //    {
        //        MessageBox.Show("没有COM端口");
        //        return false;
        //    }
        //    ctrl.Properties.Items.AddRange(list);
        //    if (ctrl.Properties.Items.Count == 0)
        //    {
        //        MessageBox.Show("没有COM端口");
        //        return false;
        //    }
        //    if (string.IsNullOrEmpty(defaultComName))
        //    {
        //        ctrl.SelectedIndex = 0;
        //        return true;
        //    }
        //    for (int i = 0; i < list.Length; i++)
        //    {
        //        if (list[i] == defaultComName)
        //        {
        //            ctrl.SelectedIndex = i;
        //            return true;
        //        }
        //    }
        //    ctrl.SelectedIndex = 0;
        //    return true;
        //}
  
        public static string[] GetPortNames()
        {
            return System.IO.Ports.SerialPort.GetPortNames();
        }
 
 
    }
}