CSoft.TopPortLib 11.0.3

dotnet add package CSoft.TopPortLib --version 11.0.3
                    
NuGet\Install-Package CSoft.TopPortLib -Version 11.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CSoft.TopPortLib" Version="11.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSoft.TopPortLib" Version="11.0.3" />
                    
Directory.Packages.props
<PackageReference Include="CSoft.TopPortLib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSoft.TopPortLib --version 11.0.3
                    
#r "nuget: CSoft.TopPortLib, 11.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CSoft.TopPortLib@11.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSoft.TopPortLib&version=11.0.3
                    
Install as a Cake Addin
#tool nuget:?package=CSoft.TopPortLib&version=11.0.3
                    
Install as a Cake Tool

CSoft.TopPortLib

把物理通信口与分帧器组装成应用可直接使用的端口。端口提供收发事件和连接生命周期,并可按场景选用 Crow、Pigeon 或 Sparrow 请求队列。

安装

dotnet add package CSoft.TopPortLib --version 11.0.3

提供 .NET 8、9、10、11 专用目标,另通过 .NET Standard 2.0 兼容 .NET Framework 4.8。

11.0.3 同时依赖 CSoft.CommTask、CSoft.Communication 和 CSoft.Parser 11.0.3。需要从源码生成依赖包时,参见本地打包与还原。

选择端口

端口 适用场景
TopPort 点对点分帧收发,例如串口、TCP 客户端和命名管道客户端。
TopPort_Server、TopPort_M2M 直接与多个对端收发,例如 TCP 服务端和 UDP 多对多。
CrowPort、CrowPort_Server 串行请求响应。服务端版本绑定一个客户端。
PigeonPort、PigeonPort_Server 双工请求响应和设备主动推送。
SparrowPort 按客户端标识发起 UDP 多对多请求,接收响应或主动推送。

Pigeon 请求取消与响应筛选

PigeonPort 的单响应请求可传入 CancellationToken。已有协议可沿用请求和响应类型,仅在需要时使用新重载。

var response = await port.RequestAsync<MyReq, MyRsp>(request, 3000, cancellationToken);
var filtered = await port.RequestAsync<MyReq, MyRsp>(
    request, 3000, cancellationToken, acceptResponse: rsp => rsp.GetResult().IsComplete);

筛选函数在响应类型与匹配字节一致后执行。返回 false 时继续等待匹配的响应。单向发送可调用 PigeonPort.SendAsync(request, timeout, cancellationToken) 或 PigeonPort_Server.SendAsync(clientId, request, timeout, cancellationToken)。

  • 新重载定义在具体端口类型上,IPigeonPort 和 IPigeonPort_Server 原有签名保留。
  • 超时以毫秒计。取消等待后会清理请求配对,已交给底层端口的写入仍可能完成。
  • 客户端旧版单向发送重载只触发底层端口的发送通知。带取消令牌的重载在底层写入成功后调用 Pigeon OnSentData,异步回调的等待计入超时和取消。
  • 停止端口后可再次启动。重新启动时会忽略上一轮运行中尚未完成解析的响应。
  • 旧版客户端单向发送的调用方应处理本次修复后正常传播的底层发送异常。

TCP 分帧收发

在 .NET 8 至 11 的控制台应用中运行以下示例。启动一个监听 127.0.0.1:2756 的 TCP 服务,收到 PING\r 后返回以 \r 结尾的应答。FootParser 会在收到终止字节后交付完整帧。

using System;
using System.Text;
using System.Threading.Tasks;
using Communication.Bus.PhysicalPort;
using Parser.Parsers;
using TopPortLib;

await using var port = new TopPort(
    new TcpClient("127.0.0.1", 2756),
    new FootParser(new byte[] { 0x0D }));

var response = new TaskCompletionSource<byte[]>(TaskCreationOptions.RunContinuationsAsynchronously);
port.OnReceiveParsedData += frame =>
{
    response.TrySetResult(frame);
    return Task.CompletedTask;
};

await port.OpenAsync();
await port.SendAsync(Encoding.ASCII.GetBytes("PING\r"));
var received = await response.Task.WaitAsync(TimeSpan.FromSeconds(2));
Console.WriteLine(Encoding.ASCII.GetString(received).TrimEnd('\r'));

自动生成协议接口

源码生成器随 CSoft.TopPortLib 自动加载,需要 Roslyn 4.8 或更高版本。使用生成的协议 Builder 时,同时引用 CSoft.Protocol 11.0.3。

  1. 在 partial 协议类上标记 [AutoCrowPortBuilder]、[AutoPigeonPortBuilder] 或 [AutoSparrowPortBuilder],提供 public static IParser CreateParser()。
  2. 为业务命令定义同一前缀的 XxxReq 和 XxxRsp。Crow 请求实现 IByteStream,响应提供 Result 属性。Pigeon 和 Sparrow 的请求实现 IAsyncRequest,其响应实现 IAsyncResponse<T>。
  3. 在应用中调用生成的 Builder 和业务方法。带 [ActivelyPushResponse] 的 Pigeon 或 Sparrow 响应可生成主动推送事件。

Crow 协议示例 · Pigeon 协议示例 · Sparrow 协议示例

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on CSoft.TopPortLib:

Package Downloads
CSoft.Communication.Bluetooth

Package Description

FTIRSpectrometerCore

基于设备私有网络应用层通信协议开发为傅里叶红外光谱产品服务的配套模块库

CSoft.Communication.Builder

通信库 Fluent Builder API - 简化通信口创建

CSoft.Communication.BLE

Package Description

CSoft.MAUI.FT231X

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.3 68 11/13/2026
11.0.2 66 11/13/2026
11.0.1 87 11/12/2026
11.0.0 72 11/12/2026
10.6.2 2,014 5/28/2026
10.6.1 321 5/28/2026
10.6.0 464 5/27/2026
10.5.1 388 5/25/2026
10.5.0 413 5/25/2026
10.4.10 520 5/19/2026
10.4.9 406 5/19/2026
10.4.8 272 5/18/2026
10.4.7 282 5/18/2026
10.4.6 324 5/15/2026
10.4.5 325 5/14/2026
10.4.3 281 5/14/2026
10.4.1 365 5/11/2026
10.4.0 291 5/11/2026 10.4.0 is deprecated because it has critical bugs.
10.3.1 276 5/7/2026
10.3.0 318 5/7/2026
Loading failed

依赖同版本的 Communication、CommTask 和 Parser,提供 Pigeon 可取消的请求与发送接口。