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
<PackageReference Include="Longbow.Modbus" Version="9.1.0" />
<PackageVersion Include="Longbow.Modbus" Version="9.1.0" />
<PackageReference Include="Longbow.Modbus" />
paket add Longbow.Modbus --version 9.1.0
#r "nuget: Longbow.Modbus, 9.1.0"
#:package Longbow.Modbus@9.1.0
#addin nuget:?package=Longbow.Modbus&version=9.1.0
#tool nuget:?package=Longbow.Modbus&version=9.1.0
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.
🔗 Related Links
📞 Contact
To contact the developers, visit the project homepage or submit issues to GitHub Issues.
Product | Versions 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. |
-
net6.0
- Longbow.SerialPorts (>= 9.0.2)
- Longbow.TcpSocket (>= 9.0.12)
-
net7.0
- Longbow.SerialPorts (>= 9.0.2)
- Longbow.TcpSocket (>= 9.0.12)
-
net8.0
- Longbow.SerialPorts (>= 9.0.2)
- Longbow.TcpSocket (>= 9.0.12)
-
net9.0
- Longbow.SerialPorts (>= 9.0.2)
- Longbow.TcpSocket (>= 9.0.12)
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.
|