NewLife.ModbusRTU 2.0.2026.610

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

NewLife.Modbus - ModbusTcp/ModbusRTU

GitHub top language GitHub License Nuget Downloads Nuget Nuget (with prereleases)

Nuget Downloads Nuget Nuget (with prereleases)

Modbus协议完整实现,包括 ModbusTCP / ModbusRTU / ModbusASCII 及多种网络变体,基于 NewLife.IoT 标准接口,支持 IoTEdge 网关。

源码: https://github.com/NewLifeX/NewLife.Modbus
Nuget:NewLife.Modbus / NewLife.ModbusRTU
文档:需求文档 / 架构设计

主要特点

  • 协议完整:实现 Modbus 应用协议规范 V1.1b3,支持 12 个公共功能码(FC01~FC06/FC08/FC15/FC16/FC21/FC23/FC43)
  • 多传输变体:ModbusTCP、ModbusUDP、ModbusRTU(串口)、ModbusASCII(串口)、ModbusRtuOverTCP、ModbusRtuOverUDP
  • 主从完备:同时提供 Master(主机/客户端)和 Slave(从机/服务端)实现,Slave 支持多客户端并发连接
  • 零分配编解码:热点路径使用 SpanReader / SpanWriter,减少 GC 压力
  • 批处理优化BuildSegments 自动合并连续地址,单次请求读取多个点位,降低轮询开销
  • IoT 驱动集成:实现 IDriver 标准接口,可零代码接入 IoTEdge 平台,5 种驱动可选
  • 轻量串口实现ModbusRtuSimple 针对资源受限边缘设备,减少依赖,更小内存占用
  • APM 可观测:内置 ITracer 链路追踪和 ILog 日志支持,与星尘分布式平台集成
  • 多框架支持net462 / net45 / netstandard2.0 / netstandard2.1 / net6.0 / net7.0 / net8.0 / net9.0 / net10.0

支持的传输变体

变体 NuGet 包 描述
ModbusTCP NewLife.Modbus 标准 Modbus TCP/IP,MBAP头+PDU,端口 502
ModbusUDP NewLife.Modbus Modbus over UDP,无连接传输
ModbusRtuOverTCP NewLife.Modbus RTU 帧(含CRC)通过 TCP 传输
ModbusRtuOverUDP NewLife.Modbus RTU 帧(含CRC)通过 UDP 传输
ModbusRTU NewLife.ModbusRTU 串口二进制紧凑格式,CRC-16 校验
ModbusASCII NewLife.ModbusRTU 串口ASCII十六进制格式,LRC 校验

快速入门

安装 NuGet 包

# TCP/UDP 场景(不需要串口)
dotnet add package NewLife.Modbus

# 串口 RTU/ASCII 场景
dotnet add package NewLife.ModbusRTU

Master 主机 - ModbusTCP 读写寄存器

using NewLife.IoT.Protocols;

// 创建 ModbusTCP 连接
var modbus = new ModbusTcp
{
    Server = "192.168.1.100:502",
    Timeout = 3000,
    Log = XTrace.Log,       // 可选:输出通信日志
};
modbus.Open();

// 读保持寄存器(FC03),站号1,起始地址0,读取10个寄存器
var registers = modbus.ReadRegister(host: 1, address: 0, count: 10);
Console.WriteLine($"寄存器值:{registers?.Join(", ")}");

// 读线圈(FC01)
var coils = modbus.ReadCoil(host: 1, address: 0, count: 8);
Console.WriteLine($"线圈状态:{coils?.Join(", ")}");

// 写单个寄存器(FC06)
modbus.WriteRegister(host: 1, address: 0, value: 1234);

// 写多个寄存器(FC16)
modbus.WriteRegisters(host: 1, address: 0, values: [100, 200, 300]);

// 写单个线圈(FC05)
modbus.WriteCoil(host: 1, address: 0, value: true);

Master 主机 - ModbusRTU 串口通信

using NewLife.IoT.Protocols;

// 创建 ModbusRTU 串口连接
var modbus = new ModbusRtu
{
    PortName = "COM3",       // Windows: "COM3",Linux: "/dev/ttyS0"
    Baudrate = 9600,
    Log = XTrace.Log,
};
modbus.Open();

// 读保持寄存器(FC03)
var registers = modbus.ReadRegister(host: 1, address: 0, count: 5);

// 写多个寄存器(FC16)
modbus.WriteRegisters(host: 1, address: 0, values: [0xABCD, 0xEF01]);

Slave 从机 - TCP 仿真服务器

