-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUartConfig.cs
104 lines (99 loc) · 3.51 KB
/
UartConfig.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Common
{
/// <summary>
/// uart form config
/// </summary>
public partial class UartConfig: Form
{
private SerialPort sp;
public UartConfig(SerialPort sp)
{
this.sp = sp;
InitializeComponent();
label_Msg.Text = "";
UpdatePortList();
cobBox_BaudRate.Text = sp.BaudRate.ToString();
cobBox_ParityBit.SelectedIndex = (int)sp.Parity;
cobBox_DataBit.SelectedIndex = sp.DataBits - 5; /* 5-8 */
if(sp.StopBits == StopBits.None)
cobBox_StopBit.SelectedIndex = (int)sp.StopBits;
else cobBox_StopBit.SelectedIndex = (int)sp.StopBits - 1;
if(sp.IsOpen == true) { groupBox1.Enabled = false; btn_Open.Text = "Close Port"; }
else { groupBox1.Enabled = true; btn_Open.Text = "Open Port"; }
}
private void UpdatePortList()
{
string[] portList = SerialPort.GetPortNames(); //获取所有的串口名字
cobBox_Name.Items.Clear();//清空链表内容
if(portList.Length > 0)
{
cobBox_Name.Items.AddRange(portList);
int i = 0;
foreach(string str in portList)
{
if(sp.PortName == str) { cobBox_Name.SelectedIndex = i; return; }
i++;
}
cobBox_Name.SelectedIndex = 0;
}
else
{
cobBox_Name.Items.Add("port not found");
cobBox_Name.SelectedIndex = 0;
}
}
private void bnt_OK_Click(object sender, EventArgs e)
{
if(sp.IsOpen == false)
{
if(cobBox_Name.Text != null)
if(cobBox_Name.Text != "")
sp.PortName = cobBox_Name.Text;
sp.BaudRate = int.Parse(cobBox_BaudRate.Text);
sp.Parity = (Parity)cobBox_ParityBit.SelectedIndex;
sp.DataBits = cobBox_DataBit.SelectedIndex + 5;
sp.StopBits = (StopBits)cobBox_StopBit.SelectedIndex + 1;
}
this.Close();
}
private void btn_Open_Click(object sender, EventArgs e)
{
if(sp.IsOpen == true)
{
sp.Close();
}
else
{
try {
sp.PortName = cobBox_Name.Text;
sp.BaudRate = int.Parse(cobBox_BaudRate.Text);
sp.Parity = (Parity)cobBox_ParityBit.SelectedIndex;
sp.DataBits = cobBox_DataBit.SelectedIndex + 5;
sp.StopBits = (StopBits)cobBox_StopBit.SelectedIndex + 1;
sp.Open();
label_Msg.Text = "";
}
catch(Exception ex)
{
label_Msg.Text = ex.Message;
label_Msg.ForeColor = Color.Red;
}
}
if(sp.IsOpen == true) { groupBox1.Enabled = false; btn_Open.Text = "Close Port"; }
else { groupBox1.Enabled = true; btn_Open.Text = "Open Port"; }
}
private void cobBox_Name_DropDown(object sender, EventArgs e)
{
UpdatePortList();
}
}
}