CSoft.Protocol.Modbus
10.0.10
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
dotnet add package CSoft.Protocol.Modbus --version 10.0.10
NuGet\Install-Package CSoft.Protocol.Modbus -Version 10.0.10
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.Protocol.Modbus" Version="10.0.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSoft.Protocol.Modbus" Version="10.0.10" />
<PackageReference Include="CSoft.Protocol.Modbus" />
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.Protocol.Modbus --version 10.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CSoft.Protocol.Modbus, 10.0.10"
#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.Protocol.Modbus@10.0.10
#: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.Protocol.Modbus&version=10.0.10
#tool nuget:?package=CSoft.Protocol.Modbus&version=10.0.10
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Modbus 协议库
一个简洁易用的 Modbus RTU/TCP 通信库,支持泛型和 Attribute 驱动的数据映射。
快速开始
1. 定义数据模型
public class SensorData
{
[CHProtocol(1)] // 寄存器地址 1
public float Temperature { get; set; }
[CHProtocol(3)] // 寄存器地址 3
public ushort Humidity { get; set; }
[CHProtocol(5)] // 寄存器地址 5
public float Pressure { get; set; }
[CHProtocol(7, count: 4)] // string 需要声明寄存器数量(4 寄存器 = 8 字节)
public string DeviceName { get; set; } = string.Empty;
}
2. 读取数据
// 创建 Modbus 主站(默认 RTU 模式)
IModBusMaster modBus = new ModBusMaster(new SerialPort("COM2"));
await modBus.OpenAsync();
byte address = 0x01;
// 直接读取并映射到类型
var data = await modBus.GetAsync<SensorData>(address);
Console.WriteLine($"温度: {data.Temperature}, 湿度: {data.Humidity}, 压力: {data.Pressure}, 名称: {data.DeviceName}");
3. 写入数据
var newData = new SensorData
{
Temperature = 25.5f,
Humidity = 60,
Pressure = 1013.25f,
DeviceName = "PUMP-A"
};
await modBus.SetAsync(0x01, newData);
高级用法
手动构建 BlockList
var blockList = BlockList.From<SensorData>();
var channels = await modBus.GetAsync(0x01, blockList);
兼容字符串地址
// 仍支持字符串地址格式:"1"、"01"、"0x01"
var data = await modBus.GetAsync<SensorData>("0x01");
支持的数据类型
| C# 类型 | Modbus 寄存器数 |
|---|---|
float |
2 |
double |
4 |
ushort |
1 |
uint |
2 |
short |
1 |
int |
2 |
string |
N(必须通过 CHProtocol(..., count: N) 指定) |
配置选项
// TCP 模式
var modbusTcp = new ModBusMaster(new TcpClient("192.168.1.100", 502), ModbusType.TCP);
// 字节序配置
modBus.IsHighByteBefore_Req = false; // 请求低字节在前
modBus.IsHighByteBefore_Rsp = false; // 响应低字节在前
安装
dotnet add package CSoft.Protocol.Modbus
| Product | Versions 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. |
| .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.
-
.NETStandard 2.0
- CSoft.Protocol.ProtocolInterface (>= 10.0.19)
-
net10.0
- CSoft.Protocol.ProtocolInterface (>= 10.0.19)
-
net8.0
- CSoft.Protocol.ProtocolInterface (>= 10.0.19)
-
net9.0
- CSoft.Protocol.ProtocolInterface (>= 10.0.19)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on CSoft.Protocol.Modbus:
| Package | Downloads |
|---|---|
|
CSoft.Protocol.TPF146D
泽天温压流146D通讯协议 |
|
|
CSoft.Protocol.DustAnalyzerSampling
泽天抽取粉尘仪通讯协议 |
|
|
CSoft.Protocol.TPF113B
泽天温压流113B通讯协议 |
|
|
CSoft.Protocol.DustAnalyzerSquare
泽天方型粉尘仪通讯协议 |
|
|
CSoft.Protocol.DustAnalyzerCircular
泽天圆型粉尘仪通讯协议 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 10.0.10 | 84 | 3/31/2026 | |
| 10.0.9 | 83 | 3/30/2026 | |
| 10.0.8 | 222 | 1/14/2026 | |
| 10.0.7 | 160 | 1/14/2026 | |
| 10.0.6 | 166 | 1/13/2026 | |
| 10.0.5 | 156 | 1/13/2026 | |
| 10.0.4 | 155 | 1/13/2026 | |
| 10.0.3 | 166 | 1/9/2026 | |
| 10.0.2 | 185 | 1/8/2026 | |
| 10.0.1 | 128 | 1/8/2026 | |
| 10.0.0 | 348 | 11/12/2025 | |
| 9.3.2 | 326 | 8/5/2025 | |
| 9.3.1 | 341 | 7/29/2025 | |
| 9.3.0 | 625 | 7/24/2025 | |
| 9.2.5 | 1,030 | 7/23/2025 | |
| 9.2.4 | 1,050 | 7/23/2025 | |
| 9.2.3 | 1,063 | 7/22/2025 | |
| 9.2.2 | 658 | 7/18/2025 | |
| 9.2.1 | 648 | 7/10/2025 | |
| 9.2.0 | 288 | 6/27/2025 |
Loading failed
修复字符串支持