Skip to content

Commit c80b64c

Browse files
committed
refactor: 重命名类
1 parent 8d78873 commit c80b64c

File tree

10 files changed

+120
-20
lines changed

10 files changed

+120
-20
lines changed

src/BootstrapBlazor/Services/Serial/ISerialPort.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface ISerialPort : IAsyncDisposable
2424
/// 打开端口方法
2525
/// </summary>
2626
/// <returns></returns>
27-
Task Open(SerialOptions options, CancellationToken token = default);
27+
Task Open(SerialPortOptions options, CancellationToken token = default);
2828

2929
/// <summary>
3030
/// 接收数据回调方法
@@ -37,4 +37,26 @@ public interface ISerialPort : IAsyncDisposable
3737
/// </summary>
3838
/// <returns></returns>
3939
Task<bool> Write(byte[] data, CancellationToken token = default);
40+
41+
/// <summary>
42+
/// 获得 Usb 设备信息
43+
/// </summary>
44+
/// <param name="token"></param>
45+
/// <returns></returns>
46+
Task<SerialPortUsbInfo?> GetUsbInfo(CancellationToken token = default);
47+
48+
/// <summary>
49+
/// 获得设备参数
50+
/// </summary>
51+
/// <param name="token"></param>
52+
/// <returns></returns>
53+
Task<SerialPortSignals?> GetSignals(CancellationToken token = default);
54+
55+
/// <summary>
56+
/// 设置设备参数
57+
/// </summary>
58+
/// <param name="options"></param>
59+
/// <param name="token"></param>
60+
/// <returns></returns>
61+
Task<bool> SetSignals(SerialPortSignalsOptions options, CancellationToken token = default);
4062
}

src/BootstrapBlazor/Services/Serial/SerialPort.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public async Task<bool> Write(byte[] data, CancellationToken token = default)
3737
/// <inheritdoc/>
3838
/// </summary>
3939
/// <returns></returns>
40-
public async Task Open(SerialOptions options, CancellationToken token = default)
40+
public async Task Open(SerialPortOptions options, CancellationToken token = default)
4141
{
4242
DotNetObjectReference<SerialPort>? interop = null;
4343
if (DataReceive != null)
@@ -74,6 +74,29 @@ public async Task DataReceiveCallback(byte[] data)
7474
}
7575
}
7676

