SampleSmart
1.0.0.0
SampleSmart 사용 방법을 보여 주는 예제 프로젝트입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
SerialPortTestView.xaml.Event.cs
이 파일의 문서화 페이지로 가기
1
using
System.Collections.ObjectModel;
2
using
System.IO.Ports;
3
using
Dreamine.Communication.Core.Protocols;
4
using
SampleSmart.Pages.PageSub.CommunicationTabs
;
5
6
namespace
SampleSmart.Pages.PageSub.CommunicationTabs.Serial
;
7
16
public
sealed
class
SerialPortTestViewEvent
17
{
26
private
readonly
CommunicationSampleRuntime
_runtime
;
27
52
public
SerialPortTestViewEvent
(
CommunicationSampleRuntime
runtime)
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
67
SerialProtocols
=
68
[
69
"PlainText"
,
70
"RawAvailable"
,
71
"RawJson"
72
];
73
74
SelectedBaudRate
= 9600;
75
SelectedSerialProtocol
=
"RawAvailable"
;
76
SelectedSerialEncoding
= PlainTextProtocolOptions.Utf8EncodingName;
77
SerialSendText
=
"test"
;
78
79
RefreshPorts
();
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
{
201
AddFallbackPorts
();
202
}
203
else
204
{
205
foreach
(var portName
in
portNames)
206
{
207
SerialPortNames
.Add(portName);
208
}
209
}
210
211
if
(
string
.IsNullOrWhiteSpace(
SelectedSerialPortName
) ||
212
!
SerialPortNames
.Contains(
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(
234
SelectedSerialPortName
,
235
SelectedBaudRate
,
236
SelectedSerialProtocol
,
237
SelectedSerialEncoding
);
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(
269
SelectedSerialProtocol
,
270
SerialSendText
,
271
SelectedSerialEncoding
);
272
}
273
282
private
void
AddFallbackPorts
()
283
{
284
for
(var index = 1; index <= 10; index++)
285
{
286
SerialPortNames
.Add($
"COM{index}"
);
287
}
288
}
289
}
SampleSmart.Pages.PageSub.CommunicationTabs
Definition
CommunicationSampleRuntime.cs:21
SampleSmart.Pages.PageSub.CommunicationTabs.Serial
Definition
SerialPortTestView.xaml.cs:18
SampleSmart.Pages.PageSub.CommunicationTabs.CommunicationSampleRuntime
Definition
CommunicationSampleRuntime.cs:40
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.ConnectSerial
void ConnectSerial()
Definition
SerialPortTestView.xaml.Event.cs:226
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SelectedSerialPortName
string SelectedSerialPortName
Definition
SerialPortTestView.xaml.Event.cs:130
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialPortNames
ObservableCollection< string > SerialPortNames
Definition
SerialPortTestView.xaml.Event.cs:90
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialProtocols
IReadOnlyList< string > SerialProtocols
Definition
SerialPortTestView.xaml.Event.cs:110
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialSendText
string SerialSendText
Definition
SerialPortTestView.xaml.Event.cs:170
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SelectedSerialProtocol
string SelectedSerialProtocol
Definition
SerialPortTestView.xaml.Event.cs:150
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SelectedBaudRate
int SelectedBaudRate
Definition
SerialPortTestView.xaml.Event.cs:140
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.RefreshPorts
void RefreshPorts()
Definition
SerialPortTestView.xaml.Event.cs:191
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent._runtime
readonly CommunicationSampleRuntime _runtime
Definition
SerialPortTestView.xaml.Event.cs:26
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.AddFallbackPorts
void AddFallbackPorts()
Definition
SerialPortTestView.xaml.Event.cs:282
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialEncodings
IReadOnlyList< string > SerialEncodings
Definition
SerialPortTestView.xaml.Event.cs:120
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.BaudRates
IReadOnlyList< int > BaudRates
Definition
SerialPortTestView.xaml.Event.cs:100
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SendSerial
void SendSerial()
Definition
SerialPortTestView.xaml.Event.cs:261
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SelectedSerialEncoding
string SelectedSerialEncoding
Definition
SerialPortTestView.xaml.Event.cs:160
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.DisconnectSerial
void DisconnectSerial()
Definition
SerialPortTestView.xaml.Event.cs:248
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialSelectionSummary
string SerialSelectionSummary
Definition
SerialPortTestView.xaml.Event.cs:180
SampleSmart.Pages.PageSub.CommunicationTabs.Serial.SerialPortTestViewEvent.SerialPortTestViewEvent
SerialPortTestViewEvent(CommunicationSampleRuntime runtime)
Definition
SerialPortTestView.xaml.Event.cs:52
Pages
PageSub
CommunicationTabs
Serial
SerialPortTestView.xaml.Event.cs
다음에 의해 생성됨 :
1.17.0