1. 重新设计了Generate项目及相关特性的命名,避免与其他类型混淆。

2. 补充了部分注释。
3. 修改了删除容器节点时,容器内子节点未正确删除的问题。
This commit is contained in:
fengjiayi
2025-07-30 21:15:07 +08:00
parent 93148b11a5
commit 152077e9b5
188 changed files with 2713 additions and 1406 deletions

View File

@@ -10,12 +10,12 @@
/// <summary>
/// PDU (Protocol Data Unit) 数据不包括从站地址和CRC
/// </summary>
public byte[] PDU { get; set; }
public byte[]? PDU { get; set; }
/// <summary>
/// 异步任务完成源,用于等待响应
/// </summary>
public TaskCompletionSource<byte[]> Completion { get; set; }
public TaskCompletionSource<byte[]>? Completion { get; set; }
}
}

View File

@@ -24,7 +24,10 @@ namespace Serein.Proto.Modbus
private readonly CancellationTokenSource _cts = new();
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
public ModbusRtuClient(string portName, int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One, byte slaveId = 1)
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
{
_slaveId = slaveId;
_serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits)

View File

@@ -42,7 +42,9 @@ namespace Serein.Proto.Modbus
/// </summary>
private int _transactionId = 0;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
public ModbusTcpClient(string host, int port = 502)
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
{
_tcpClient = new TcpClient();
_tcpClient.Connect(host, port);
@@ -271,6 +273,11 @@ namespace Serein.Proto.Modbus
while (_tcpClient.Connected)
{
var request = await _channel.Reader.ReadAsync();
if (request.PDU is null)
{
request.Completion?.TrySetCanceled();
continue;
}
byte[] packet = BuildPacket(request.TransactionId, 0x01, (byte)request.FunctionCode, request.PDU);
OnTx?.Invoke(packet); // 触发发送日志
await _stream.WriteAsync(packet, 0, packet.Length);

View File

@@ -24,7 +24,9 @@ namespace Serein.Proto.Modbus
private readonly ConcurrentDictionary<ushort, TaskCompletionSource<byte[]>> _pendingRequests = new();
private int _transactionId = 0;
#pragma warning disable CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
public ModbusUdpClient(string host, int port = 502)
#pragma warning restore CS8618 // 在退出构造函数时,不可为 null 的字段必须包含非 null 值。请考虑添加 "required" 修饰符或声明为可为 null。
{
_remoteEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
_udpClient = new UdpClient();
@@ -153,11 +155,20 @@ namespace Serein.Proto.Modbus
return tcs.Task;
}
/// <summary>
/// 处理发送队列的异步方法
/// </summary>
/// <returns></returns>
private async Task ProcessQueueAsync()
{
while (true)
{
var request = await _channel.Reader.ReadAsync();
if(request.PDU is null)
{
request.Completion?.TrySetCanceled();
continue;
}
byte[] packet = BuildPacket(request.TransactionId, 0x01, (byte)request.FunctionCode, request.PDU);
OnTx?.Invoke(packet);
await _udpClient.SendAsync(packet, packet.Length);