77+
/// <summary>
78+
/// <inheritdoc/>
79+
/// </summary>
80+
/// <param name="token"></param>
81+
/// <returns></returns>
82+
public async Task<SerialPortUsbInfo?> GetUsbInfo(CancellationToken token = default) => await jsModule.InvokeAsync<SerialPortUsbInfo>("getInfo", token, serialPortId);
83+
84+
/// <summary>
85+
/// <inheritdoc/>
86+
/// </summary>
87+
/// <param name="token"></param>
88+
/// <returns></returns>
89+
public async Task<SerialPortSignals?> GetSignals(CancellationToken token = default) => await jsModule.InvokeAsync<SerialPortSignals>("getSignals", token, serialPortId);
90+
91+
/// <summary>
92+
/// <inheritdoc/>
93+
/// </summary>
94+
/// <param name="options"></param>
95+
/// <param name="token"></param>
96+
/// <returns></returns>
97+
/// <exception cref="NotImplementedException"></exception>
98+
public async Task<bool> SetSignals(SerialPortSignalsOptions options, CancellationToken token = default) => await jsModule.InvokeAsync<bool>("setSignals", token, serialPortId, options);
99+
77100
private async ValueTask DisposeAsync(bool disposing)
78101
{
79102
if (disposing)

src/BootstrapBlazor/Services/Serial/SerialFlowControlType.cs renamed to src/BootstrapBlazor/Services/Serial/SerialPortFlowControlType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BootstrapBlazor.Components;
1010
/// 流量控制方法
1111
/// </summary>
1212
[JsonEnumConverter(true)]
13-
public enum SerialFlowControlType
13+
public enum SerialPortFlowControlType
1414
{
1515
/// <summary>
1616
/// 未启用流量控制

src/BootstrapBlazor/Services/Serial/SerialOptions.cs renamed to src/BootstrapBlazor/Services/Serial/SerialPortOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace BootstrapBlazor.Components;
99
/// <summary>
1010
/// 串口通讯参数
1111
/// </summary>
12-
public class SerialOptions
12+
public class SerialPortOptions
1313
{
1414
/// <summary>
1515
/// 波特率 默认 9600
@@ -30,7 +30,7 @@ public class SerialOptions
3030
/// 校验位 none、even、odd 默认 "none"
3131
/// </summary>
3232
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
33-
public SerialParityType ParityType { get; set; }
33+
public SerialPortParityType ParityType { get; set; }
3434

3535
/// <summary>
3636
/// 读写缓冲区 默认 255
@@ -41,5 +41,5 @@ public class SerialOptions
4141
/// 流控制 "none"或"hardware" 默认值为"none"
4242
/// </summary>
4343
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
44-
public SerialFlowControlType FlowControlType { get; set; }
44+
public SerialPortFlowControlType FlowControlType { get; set; }
4545
}

src/BootstrapBlazor/Services/Serial/SerialParityType.cs renamed to src/BootstrapBlazor/Services/Serial/SerialPortParityType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BootstrapBlazor.Components;
1010
/// 校验位枚举
1111
/// </summary>
1212
[JsonEnumConverter(true)]
13-
public enum SerialParityType
13+
public enum SerialPortParityType
1414
{
1515
/// <summary>
1616
/// 每个数据字不发送奇偶校验位

src/BootstrapBlazor/Services/Serial/SerialSignals.cs renamed to src/BootstrapBlazor/Services/Serial/SerialPortSignals.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using System.Text.Json.Serialization;
6+
57
namespace BootstrapBlazor.Components;
68

79
//RS-232C接口定义(DB9)
@@ -19,29 +21,33 @@ namespace BootstrapBlazor.Components;
1921
/// <summary>
2022
/// 串口信号
2123
/// </summary>
22-
public class SerialSignals
24+
public class SerialPortSignals
2325
{
2426
/// <summary>
2527
/// 振铃提示 RI(Ring Indicator)
2628
/// 如果 RI 为 true,则表示已检测到振铃。如果 RI 为 false,则表示未检测到振铃。Pin 9
27-
/// </summary>
29+
/// </summary>
30+
[JsonPropertyName("ringIndicator")]
2831
public bool RING { get; set; }
2932

3033
/// <summary>
3134
/// 数据准备就绪 DSR(Data Set Ready)
3235
/// 如果 DSR 为 true,则表示已准备好接收数据。如果 DSR 为 false,则表示未准备好接收数据。Pin 6
3336
/// </summary>
37+
[JsonPropertyName("dataSetReady")]
3438
public bool DSR { get; set; }
3539

3640
/// <summary>
3741
/// 清除发送 CTS(Clear To Send)
3842
/// 如果 CTS 为 true,则表示已准备好发送数据。如果 CTS 为 false,则表示未准备好发送数据。Pin 8
3943
/// </summary>
44+
[JsonPropertyName("clearToSend")]
4045
public bool CTS { get; set; }
4146

4247
/// <summary>
4348
/// 载波检测 DCD(Data Carrier Detect)
4449
/// 如果 DCD 为 true,则表示已检测到载波。如果 DCD 为 false,则表示未检测到载波。Pin 1
4550
/// </summary>
51+
[JsonPropertyName("dataCarrierDetect")]
4652
public bool DCD { get; set; }
4753
}

src/BootstrapBlazor/Services/Serial/SerialSignalsSetting.cs renamed to src/BootstrapBlazor/Services/Serial/SerialPortSignalsOptions.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,26 @@ namespace BootstrapBlazor.Components;
99
/// <summary>
1010
/// 串口信号设置
1111
/// </summary>
12-
public class SerialSignalsSetting
12+
public class SerialPortSignalsOptions
1313
{
1414
/// <summary>
1515
/// 中断
1616
/// <para></para>如果 Break 为 true,则表示已中断。如果 Break 为 false,则表示未中断。
1717
/// </summary>
18-
public bool? Break { get; set; }
18+
[JsonPropertyName("break")]
19+
public bool Break { get; set; }
1920

2021
/// <summary>
2122
/// 数据终端准备就绪 DTR(Data Terminal Ready)
2223
/// <para></para>如果 DTR 为 true,则表示已准备好接收数据。如果 DTR 为 false,则表示未准备好接收数据。Pin 4
2324
/// </summary>
24-
[JsonPropertyName("DTR")]
25-
public bool? DTR { get; set; }
25+
[JsonPropertyName("dataTerminalReady")]
26+
public bool DTR { get; set; }
2627

2728
/// <summary>
2829
/// 请求发送 RTS(Request To Send)
2930
/// <para></para>如果 RTS 为 true,则表示已准备好发送数据。如果 RTS 为 false,则表示未准备好发送数据。Pin 7
3031
/// </summary>
31-
[JsonPropertyName("RTS")]
32-
public bool? RTS { get; set; }
32+
[JsonPropertyName("requestToSend")]
33+
public bool RTS { get; set; }
3334
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// Usb 串口设备信息类
9+
/// </summary>
10+
public class SerialPortUsbInfo
11+
{
12+
/// <summary>
13+
/// 厂商 Id
14+
/// </summary>
15+
public string? UsbVendorId { get; internal set; }
16+
17+
/// <summary>
18+
/// 产品 Id
19+
/// </summary>
20+
public string? UsbProductId { get; internal set; }
21+
}

src/BootstrapBlazor/wwwroot/modules/serial.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,33 @@ export async function write(id, data) {
100100
return ret;
101101
}
102102

103+
export async function getInfo(id) {
104+
const serial = Data.get(id);
105+
const { serialPort } = serial;
106+
if (serialPort) {
107+
const info = await serialPort.getInfo();
108+
console.log(info);
109+
}
110+
}
111+
112+
export async function getSignals(id) {
113+
const serial = Data.get(id);
114+
const { serialPort } = serial;
115+
if (serialPort) {
116+
const info = await serialPort.getSignals();
117+
return info;
118+
}
119+
}
120+
121+
export async function setSignals(id, options) {
122+
const serial = Data.get(id);
123+
const { serialPort } = serial;
124+
if (serialPort) {
125+
const info = await serialPort.setSignals(options);
126+
return info;
127+
}
128+
}
129+
103130
export async function dispose(id) {
104131
await close(id);
105132
Data.remove(id);

test/UnitTest/Services/SerialServiceTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ public async Task GetPort_Ok()
2626
return Task.CompletedTask;
2727
};
2828

29-
var option = new SerialOptions()
29+
var option = new SerialPortOptions()
3030
{
3131
BaudRate = 9600,
3232
BufferSize = 1024,
3333
DataBits = 8,
34-
FlowControlType = SerialFlowControlType.Hardware,
35-
ParityType = SerialParityType.Odd,
34+
FlowControlType = SerialPortFlowControlType.Hardware,
35+
ParityType = SerialPortParityType.Odd,
3636
StopBits = 1
3737
};
3838
await serialPort.Open(option);
3939
Assert.True(serialPort.IsOpen);
4040
Assert.Equal(9600, option.BaudRate);
4141
Assert.Equal(1024, option.BufferSize);
4242
Assert.Equal(8, option.DataBits);
43-
Assert.Equal(SerialFlowControlType.Hardware, option.FlowControlType);
44-
Assert.Equal(SerialParityType.Odd, option.ParityType);
43+
Assert.Equal(SerialPortFlowControlType.Hardware, option.FlowControlType);
44+
Assert.Equal(SerialPortParityType.Odd, option.ParityType);
4545
Assert.Equal(1, option.StopBits);
4646

4747
await serialPort.Write([0x31, 0x32]);

0 commit comments

Comments
 (0)