CSoft.Protocol.Modbus 10.0.10

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" />
                    
Directory.Packages.props
<PackageReference Include="CSoft.Protocol.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 CSoft.Protocol.Modbus --version 10.0.10
                    
#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
                    
Install as a Cake Addin
#tool nuget:?package=CSoft.Protocol.Modbus&version=10.0.10
                    
Install as a Cake Tool

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

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.

修复字符串支持