Skip to content

vandycknick/libmodbus.cs

Repository files navigation

LibModbus

Build status NuGet feedz.io

Library to send/receive data with a device which respects the Modbus protocol for dotnet.

Install

Install the NuGet package into your project.

PM> Install-Package LibModbus
$ dotnet add package LibModbus

Supported Functions

  • Read Coils (FC1)
  • Read Discrete Inputs (FC2)
  • Read Holding Registers (FC3)
  • Read Input Registers (FC4)
  • Write Single Coil (FC5)
  • Write Single Register (FC6)
  • Write Multiple Coils (FC15)
  • Write Multiple Registers (FC16)

Usage

Client Builder

var address = "127.0.0.1";
await using var client = new ModbusClientBuilder()
                        .UseSocket(address)
                        .Build();

Connect And Read Coils

await client.ConnectAsync();

var coils = await client.ReadCoils(2, 10);

var index = 2;
foreach (var coil in coils)
{
    var onOrOff = coil ? "on" : "off";
    Console.WriteLine($"Coil {index++} is {onOrOff}");
}

Protocol-Specification

License

Copyright 2020 Nick Van Dyck

MIT