DLNmodbus4TCPHelper 1.0.0

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

DLNmodbus4TCPHelper

基于 NModbus4 的 Modbus TCP 辅助类,简化 Modbus TCP 通信开发。

安装

dotnet add package DLNmodbus4TCPHelper

使用示例

using ModbusDemo;

// 创建实例
var modbus = new DLNmodbus4TCPHelper("192.168.1.100", 502);

// 连接
if (modbus.Connect())
{
    // 读取保持寄存器
    ushort[] registers = modbus.Nmodbus4Tcp_ReadHoldingRegisters(1, 0, 10);

    // 写入单个线圈
    modbus.Nmodbus4Tcp_WriteSingleCoil(1, 0, true);

    // 断开连接
    modbus.Disconnect();
}

主要方法

连接管理

  • Connect() - 建立TCP连接
  • Disconnect() - 断开连接
  • IsConnected - 连接状态

读取操作

  • Nmodbus4Tcp_ReadCoils - 读取线圈
  • Nmodbus4Tcp_ReadInputs - 读取输入
  • Nmodbus4Tcp_ReadHoldingRegisters - 读取保持寄存器

写入操作

  • Nmodbus4Tcp_WriteSingleCoil - 写入单个线圈
  • Nmodbus4Tcp_WriteSingleRegister - 写入单个寄存器
  • Nmodbus4Tcp_WriteMultipleCoils - 批量写入线圈
  • Nmodbus4Tcp_WriteMultipleRegisters - 批量写入寄存器

错误处理

所有操作在未连接时会抛出 InvalidOperationException。建议使用 try-catch 捕获异常:

try
{
    modbus.Nmodbus4Tcp_WriteSingleCoil(1, 0, true);
}
catch (InvalidOperationException ex)
{
    Console.WriteLine($"请先连接: {ex.Message}");
}
catch (Exception ex)
{
    Console.WriteLine($"操作失败: {ex.Message}");
}

依赖

  • NModbus4 2.1.0
  • .NET Framework 4.8 或更高版本
Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 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
1.0.0 147 6/5/2026