using NewLife.IoT;
using NewLife.IoT.Models;

// 创建并配置从机
var slave = new ModbusSlave
{
    Port = 502,
    Log = XTrace.Log,
};

// 预置寄存器数据(保持寄存器,FC03/FC06 操作)
for (var i = 0; i < 100; i++)
    slave.Registers.Add(new RegisterUnit { Address = (UInt16)i, Value = (UInt16)(i * 10) });

// 预置线圈数据(FC01/FC05 操作)
for (var i = 0; i < 16; i++)
    slave.Coils.Add(new CoilUnit { Address = (UInt16)i, Value = i % 2 == 0 });

// 启动从机,开始监听
slave.Start();
Console.WriteLine("Modbus Slave 已启动,端口 502");

IoT 驱动层集成

using NewLife.IoT.Drivers;

// 创建 TCP 驱动(支持 IoTEdge 驱动标准接口)
var driver = new ModbusTcpDriver();
var parameter = new ModbusTcpParameter
{
    Server = "192.168.1.100:502",
    Host = 1,
    Timeout = 3000,
    BatchStep = 1,   // 地址间隔 ≤1 的点位自动合并为批量请求
    BatchSize = 100, // 每批最多 100 个点位
};

// 打开驱动(单连接复用)
var node = driver.Open(device: null, parameter);

// 批量读取(会自动合并连续地址,减少请求次数)
var points = new[] { /* ThingModel 点位定义 */ };
var result = driver.Read(node, points);

新生命开发团队

XCode

新生命团队(NewLife)成立于2002年,是新时代物联网行业解决方案提供者,致力于提供软硬件应用方案咨询、系统架构规划与开发服务。
团队主导的80多个开源项目已被广泛应用于各行业,Nuget累计下载量高达400余万次。
团队开发的大数据中间件NewLife.XCode、蚂蚁调度计算平台AntJob、星尘分布式平台Stardust、缓存队列组件NewLife.Redis以及物联网平台FIoT,均成功应用于电力、高校、互联网、电信、交通、物流、工控、医疗、文博等行业,为客户提供了大量先进、可靠、安全、高质量、易扩展的产品和系统集成服务。

我们将不断通过服务的持续改进,成为客户长期信赖的合作伙伴,通过不断的创新和发展,成为国内优秀的IoT服务供应商。

新生命团队始于2002年,部分开源项目具有20年以上漫长历史,源码库保留有2010年以来所有修改记录
网站:https://newlifex.com
开源:https://github.com/newlifex
QQ群:1600800/1600838
微信公众号:
智能大石头

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on NewLife.ModbusRTU:

Package Downloads
SmartA2

IoT驱动,符合IoT标准库的PC驱动,采集CPU、内存、网络等数据,提供语音播报和重启等服务。

SmartA4

IoT驱动,符合IoT标准库的PC驱动,采集CPU、内存、网络等数据,提供语音播报和重启等服务。

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on NewLife.ModbusRTU:

Repository Stars
NewLifeX/XCoder
新生命码神工具,代码生成、网络工具、API工具、串口工具、正则工具、图标工具、加解密工具、地图接口。
Version Downloads Last Updated
2.0.2026.610 107 6/10/2026
2.0.2026.609-beta1551 92 6/9/2026
2.0.2026.501 119 5/1/2026
2.0.2026.501-beta1644 105 5/1/2026
2.0.2026.423-beta1215 101 4/23/2026
2.0.2026.303-beta1601 143 3/3/2026
2.0.2026.102-beta1656 174 1/2/2026
2.0.2025.818-beta1641 246 8/18/2025
2.0.2025.803-beta1524 221 8/3/2025
2.0.2025.803-beta1427 178 8/3/2025
2.0.2025.723-beta1756 608 7/23/2025
2.0.2025.701 966 7/1/2025
2.0.2025.701-beta1714 226 7/1/2025
2.0.2025.627-beta0843 182 6/27/2025
2.0.2025.626-beta0034 210 6/26/2025
2.0.2025.623-beta0832 218 6/23/2025
2.0.2025.622-beta0309 185 6/22/2025
2.0.2025.602 317 6/2/2025
2.0.2025.602-beta1645 215 6/2/2025
2.0.2025.522-beta1626 249 5/22/2025
Loading failed

升级依赖 NewLife.Core 与基础组件以获取修复与性能改进;修复 ModbusSession 写入/读取数据丢失问题;增加并完善单元与集成测试;增强串口参数与默认映射;CI/CD 优化