CSoft.Communication 11.0.3

dotnet add package CSoft.Communication --version 11.0.3
                    
NuGet\Install-Package CSoft.Communication -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.Communication" Version="11.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSoft.Communication" Version="11.0.3" />
                    
Directory.Packages.props
<PackageReference Include="CSoft.Communication" />
                    
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.Communication --version 11.0.3
                    
#r "nuget: CSoft.Communication, 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.Communication@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.Communication&version=11.0.3
                    
Install as a Cake Addin
#tool nuget:?package=CSoft.Communication&version=11.0.3
                    
Install as a Cake Tool

CSoft.Communication

提供串口、TCP、UDP 和本机命名管道的字节级通信接口。点对点端口通过 PipeReader 接收数据,服务端按客户端 ID 发送数据。

安装

dotnet add package CSoft.Communication --version 11.0.3

包含 .NET 8、.NET 9、.NET 10、.NET 11 和 .NET Standard 2.0 目标。.NET Framework 4.8 使用 .NET Standard 2.0 资产。串口收发需要可用的系统串口和驱动。

选择端口

场景 类型 主要用法
串口、TCP 客户端、命名管道客户端 SerialPort、TcpClient、NamedPipeClient 调用 OpenAsync(),通过 SendDataAsync(data, cancellationToken) 发送,从 PipeReader 读取。
TCP 或命名管道服务端 TcpServer、NamedPipeServer 调用 StartAsync(),在 OnClientConnect 中取得客户端 ID 和读取器,通过 SendDataAsync(clientId, data) 回复。
UDP 多端点通信 Udp 调用 StartAsync() 后,按客户端 ID 或远端地址和端口发送。
管理点对点连接 BusPort 包装一个 IPhysicalPort,处理连接事件、断线重连和串行发送。OpenAsync(reconnectAfterInitialFailure: true) 允许首次连接失败后继续在后台重试。

本机命名管道示例

将以下代码放入 .NET 8 或更新版本的控制台项目。服务端在连接建立后发送一个字节,客户端读取并输出 42。

using System;
using System.Threading;
using Communication.Bus;
using Communication.Bus.PhysicalPort;

var pipeName = $"csoft-example-{Guid.NewGuid():N}";
await using var server = new NamedPipeServer(pipeName);
server.OnClientConnect += (clientId, _) =>
    server.SendDataAsync(clientId, new byte[] { 0x42 });
await server.StartAsync();

await using var client = new NamedPipeClient(pipeName);
await client.OpenAsync();

using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
var reader = client.PipeReader;
var result = await reader.ReadAsync(timeout.Token);
try
{
    if (result.Buffer.IsEmpty)
        throw new InvalidOperationException("没有读到示例字节。");

    Console.WriteLine(result.Buffer.First.Span[0].ToString("X2"));
}
finally
{
    reader.AdvanceTo(result.Buffer.End);
}

读取与关闭

  • 同一 PipeReader 由一个读取循环管理。每次读取后调用 AdvanceTo,并在推进后停止访问本次返回的缓冲区。
  • 串口、TCP 和命名管道是字节流,一次读取可能包含半帧或多帧。需要按协议边界拆分时,可使用 CSoft.Parser。
  • 服务端会等待 OnClientConnect 回调返回。需要持续读取时,在回调中启动并管理每个客户端的读取任务,让回调尽快返回。
  • 客户端用 CloseAsync() 关闭,服务端用 StopAsync() 停止。也可用 await using 让端口在离开作用域时异步释放。
  • BusPort 在连接关闭时取消仍在等待发送锁的请求。11.0.2 修复了排队请求取消时误释放发送锁的问题。
  • 关闭会取消当前连接并等待物理口关闭。后台断开通知独立执行,旧连接的发送失败只取消对应的连接。

需要组合端口、解析器和请求响应队列时,可使用 CSoft.TopPortLib。

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 (1)

Showing the top 1 NuGet packages that depend on CSoft.Communication:

Package Downloads
CSoft.TopPortLib

通信口使用类库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.3 95 11/13/2026
11.0.2 83 11/13/2026
11.0.1 110 11/12/2026
11.0.0 100 11/12/2026
10.6.2 2,067 5/28/2026
10.6.1 342 5/28/2026
10.6.0 495 5/27/2026
10.5.1 429 5/25/2026
10.5.0 432 5/25/2026
10.4.10 525 5/19/2026
10.4.9 445 5/19/2026
10.4.8 304 5/18/2026
10.4.7 304 5/18/2026
10.4.6 356 5/15/2026
10.4.5 349 5/14/2026
10.4.3 307 5/14/2026
10.4.1 397 5/11/2026
10.4.0 334 5/11/2026 10.4.0 is deprecated because it has critical bugs.
10.3.1 312 5/7/2026
10.3.0 344 5/7/2026
Loading failed

同步到 11.0.3,依赖 CSoft.Parser 11.0.3。包含端口关闭和重连时的写入修复。