Longbow.Modbus 9.1.0

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

Longbow.Modbus

Longbow.Modbus is a .NET library for communicating with devices that support the Modbus protocol. It supports multiple transport modes, including TCP, RTU, UDP, and RTU over TCP/UDP.

🚀 Features

  • Supports Modbus TCP, RTU, UDP, and RTU over TCP/UDP communication.
  • Provides asynchronous APIs for efficient non-blocking communication.
  • Supports common Modbus function codes, such as reading coils, input registers, holding registers, and writing single or multiple coils and registers.
  • Offers dependency injection integration for easy use in modern .NET applications.
  • Supports connection pool management to improve performance and resource utilization.

📦 Installation

You can install Longbow.Modbus via NuGet:

dotnet add package Longbow.Modbus

🛠️ Quick Start

1. Register Services

If you are using dependency injection (e.g., in ASP.NET Core), you can register Modbus services in Startup.cs or Program.cs:

services.AddModbusFactory();

2. Create a Modbus Client

You can create different types of Modbus clients using IModbusFactory:

var modbusFactory = serviceProvider.GetRequiredService<IModbusFactory>();

var modbusFactory = serviceProvider.GetRequiredService<IModbusFactory>();

// Create a TCP client
var tcpClient = modbusFactory.GetOrCreateTcpMaster("tcp-client", options =>
{
    options.ConnectTimeout = 5000;
    options.LocalEndPoint = new IPEndPoint(IPAddress.Any, 0);
});

// Create a UDP client
var udpClient = modbusFactory.GetOrCreateUdpMaster("udp-client", options =>
{
    options.ConnectTimeout = 5000;
    options.LocalEndPoint = new IPEndPoint(IPAddress.Any, 0);
});

// Create a RTU client
var rtuClient = modbusFactory.GetOrCreateRtuMaster("rtu-client", options =>
{
    options.PortName = "COM1";
    options.BaudRate = 9600;
});

// Create a RTU Over TCP client
var rtuClient = modbusFactory.GetOrCreateRtuOverTcpMaster("rtu-over-tcp-client", options =>
{
    options.ConnectTimeout = 5000;
    options.LocalEndPoint = new IPEndPoint(IPAddress.Any, 0);
});

// Create a RTU Over UDP client
var rtuClient = modbusFactory.GetOrCreateRtuOverUdpMaster("rtu-over-udp-client", options =>
{
    options.ConnectTimeout = 5000;
    options.LocalEndPoint = new IPEndPoint(IPAddress.Any, 0);
});

3. Connect to a Modbus Device

Connect to the device based on the protocol you are using:

// TCP connection
await tcpClient.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.0.1"), 502));

// RTU connection
await rtuClient.ConnectAsync();

4. Read Data

You can use the following methods to read different types of data:

// Read coils
bool[] coils = await client.ReadCoilsAsync(1, 0, 10);

// Read input registers
ushort[] inputRegisters = await client.ReadInputRegistersAsync(1, 0, 10);

// Read holding registers
ushort[] holdingRegisters = await client.ReadHoldingRegistersAsync(1, 0, 10);

// Read input registers
ushort[] holdingRegisters = await client.ReadInputRegistersAsync(1, 0, 10);

5. Write Data

You can use the following methods to write data:

// Write a single coil
await client.WriteCoilAsync(1, 0, true);

// Write multiple coils
await client.WriteMultipleCoilsAsync(1, 0, new bool[] { true, false, true });

// Write a single register
await client.WriteRegisterAsync(1, 0, 1234);

// Write multiple registers
await client.WriteMultipleRegistersAsync(1, 0, new ushort[] { 1234, 5678 });

🔧 Supported Modbus Function Codes

  • 0x01 - Read Coil Status (FC1)
  • 0x02 - Read Input Status (FC2)
  • 0x03 - Read Holding Registers (FC3)
  • 0x04 - Read Input Registers (FC4)
  • 0x05 - Write Single Coil (FC5)
  • 0x06 - Write Single Register (FC6)
  • 0x0F - Write Multiple Coils (FC15)
  • 0x10 - Write Multiple Registers (FC16)

🚧 Notes

  • Ensure the device's IP address, port, serial port settings, etc., are configured correctly.
  • Use asynchronous methods to avoid blocking the main thread.
  • When using the RTU protocol, ensure the serial port device is properly connected and configured.
  • When using connection pooling, manage client instances properly to avoid resource leaks.

🤝 Contribution

Contributions to the code and documentation are welcome! Please refer to CONTRIBUTING.md for more information.

📄 License

This project is licensed under the Apache License. See the LICENSE file for details.

📞 Contact

To contact the developers, visit the project homepage or submit issues to GitHub Issues.

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
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 (1)

Showing the top 1 popular GitHub repositories that depend on Longbow.Modbus:

Repository Stars
dotnetcore/BootstrapBlazor
Bootstrap Blazor is an enterprise-level UI component library based on Bootstrap and Blazor.
Version Downloads Last Updated
9.1.0 0 9/24/2025
9.0.10 0 9/24/2025
9.0.9 114 9/21/2025
9.0.8 125 9/20/2025
9.0.7 273 9/18/2025
9.0.6 342 9/17/2025
9.0.5 278 9/16/2025
9.0.4 124 9/13/2025
9.0.3 161 9/10/2025
9.0.2 172 9/7/2025
9.0.1 107 9/7/2025
9.0.0 90 9/6/2025