CTea.SocketLibrary 1.0.0

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

SocketLibrary

SocketLibrary 是一个基于 .NET 的轻量级通信组件,为 串口 (SerialPort)、TCP、UDP 以及裸 Socket 等多种传输介质提供统一、易用的读写接口。它将报文封装、CRC 校验、同步 / 异步调用等细节屏蔽在内部,开发者仅需关注业务逻辑即可。

当前仓库同时包含四套协议族的实现:CSocketESocketNSocketUSocket,使用方式保持高度一致,只需替换命名空间即可。


特性亮点

  • 🛠 多协议支持 – C、E、N、U 四种 Socket 协议家族一次性打包。
  • 🔌 多传输通道 – TCP、UDP、原生 Socket 以及串口 (RTU) 开箱即用。
  • 同步 / 异步 API – 根据业务场景自由切换,提升吞吐与易用性。
  • 🧩 高度模块化 – Factory + Adapter 设计,轻松拓展新的传输层或日志组件。
  • 📦 零依赖引入 – 仅依赖 .NET 标准库,体积小巧,便于嵌入。
  • 📝 完整接口契约 – 通过 SocketLibrary.Interfaces 统一约束行为,调用方与实现方解耦。

安装方式

  1. 源码引用(推荐)

    git clone https://github.com/your-org/SocketLibrary.git
    

    在解决方案中直接引用 SocketLibrary.csproj

  2. NuGet(TODO) 未来将发布至 NuGet,敬请关注。


快速上手

以下示例以 CSocket 为例,其余协议族更换命名空间即可:ESocket.Device.SocketFactoryNSocket.Device.SocketFactoryUSocket.Device.SocketFactory

1. 基于 TCP 的主站示例

using CSocket.Device;
using System.Net.Sockets;

var client  = new TcpClient();
await client.ConnectAsync("192.168.1.10", 502);

var factory = new SocketFactory();
var master  = factory.CreateMaster(client);

// ----- 读操作 -----
ushort[] requestData = [ /* 填写请求数据 */ ];
int result = master.Read(0x01,        // 设备地址
                         0x10,        // 功能码 / 命令码
                         requestData, // 请求数据
                         out ushort[] respondData);

// ----- 写操作 -----
master.Write(0x01, 0x10, new ushort[] { 0x1234, 0xABCD });

2. 基于串口 (RTU) 的主站示例

using CSocket.Device;
using System.IO.Ports;

var serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
serialPort.Open();

var factory = new SocketFactory();
var master  = factory.CreateMaster(serialPort);

// 异步写入
await master.WriteAsync(0x01, 0x10, new byte[] { 0x01, 0x02, 0x03 });

更多 API 详情请查阅源代码中 *Master*Transport*Message 等类。


目录结构

SocketLibrary/
├─ CSocket/   # C 协议族实现 (Device / IO / Message / Extensions)
├─ ESocket/   # E 协议族实现
├─ NSocket/   # N 协议族实现
├─ USocket/   # U 协议族实现
├─ Interfaces/    # 公共接口定义
├─ Extensions/    # 通用扩展方法,如 CRC、日志
├─ Utility/       # 工具类
└─ Libraries/     # 面向业务的聚合封装

设计概览

  1. Factory Pattern – 通过 ISocketFactory 创建 ISocketMasterISocketTransport,在保持统一 API 的同时屏蔽具体实现。
  2. Adapter PatternTcpClientAdapterUdpClientAdapterSerialPortAdapter 将不同物理通道抽象为统一的 IStreamResource
  3. Layered TransportSocketIpTransportSocketSerialTransport 等对上层负责报文拆装,对下层负责流控制。

贡献指南

  1. Fork & Clone 本仓库。
  2. 使用最新 LTS 版本的 .NET SDK 构建。
  3. 提交 PR 前请确保 dotnet test 通过并遵循 Conventional Commits 规范。

License

Distributed under the MIT License. See LICENSE for more information.

Product Compatible and additional computed target framework versions.
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 188 6/18/2025