Wyrm.ModBusClient 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Wyrm.ModBusClient --version 1.4.0
                    
NuGet\Install-Package Wyrm.ModBusClient -Version 1.4.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="Wyrm.ModBusClient" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wyrm.ModBusClient" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Wyrm.ModBusClient" />
                    
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 Wyrm.ModBusClient --version 1.4.0
                    
#r "nuget: Wyrm.ModBusClient, 1.4.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 Wyrm.ModBusClient@1.4.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=Wyrm.ModBusClient&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Wyrm.ModBusClient&version=1.4.0
                    
Install as a Cake Tool

Wyrm.ModBusClient

A .Net TCP ModBus client library.

Usage

Add the ModBus client into dependency injection using the AddModBusClient extension method.

using Wyrm.ModBusClient.DependencyInjection;
...
services.AddModBusClient();

By default, it is added as a Singleton but you can specify the lifetime.

services.AddModBusClient(ServiceLifetime.Scoped);

Then inject the IModBusClient interface into your class.

Call ConnectAsync before calling any of the ModBus methods. Although the client can remain connected, it's better to connect, perform the function or functions, and then close it. You can connect the client again after closing it. Make sure you dispose of the client when finished with it.

Functions

Only the TCP specific functions have been implemented. You can set the Unit Identifier before calling any function (it will keep the value set for subsequent functions).

await _modBusClient.ConnectAsync(endPoint, ct);
...
_modBusClient.UnitIdentifier = 0xFF;
var coils = await _modBusClient.ReadCoilsAsync(1, 5, ct);
...
_modBusClient.Close();

You can also set the Transaction Id before calling any function. The Transaction Id increments after each function is called so if you need it to always be a specific value then you need to set it before calling a function.

await _modBusClient.ConnectAsync(endPoint, ct);
...
_modBusClient.TransactionId = 0x5959;
var coils = await _modBusClient.ReadCoilsAsync(1, 5, ct);
...
_modBusClient.Close();

It is also possible to set the Protocol Identifier (for subsequent functions). Usually this is always 0, but certain vendors may require a different number.

await _modBusClient.ConnectAsync(endPoint, ct);
_modBusClient.ProtocolIdentifier = 1;
...
_modBusClient.Close();

You can also set a framer function to be used for all subsequent functions to frame issued commands. Usually this isn't required but some vendors have a custom protocol. The framer function takes the extended PDU (Unit Identifier + PDU) and should return the framed PDU in its entirety. The transaction id, protocol identifier, and data length will be applied as usual.

_modBusClient.PduFramer = PduFramerFunc;
var coils = await _modBusClient.ReadCoilsAsync(1, 5, ct);
...
private IList<byte> PduFramerFunc(IList<byte> command)
{
    var framedCommand = new List<byte>();
    framedCommand.AddRange([0x01, 0x02]);
    framedCommand.AddRange(new byte[16]);
    framedCommand.AddRange([0, (byte)(command.Count + 2)]);
    framedCommand.AddRange(command);
    framedCommand.AddRange(CheckSumBytes(command));
    return framedCommand;
}

If commands need to be framed then their responses will almost certainly need to be deframed. You can set a deframer function to do that for all subsequent functions. The deframer function takes the framed PDU (everything after the length word) and should return a valid extended MODBUS PDU (Unit Identifier + PDU). Note that there may be additional fields within the framed PDU data that also need to be removed.

_modBusClient.PduFramer = PduFramerFunc;
_modBusClient.PduDeframer = PduDeframerFunc;
var coils = await _modBusClient.ReadCoilsAsync(1, 5, ct);
...
private ReadOnlyMemory<byte> PduDeframerFunc(ReadOnlyMemory<byte> response)
{
    return response.Slice(20, (response.Length - 22));
}

The functions will only return when data has been received so need to be cancelled if they run for too long.

Request/Response Functions

Some systems issue multiple register value responses for one request. To support those, there are specific request functions for the two read register functions and a generic read response.

await _modBusClient.ConnectAsync(endPoint, ct);
...
await _modBusClient.ReadHoldingRegistersRequestAsync(1, 5, ct);
...
while (true)
{
    try
    {
        var response = _modBusClient.ReadRegistersResponseDataAsync(ct);
        this.ProcessResponse(response);
    }
    catch (ModBusClientException)
    {
    }
    catch (OperationCanceledException)
    {
        break;
    }
}
...
_modBusClient.Close();

The idea here is that there is a constant loop requesting data and processing the responses, and the requests are issued separately (such as on a timer).

Implemented Functions

Function
ReadCoilsAsync
ReadDiscreteInputsAsync
ReadHoldingRegistersAsync
ReadInputRegistersAsync
WriteSingleCoilAsync
WriteSingleRegisterAsync
WriteMultipleCoilsAsync
WriteMultipleRegistersAsync
ReadFileRecordAsync
WriteFileRecordAsync
MaskWriteRegisterAsync
ReadWriteMultipleRegistersAsync
ReadFifoQueueAsync
ReadDeviceIdentifierAsync
ReadHoldingRegistersRequestAsync
ReadInputRegistersRequestAsync
ReadRegistersResponseDataAsync
SendCustomPduAsync
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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 (1)

Showing the top 1 NuGet packages that depend on Wyrm.ModBusClient:

Package Downloads
Wyrm.ModBusClient.GivEnergy

A TCP ModBus client for GivEnergy inverters.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.0 187 7/17/2026
1.5.0 121 7/4/2026
1.4.0 129 7/3/2026
1.3.1 117 6/30/2026
1.3.0 116 5/30/2026
1.2.0 117 5/22/2026
1.1.0 115 5/22/2026
1.0.0 123 5/10/2026