ModbusApi 1.0.0
dotnet add package ModbusApi --version 1.0.0
NuGet\Install-Package ModbusApi -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="ModbusApi" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ModbusApi" Version="1.0.0" />
<PackageReference Include="ModbusApi" />
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 ModbusApi --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ModbusApi, 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 ModbusApi@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=ModbusApi&version=1.0.0
#tool nuget:?package=ModbusApi&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ModbusApi
基于 FluentModbus 的 Modbus 通讯二次封装类库,支持 Modbus TCP 与 Modbus RTU,对外只暴露简洁的读写接口,屏蔽底层库、TcpClient、SerialPort 等实现细节。
特性
- 统一接口,TCP / RTU 通过配置切换
- 强类型读写:
bool/short/int/float/string - 支持单个与连续读写
- 读写自动分段,突破 Modbus 单次数量上限
- 内置字节序(大小端)配置
- 字符串采用定长寄存器 + ASCII 编码
- 线程安全(内部加锁,多线程可安全调用)
- TCP / RTU 超时可配置(连接、读、写)
技术栈
| 项 | 说明 |
|---|---|
| 目标框架 | .NET 10.0 |
| 底层库 | FluentModbus 5.3.2 |
| 串口支持 | System.IO.Ports 9.0.0 |
目录结构
ModbusApi/
├── Modbus/
│ ├── IModbusClient.cs # 对外接口
│ ├── ModbusClient.cs # 实现
│ ├── ModbusConnectionOptions.cs # 连接配置
│ └── ModbusEnums.cs # 枚举定义
├── Demo/
│ ├── Program.cs # 演示程序
│ └── Demo.csproj
├── ModbusApi.csproj
└── README.md
快速开始
安装依赖
dotnet add package FluentModbus --version 5.3.2
dotnet add package System.IO.Ports --version 9.0.0
TCP 示例
using ModbusApi.Modbus;
var options = new ModbusConnectionOptions
{
Type = ModbusConnectionType.Tcp, IpAddress = "192.168.1.100",
Port = 502, UnitIdentifier = 1, Endianness = ModbusEndianness.BigEndian
};
using var client = new ModbusClient(options);
client.Open();
var speed = client.ReadFloat(0); // 读 float(占 2 寄存器)
var status = client.ReadBool(100); // 读线圈
client.WriteInt16(10, 1234); // 写 short(1 寄存器)
client.WriteString(20, "HELLO", 8); // 写 ASCII 字符串(8 寄存器)
RTU 示例
var options = new ModbusConnectionOptions
{
Type = ModbusConnectionType.Rtu, PortName = "COM3",
BaudRate = 9600, Parity = ModbusParity.Even, StopBits = ModbusStopBits.One
};
using var client = new ModbusClient(options);
client.Open();
var values = client.ReadInt16s(0, 10); // 连续读 10 个 short
API 一览
连接管理
| 方法/属性 | 说明 |
|---|---|
Open() |
建立连接 |
Close() |
关闭连接 |
IsConnected |
是否已连接 |
读写方法
| 类型 | 寄存器/线圈占用 | 读单个 | 写单个 | 连续读 | 连续写 |
|---|---|---|---|---|---|
bool |
1 线圈 | ReadBool |
WriteBool |
ReadBools |
WriteBools |
short |
1 寄存器 | ReadInt16 |
WriteInt16 |
ReadInt16s |
WriteInt16s |
int |
2 寄存器 | ReadInt32 |
WriteInt32 |
ReadInt32s |
WriteInt32s |
float |
2 寄存器 | ReadFloat |
WriteFloat |
ReadFloats |
WriteFloats |
string |
N 寄存器 | ReadString |
WriteString |
ReadStrings |
WriteStrings |
数据类型约定
- 地址:均为寄存器/线圈地址(16 位),从 0 开始。
int/float:占用 2 个寄存器,字节序由Endianness控制。- 字符串:定长寄存器 + ASCII 编码,每寄存器存放 2 个字符;不足补
\0,超出部分截断。读取时自动去除尾部\0。
字节序
通过 ModbusConnectionOptions.Endianness 设置:
BigEndian(默认):符合 Modbus 标准LittleEndian:适用于部分国产 PLC / 设备
注意事项与限制
- 自动分段:单次读保持寄存器按 125 个寄存器分段、写按 123 个分段;读线圈按 2000 个、写线圈按 1968 个分段,调用方无需自行处理。
int/float因每元素占 2 寄存器,自动按元素边界切分。 - 超时:
ConnectTimeout(仅 TCP)/ReadTimeout/WriteTimeout对 TCP 与 RTU 均生效,默认均为 1000 ms。 - 所有方法为同步调用,网络/串口阻塞期间会占用当前线程。
- 未实现自动重连:连接断开后需重新调用
Open()。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- FluentModbus (>= 5.3.2)
- System.IO.Ports (>= 9.0.0)
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 | 94 | 8/29/2026 |