Modbus development ecosystem on. NET platform
In Windows industrial automation software development,. NET (C #/VB. NET) is one of the most mainstream development platforms. Modbus.org recommends multiple options The Modbus library on the. NET platform covers different choices from open source free to commercial support. This article will introduce the characteristics and usage methods of these libraries one by one.
NModbus - The Most Popular C # Modbus Library
NModbus is The most mature and widely used Modbus open source library on the. NET platform. Originally hosted on Google Code, now migrated to GitHub:github.com/NModbus/NModbus. VNet package name:NModbus.
Features:
- Supports four transmission modes: Modbus RTU (serial port), Modbus ASCII, Modbus TCP, and Modbus UDP
- Fully supports all commonly used function codes (FC01-FC06, FC15, FC16)
- One click installation through the VNet Package Manager
- Support NET Framework and NET Core/. NET 5+
- Open Source License: MIT (Commercially Available)
Installation
# NuGet 命令行安装
dotnet add package NModbus
# 或通过 Visual Studio 的 NuGet 包管理器搜索 "NModbus"Modbus TCP Read/Write Example
using System;
using System.Net.Sockets;
using NModbus;
// 创建 TCP 客户端
using var client = new TcpClient("192.168.1.100", 502);
var factory = new ModbusFactory();
var master = factory.CreateMaster(client);
// 读取从站 1 的保持寄存器 0-9
ushort[] registers = master.ReadHoldingRegisters(1, 0, 10);
Console.WriteLine("读取结果:");
for (int i = 0; i < registers.Length; i++)
Console.WriteLine($" 地址 {i}: {registers[i]}");
// 写入单个寄存器
master.WriteSingleRegister(1, 0, 1234);
// 批量写入多个寄存器
ushort[] writeData = { 100, 200, 300 };
master.WriteMultipleRegisters(1, 10, writeData);Modbus RTU Serial Communication
using System.IO.Ports;
using NModbus;
using var port = new SerialPort("COM3")
{
BaudRate = 9600,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One
};
port.Open();
var factory = new ModbusFactory();
var master = factory.CreateRtuMaster(port);
ushort[] data = master.ReadHoldingRegisters(1, 0, 5);
foreach (var val in data)
Console.WriteLine(val);Free. NET Modbus
Free. NET Modbus is another open source NET Modbus library, hosted on Google Code Archive. Similar to NModbus, it also supports Modbus ASCII, RTU, TCP, and UDP. If you are using an old version NET Framework (2.0/3.5) , This library may have better compatibility. But for new projects, it is recommended to use the actively maintained NModbus.
ASComm.net - Commercial Grade NET Modbus Driver
ASComm.net is a commercial product of Automated Solutions NET component, official website:automatedsolutions.com. It is fully managed The NET library not only supports Modbus TCP/RTU/ASCII, but also supports various industrial protocols such as Allen Bradley Logix, PLC5, SLC500, MicroLogix, and GE SRTP.
Core Features
- Visualization Design: It can be configured through code or visualized through the Visual Studio property window
- Background Scanning: Automatically scans data points at a specified rate and triggers events when data changes are detected
- Serialization support: Configuration can be serialized to XML/JSON format
- 30 day free trial: No functional restrictions, the full version can be fully tested
- Multi-protocolOne component supports Modbus+Allen Bradley+GE, etc
ASComm.net Code Example
using ASComm.NET;
// 创建 Modbus TCP 通信组件
var comm = new ModbusTcpComm();
comm.RemoteAddress = "192.168.1.100";
comm.RemotePort = 502;
// 添加要扫描的数据组
var group = new ModbusScanGroup();
group.SlaveId = 1;
group.StartAddress = 0;
group.Length = 10;
group.ScanRate = 1000; // 每秒扫描一次
// 数据变化事件
group.DataChanged += (s, e) =>
{
Console.WriteLine($"地址 {e.Address} 变为 {e.Value}");
};
comm.ScanGroups.Add(group);
comm.Connect();Delphi Modbus TCP component
For Delphi/Lazarus developers, there is an open-source Modbus TCP component set hosted on SourceForge:sourceforge.net/projects/delphimodbusIt is based on the Indy (Internet Direct) network component library and supports two versions, Indy 9 and Indy 10.
⚠️AttentionThis project has a low level of activity, and the recent Delphi version (10.4 Sydney/11 Alexandria) may need adjustments to be used directly. If you need to develop Modbus functionality on the new version of Delphi, you can also consider calling MBServer ActiveX through COM interoperability or using Delphi to call the C language version of libmodbus dynamic library (DLL).
ModLink - Delphi VCL Native Component
ModLink is a native VCL component for Borland Delphi, developed by Ivo Bauer and available on the official websitewww.ozm.cz/ivobauer/modlinkIt is a set of visual components that can be directly dragged and dropped onto Delphi forms, allowing Delphi developers to integrate Modbus communication capabilities without handwritten Socket communication code.
ModLink supports multiple network types, including:
- Modbus TCP (via TModLinkTCP component)
- Modbus RTU serial port (via TModLinkRTU component)
- Modbus ASCII serial port
- Modbus Plus network
⚠️ ModLink is a product of the Delphi era, and the project has not been updated for many years. If you are maintaining an old Delphi industrial control project, ModLink still has value; But for new projects, it is recommended to use more modern solutions such as NModbus (. NET) or pymodbus (Python). How to choose
NET/Delphi Modbus Library
| Scenario | Recommendation | reason |
|---|---|---|
| C # New Project | NModbus (NuGet) | MIT license, active maintenance, concise API |
| . NET Business Project | ASComm.NET | Multi protocol support, visual configuration, commercial support |
| Old Delphi project | ModLink or DelphiModbus | Native Delphi components, eliminating cross language calls |
| Python Script | pymodbus | The most active Python Modbus library |
Summary
The Modbus development ecosystem on the. NET platform is the most diverse. For new projects, open source and free NModbus is the best choice for most scenarios - MIT license, one click installation of VNet, and support for all mainstream transport modes. If it is necessary to connect Allen Bradley or GE devices simultaneously, ASComm.net provides a unified multi protocol solution. Although Delphi developers have fewer options, ModLink and DelphiModbus components cover the most basic Modbus TCP/RTU communication requirements.
Leave a Reply