SampleSmart 1.0.0.0
SampleSmart 사용 방법을 보여 주는 예제 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
SerialPortTestView.xaml.Event.cs
이 파일의 문서화 페이지로 가기
1using System.Collections.ObjectModel;
2using System.IO.Ports;
3using Dreamine.Communication.Core.Protocols;
5
7
16public sealed class SerialPortTestViewEvent
17{
27
53 {
54 _runtime = runtime ?? throw new ArgumentNullException(nameof(runtime));
55
56 SerialPortNames = new ObservableCollection<string>();
57
58 BaudRates =
59 [
60 9600,
61 19200,
62 38400,
63 57600,
64 115200
65 ];
66
68 [
69 "PlainText",
70 "RawAvailable",
71 "RawJson"
72 ];
73
74 SelectedBaudRate = 9600;
75 SelectedSerialProtocol = "RawAvailable";
76 SelectedSerialEncoding = PlainTextProtocolOptions.Utf8EncodingName;
77 SerialSendText = "test";
78
80 }
81
90 public ObservableCollection<string> SerialPortNames { get; }
91
100 public IReadOnlyList<int> BaudRates { get; }
101
110 public IReadOnlyList<string> SerialProtocols { get; }
111
120 public IReadOnlyList<string> SerialEncodings => _runtime.TextEncodings;
121
130 public string SelectedSerialPortName { get; set; } = string.Empty;
131
140 public int SelectedBaudRate { get; set; }
141
150 public string SelectedSerialProtocol { get; set; } = string.Empty;
151
160 public string SelectedSerialEncoding { get; set; }
161
170 public string SerialSendText { get; set; } = string.Empty;
171
180 public string SerialSelectionSummary =>
181 $"Port={SelectedSerialPortName}, BaudRate={SelectedBaudRate}, Protocol={SelectedSerialProtocol}, Encoding={SelectedSerialEncoding}";
182
191 public void RefreshPorts()
192 {
193 SerialPortNames.Clear();
194
195 var portNames = SerialPort.GetPortNames()
196 .OrderBy(x => x, StringComparer.OrdinalIgnoreCase)
197 .ToArray();
198
199 if (portNames.Length == 0)
200 {
202 }
203 else
204 {
205 foreach (var portName in portNames)
206 {
207 SerialPortNames.Add(portName);
208 }
209 }
210
211 if (string.IsNullOrWhiteSpace(SelectedSerialPortName) ||
213 {
214 SelectedSerialPortName = SerialPortNames.FirstOrDefault() ?? string.Empty;
215 }
216 }
217
226 public void ConnectSerial()
227 {
228 if (string.IsNullOrWhiteSpace(SelectedSerialPortName))
229 {
230 return;
231 }
232
233 _ = _runtime.ConnectSerialAsync(
238 }
239
248 public void DisconnectSerial()
249 {
250 _ = _runtime.DisconnectSerialAsync();
251 }
252
261 public void SendSerial()
262 {
263 if (string.IsNullOrWhiteSpace(SerialSendText))
264 {
265 SerialSendText = "test";
266 }
267
268 _ = _runtime.SendSerialAsync(
272 }
273
282 private void AddFallbackPorts()
283 {
284 for (var index = 1; index <= 10; index++)
285 {
286 SerialPortNames.Add($"COM{index}");
287 }
288 }
289}