-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComControl.xaml.cs
143 lines (123 loc) · 5.36 KB
/
ComControl.xaml.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WellCom
{
/// <summary>
/// ComControl.xaml 的交互逻辑
/// </summary>
public partial class ComControl : UserControl , INotifyPropertyChanged
{
/// <summary>
/// 停止位选项
/// </summary>
public ObservableCollection<KeyValuePair<string, StopBits>> StopBitOptions { get; set; } = new ObservableCollection<KeyValuePair<string, StopBits>>();
/// <summary>
/// 校验位选项
/// </summary>
public ObservableCollection<KeyValuePair<string, Parity>> ParityOptions { get; set; } = new ObservableCollection<KeyValuePair<string, Parity>>();
/// <summary>
/// 数据位选项
/// </summary>
public ObservableCollection<KeyValuePair<string, int>> DataBitsOptions { get; set; } = new ObservableCollection<KeyValuePair<string, int>>();
/// <summary>
/// 波特率选项
/// </summary>
public ObservableCollection<KeyValuePair<int, int>> BaudRateOptions { get; set; } = new ObservableCollection<KeyValuePair<int, int>>();
/// <summary>
/// 串口号选项
/// </summary>
public ObservableCollection<KeyValuePair<string, string>> PortNameOptions { get; set; } = new ObservableCollection<KeyValuePair<string, string>>();
/// <summary>
/// 停止位
/// </summary>
public StopBits StopBits { get; set; } = StopBits.One;
/// <summary>
/// 校验位
/// </summary>
public Parity Parity { get; set; } = Parity.None;
/// <summary>
/// 数据位
/// </summary>
public int DataBits { get; set; } = 8;
/// <summary>
/// 波特率
/// </summary>
public int BaudRate { get; set; } = 9600;
private string _portName;
/// <summary>
/// 串口号
/// </summary>
public string PortName {
get { return _portName; }
set {
_portName = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PortName"));
}
}
public ComControl()
{
InitializeComponent();
Init_Data();
DataContext = this;
}
public event PropertyChangedEventHandler? PropertyChanged;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void Init_Data()
{
//停止位选项
StopBitOptions.Add(new KeyValuePair<string, StopBits>("1", StopBits.One));
StopBitOptions.Add(new KeyValuePair<string, StopBits>("1.5", StopBits.OnePointFive));
StopBitOptions.Add(new KeyValuePair<string, StopBits>("2", StopBits.Two));
//校验位选项
ParityOptions.Add(new KeyValuePair<string, Parity>("None", Parity.None));
ParityOptions.Add(new KeyValuePair<string, Parity>("Odd", Parity.Odd));
ParityOptions.Add(new KeyValuePair<string, Parity>("Even", Parity.Even));
ParityOptions.Add(new KeyValuePair<string, Parity>("Mark", Parity.Mark));
ParityOptions.Add(new KeyValuePair<string, Parity>("Space", Parity.Space));
//数据位选项
DataBitsOptions.Add(new KeyValuePair<string, int>("5", 5));
DataBitsOptions.Add(new KeyValuePair<string, int>("6", 6));
DataBitsOptions.Add(new KeyValuePair<string, int>("7", 7));
DataBitsOptions.Add(new KeyValuePair<string, int>("8", 8));
//波特率
BaudRateOptions.Add(new KeyValuePair<int, int>(110, 110));
BaudRateOptions.Add(new KeyValuePair<int, int>(300, 300));
BaudRateOptions.Add(new KeyValuePair<int, int>(600, 600));
BaudRateOptions.Add(new KeyValuePair<int, int>(1200, 1200));
BaudRateOptions.Add(new KeyValuePair<int, int>(2400, 2400));
BaudRateOptions.Add(new KeyValuePair<int, int>(4800, 4800));
BaudRateOptions.Add(new KeyValuePair<int, int>(9600, 9600));
BaudRateOptions.Add(new KeyValuePair<int, int>(14400, 14400));
BaudRateOptions.Add(new KeyValuePair<int, int>(19200, 19200));
BaudRateOptions.Add(new KeyValuePair<int, int>(38400, 38400));
BaudRateOptions.Add(new KeyValuePair<int, int>(56000, 56000));
BaudRateOptions.Add(new KeyValuePair<int, int>(57600, 57600));
BaudRateOptions.Add(new KeyValuePair<int, int>(115200, 115200));
BaudRateOptions.Add(new KeyValuePair<int, int>(128000, 128000));
BaudRateOptions.Add(new KeyValuePair<int, int>(256000, 256000));
string[] sPorts = SerialPort.GetPortNames().OrderBy(m => m).ToArray();
foreach (var portName in sPorts)
{
PortNameOptions.Add(new KeyValuePair<string, string>(portName, portName));
PortName = portName;
}
}
}
}