@@ -9,19 +9,14 @@ namespace BootstrapBlazor.Server.Components.Samples;
99/// <summary>
1010/// WebSerials 组件
1111/// </summary>
12- public partial class WebSerials : IDisposable
12+ public partial class WebSerials : IAsyncDisposable
1313{
1414 private string _sendData = "" ;
1515 private int _sendInterval = 1000 ;
1616 private bool _appendCRLF ;
1717 private bool _isHEX ;
1818 private bool _isLoop ;
19- private ConsoleMessageCollection _messages = new ( 8 ) ;
20-
21- private string ? _message ;
22- private string ? _statusMessage ;
23- private string ? _errorMessage ;
24- private readonly WebSerialOptions options = new ( ) { BaudRate = 115200 , AutoGetSignals = true } ;
19+ private readonly ConsoleMessageCollection _messages = new ( 8 ) ;
2520
2621 private readonly List < SelectedItem > _baudRateList =
2722 [
@@ -45,21 +40,15 @@ public partial class WebSerials : IDisposable
4540
4641 private readonly List < SelectedItem > _stopBits = [ new ( "1" , "1" ) , new ( "2" , "2" ) ] ;
4742
48- private bool Flag { get ; set ; }
49-
50- private bool IsConnected { get ; set ; }
51-
52- /// <summary>
53- /// 收到的信号数据
54- /// </summary>
55- public WebSerialSignals Signals { get ; set ; } = new WebSerialSignals ( ) ;
56-
5743 [ Inject , NotNull ]
5844 private ISerialService ? SerialService { get ; set ; }
5945
46+ [ Inject , NotNull ]
47+ private ToastService ? ToastService { get ; set ; }
48+
6049 private ISerialPort ? _serialPort ;
6150
62- private SerialOptions _serialOptions = new ( ) ;
51+ private readonly SerialOptions _serialOptions = new ( ) ;
6352
6453 private bool CheckOpen => _serialPort is not { IsOpen : false } ;
6554
@@ -68,6 +57,10 @@ public partial class WebSerials : IDisposable
6857 private async Task GetPort ( )
6958 {
7059 _serialPort = await SerialService . GetPort ( ) ;
60+ if ( SerialService . IsSupport == false )
61+ {
62+ await ToastService . Error ( Localizer [ "NotSupportSerialTitle" ] , Localizer [ "NotSupportSerialContent" ] ) ;
63+ }
7164 }
7265
7366 private async Task OpenPort ( )
@@ -84,6 +77,11 @@ private async Task OpenPort()
8477 await InvokeAsync ( StateHasChanged ) ;
8578 } ;
8679 await _serialPort . Open ( _serialOptions ) ;
80+
81+ if ( _serialPort . IsOpen == false )
82+ {
83+ await ToastService . Error ( Localizer [ "OpenPortSerialTitle" ] , Localizer [ "OpenPortSerialContent" ] ) ;
84+ }
8785 }
8886 }
8987
@@ -151,60 +149,6 @@ private static byte[] ConvertToHex(string data)
151149 return [ .. ret ] ;
152150 }
153151
154- private Task OnSignals ( WebSerialSignals ? signals )
155- {
156- if ( signals is null ) return Task . CompletedTask ;
157-
158- Signals = signals ;
159-
160- if ( ! options . AutoGetSignals )
161- {
162- // 仅在不自动获取信号时才显示
163- _message = $ "{ DateTime . Now : hh:mm:ss} 收到信号数据: { Environment . NewLine } " +
164- $ "RING: { signals . RING } { Environment . NewLine } " +
165- $ "DSR: { signals . DSR } { Environment . NewLine } " +
166- $ "CTS: { signals . CTS } { Environment . NewLine } " +
167- $ "DCD: { signals . DCD } { Environment . NewLine } " +
168- $ "{ _message } { Environment . NewLine } ";
169- }
170-
171- StateHasChanged ( ) ;
172- return Task . CompletedTask ;
173- }
174-
175- private Task OnConnect ( bool flag )
176- {
177- IsConnected = flag ;
178- if ( flag )
179- {
180- _message = null ;
181- _statusMessage = null ;
182- _errorMessage = null ;
183- }
184- StateHasChanged ( ) ;
185- return Task . CompletedTask ;
186- }
187-
188- private Task OnLog ( string message )
189- {
190- _statusMessage = message ;
191- StateHasChanged ( ) ;
192- return Task . CompletedTask ;
193- }
194-
195- private Task OnError ( string message )
196- {
197- _errorMessage = message ;
198- StateHasChanged ( ) ;
199- return Task . CompletedTask ;
200- }
201-
202- private void OnApply ( )
203- {
204- //options.BaudRate = SelectedBaudRate;
205- //Flag = !Flag;
206- }
207-
208152 /// <summary>
209153 /// 获得属性方法
210154 /// </summary>
@@ -292,151 +236,26 @@ private static AttributeItem[] GetAttributes() =>
292236 }
293237 ] ;
294238
295- /// <summary>s
296- /// 获得WebSerialOptions属性方法
297- /// </summary>
298- /// <returns></returns>
299- private static AttributeItem [ ] GetWebSerialOptionsAttributes ( ) =>
300- [
301- new ( )
302- {
303- Name = "BaudRate" ,
304- Description = "波特率" ,
305- Type = "int" ,
306- ValueList = "-" ,
307- DefaultValue = "9600"
308- } ,
309- new ( )
310- {
311- Name = "DataBits" ,
312- Description = "数据位" ,
313- Type = "int" ,
314- ValueList = "7|8" ,
315- DefaultValue = "8"
316- } ,
317- new ( )
318- {
319- Name = "StopBits" ,
320- Description = "停止位" ,
321- Type = "int" ,
322- ValueList = "1|2" ,
323- DefaultValue = "1"
324- } ,
325- new ( )
326- {
327- Name = "ParityType" ,
328- Description = "流控制" ,
329- Type = "WebSerialFlowControlType" ,
330- ValueList = "none|even|odd" ,
331- DefaultValue = "none"
332- } ,
333- new ( )
334- {
335- Name = "BufferSize" ,
336- Description = "读写缓冲区" ,
337- Type = "int" ,
338- ValueList = "-" ,
339- DefaultValue = "255"
340- } ,
341- new ( )
342- {
343- Name = "FlowControlType" ,
344- Description = "校验" ,
345- Type = "WebSerialParityType" ,
346- ValueList = "none|hardware" ,
347- DefaultValue = "none"
348- } ,
349- new ( )
350- {
351- Name = nameof ( WebSerialOptions . InputWithHex ) ,
352- Description = "HEX发送" ,
353- Type = "bool" ,
354- ValueList = "-" ,
355- DefaultValue = "false"
356- } ,
357- new ( )
358- {
359- Name = nameof ( WebSerialOptions . OutputInHex ) ,
360- Description = "HEX接收" ,
361- Type = "bool" ,
362- ValueList = "-" ,
363- DefaultValue = "false"
364- } ,
365- new ( )
366- {
367- Name = nameof ( WebSerialOptions . AutoConnect ) ,
368- Description = "自动连接设备" ,
369- Type = "bool" ,
370- ValueList = "-" ,
371- DefaultValue = "true"
372- } ,
373- new ( )
374- {
375- Name = nameof ( WebSerialOptions . AutoFrameBreakType ) ,
376- Description = "自动断帧方式" ,
377- Type = "AutoFrameBreakType" ,
378- ValueList = "-" ,
379- DefaultValue = "Character"
380- } ,
381- new ( ) {
382- Name = nameof ( WebSerialOptions . FrameBreakChar ) ,
383- Description = "断帧字符" ,
384- Type = "string" ,
385- ValueList = "-" ,
386- DefaultValue = "\\ n"
387- } ,
388- new ( )
389- {
390- Name = nameof ( WebSerialOptions . ConnectBtnTitle ) ,
391- Description = "获得/设置 连接按钮文本" ,
392- Type = "string" ,
393- ValueList = "" ,
394- DefaultValue = "连接"
395- } ,
396- new ( )
397- {
398- Name = nameof ( WebSerialOptions . DisconnectBtnTitle ) ,
399- Description = "获得/设置 断开连接按钮文本" ,
400- Type = "string" ,
401- ValueList = "" ,
402- DefaultValue = "连接"
403- } ,
404- new ( )
405- {
406- Name = nameof ( WebSerialOptions . WriteBtnTitle ) ,
407- Description = "获得/设置 写入按钮文本" ,
408- Type = "string" ,
409- ValueList = "" ,
410- DefaultValue = "写入"
411- } ,
412- new ( )
413- {
414- Name = nameof ( WebSerialOptions . AutoGetSignals ) ,
415- Description = "获得/设置 自动检查状态" ,
416- Type = "bool" ,
417- ValueList = "-" ,
418- DefaultValue = "false"
419- }
420- ] ;
421-
422- private void Dispose ( bool disposing )
239+ private async ValueTask DisposeAsync ( bool disposing )
423240 {
424241 if ( disposing )
425- {
426242 if ( _loopSendTokenSource != null )
427243 {
428244 _loopSendTokenSource . Cancel ( ) ;
429245 _loopSendTokenSource = null ;
430246 }
247+ if ( _serialPort != null )
248+ {
249+ await _serialPort . DisposeAsync ( ) ;
431250 }
432251 }
433252
434253 /// <summary>
435254 /// <inheritdoc/>
436255 /// </summary>
437- public void Dispose ( )
256+ public async ValueTask DisposeAsync ( )
438257 {
439- Dispose ( true ) ;
258+ await DisposeAsync ( true ) ;
440259 GC . SuppressFinalize ( this ) ;
441260 }
442261}
0 commit comments