DL_TCP 1.0.1

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

DL_TCP

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

关于作者

大林机器视觉实训中心(大林)专注于工业视觉与上位机自动化培训,提供涵盖机器视觉算法,深度学习,具身智能,数字孪生,大语言模型相机与光源应用、C#上位机开发、PLC通信及项目实战落地的一体化课程与技术服务,致力于培养具备工程实战能力的工业视觉人才。

微信:15861139266

安装

dotnet add package DL_TCP

使用示例

using DL_ModbusTCP;

// 创建实例
var modbus = new DL_ModbusTCP("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.1 145 6/5/2026