LinkForge.Modbus 0.1.0

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

LinkForge

面向 .NET 的工业通讯组件库,聚焦可独立发布为 NuGet 的传输层与协议层能力,当前已完成 TCP 通道与 Modbus TCP 的首个纵向闭环。

NuGet LinkForge.Abstractions NuGet LinkForge.Transports.Tcp NuGet LinkForge.Modbus .NET 8.0 .NET Standard 2.0 License

📦 NuGet 包

包名 说明 安装命令
LinkForge.Abstractions 通道契约与基础异常模型 dotnet add package LinkForge.Abstractions --version 0.1.0
LinkForge.Transports.Tcp 基于 Socket/TcpClient 的 TCP 通道实现 dotnet add package LinkForge.Transports.Tcp --version 0.1.0
LinkForge.Modbus Modbus TCP 协议封装与常用功能码支持 dotnet add package LinkForge.Modbus --version 0.1.0

🧱 当前结构

  • src/LinkForge.Abstractions 统一通道契约与基础异常模型。
  • src/LinkForge.Transports.Tcp 不依赖第三方库的 TCP 通道实现。
  • src/LinkForge.Modbus 当前提供 Modbus TCP 协议实现与常用功能码支持。
  • samples/LinkForge.Sample.ModbusTcp 最小可运行调用示例。
  • tests 面向传输层与协议层的回归测试工程。

✅ 当前能力

  • LinkForge.Transports.Tcp 提供零第三方依赖的 TCP 通道实现,支持连接、发送、接收、超时控制。
  • LinkForge.Modbus 当前已支持以下 Modbus TCP 功能码:
    • 0x01 读线圈
    • 0x02 读离散输入
    • 0x03 读保持寄存器
    • 0x04 读输入寄存器
    • 0x05 写单个线圈
    • 0x06 写单个寄存器
    • 0x10 写多个寄存器
  • 协议层已包含:
    • MBAP 头校验
    • 事务标识符校验
    • 单元标识符校验
    • 异常响应转换为 ModbusException
    • 响应超时与半包接收处理

🎯 设计原则

  • KISS 优先打通一个完整纵切面,不提前引入串口、重试、日志、连接池等复杂能力。
  • YAGNI 只实现当前明确需要的公共契约、TCP 传输与 Modbus TCP 常用能力。
  • DRY 协议层只依赖 IChannel,后续 UDP、串口、网关通道都可以复用同一套协议处理代码。
  • SOLID 传输层与协议层解耦,协议客户端不直接绑定具体 TCP 实现。

🚀 快速开始

dotnet add package LinkForge.Modbus --version 0.1.0
dotnet add package LinkForge.Transports.Tcp --version 0.1.0
using LinkForge.Modbus;
using LinkForge.Transports.Tcp;

using TcpChannel channel = new TcpChannel(new TcpChannelOptions
{
    Host = "192.168.0.10",
    Port = 502
});

using ModbusTcpClient client = new ModbusTcpClient(channel, new ModbusTcpClientOptions
{
    UnitIdentifier = 1
});

await client.ConnectAsync();
ushort[] values = await client.ReadHoldingRegistersAsync(startAddress: 0, quantity: 2);
await client.DisconnectAsync();

如果需要完整可运行示例,可执行:

dotnet run --project "samples/LinkForge.Sample.ModbusTcp/LinkForge.Sample.ModbusTcp.csproj" -- --host 192.168.0.10 --port 502 --unit 1

也可以通过环境变量传参:

$env:LINKFORGE_MODBUS_HOST="192.168.0.10"
$env:LINKFORGE_MODBUS_PORT="502"
$env:LINKFORGE_MODBUS_UNIT_ID="1"
dotnet run --project "samples/LinkForge.Sample.ModbusTcp/LinkForge.Sample.ModbusTcp.csproj"

🧪 构建与验证

dotnet build "LinkForge.sln"
dotnet test "LinkForge.sln"
dotnet pack "LinkForge.sln" -c Release

生成的包默认输出到 artifacts/packages

🛣️ 后续规划

  1. 继续补充异常响应、断连、超时、半包与事务号不匹配等回归场景。
  2. 视协议规划新增 LinkForge.Transports.Serial,再扩展 Modbus RTU
  3. 在抽象稳定后继续落地 S7MCOPC UA 等独立包。

📄 许可证

本仓库使用 Apache License 2.0,详见根目录 LICENSE

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 was computed.  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 was computed.  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. 
.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

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
0.1.0 109 3/20/2026

0.1.0 初始稳定版,包含公共通道抽象、TCP 传输实现、Modbus TCP 常用功能码与 xUnit 回归测试。