命令树优化,测试编辑区域优化
This commit is contained in:
@@ -7,11 +7,21 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DeviceCommand.Flexible
|
||||
{
|
||||
/// <summary>
|
||||
/// 灵活型串口通信类(免实例化,每次调用临时创建串口连接)。
|
||||
/// 支持只发送指令、发送并读取应答两种最常用操作,
|
||||
/// 内部使用通信锁保证同一时刻只有一个串口事务在执行,
|
||||
/// 适用于偶发性、无需保持长连接的串口设备通信场景(如示波器、电源的 SCPI 指令)。
|
||||
/// </summary>
|
||||
[ACPCommand]
|
||||
public static class FSerialPort
|
||||
{
|
||||
// 通信锁:保证同一时刻只有一个串口事务在执行
|
||||
private static readonly SemaphoreSlim _commLock = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// 创建串口实例并配置 UTF8 编码与超时参数。
|
||||
/// </summary>
|
||||
private static SerialPort CreatePort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits, int sendTimeout,int receiveTimeout)
|
||||
{
|
||||
return new SerialPort(portName, baudRate, parity, dataBits, stopBits)
|
||||
@@ -24,7 +34,19 @@ namespace DeviceCommand.Flexible
|
||||
|
||||
#region 最常用:只发送字符串
|
||||
|
||||
public static async Task SendAsync(string portName,int baudRate,Parity parity,int dataBits,StopBits stopBits,int sendTimeout,int receiveTimeout,string command,CancellationToken ct = default)
|
||||
/// <summary>
|
||||
/// 向串口发送一条字符串指令(只发送,不等待应答)。
|
||||
/// </summary>
|
||||
/// <param name="portName">串口名称,如 "COM1"</param>
|
||||
/// <param name="baudRate">波特率,如 9600、115200</param>
|
||||
/// <param name="parity">校验位</param>
|
||||
/// <param name="dataBits">数据位,常用 8</param>
|
||||
/// <param name="stopBits">停止位</param>
|
||||
/// <param name="sendTimeout">发送超时时间(毫秒)</param>
|
||||
/// <param name="receiveTimeout">接收超时时间(毫秒)</param>
|
||||
/// <param name="command">要发送的指令字符串</param>
|
||||
/// <param name="ct">异步取消令牌</param>
|
||||
public static async Task 发送指令(string portName,int baudRate,Parity parity,int dataBits,StopBits stopBits,int sendTimeout,int receiveTimeout,string command,CancellationToken ct = default)
|
||||
{
|
||||
await _commLock.WaitAsync(ct);
|
||||
try
|
||||
@@ -52,7 +74,21 @@ namespace DeviceCommand.Flexible
|
||||
|
||||
#region 最常用:发送字符串并读取字符串
|
||||
|
||||
public static async Task<string> SendReadAsync(string portName,int baudRate, Parity parity, int dataBits, StopBits stopBits,int sendTimeout, int receiveTimeout,string command,string delimiter = "\n", CancellationToken ct = default)
|
||||
/// <summary>
|
||||
/// 向串口发送一条字符串指令并读取应答,读取到结束符为止(应答不含结束符)。
|
||||
/// </summary>
|
||||
/// <param name="portName">串口名称,如 "COM1"</param>
|
||||
/// <param name="baudRate">波特率,如 9600、115200</param>
|
||||
/// <param name="parity">校验位</param>
|
||||
/// <param name="dataBits">数据位,常用 8</param>
|
||||
/// <param name="stopBits">停止位</param>
|
||||
/// <param name="sendTimeout">发送超时时间(毫秒)</param>
|
||||
/// <param name="receiveTimeout">接收超时时间(毫秒),超时未收到结束符抛出超时异常</param>
|
||||
/// <param name="command">要发送的指令字符串</param>
|
||||
/// <param name="delimiter">应答结束符,默认换行符 "\n"</param>
|
||||
/// <param name="ct">异步取消令牌</param>
|
||||
/// <returns>去除结束符并去除首尾空白后的应答字符串</returns>
|
||||
public static async Task<string> 发送并读取应答(string portName,int baudRate, Parity parity, int dataBits, StopBits stopBits,int sendTimeout, int receiveTimeout,string command,string delimiter = "\n", CancellationToken ct = default)
|
||||
{
|
||||
await _commLock.WaitAsync(ct);
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user