Modbus library supports for Modbus Protocol develops base on C# and Net Framework 4.5.


You can clone/download source code from this repository and then add to your project. Or install via Nuget:

Install-Package Modbus


Easiest way.

// use modbus rtu
var session = ModbusSessionFactory.CreateRtuSession(serialPort);
// arguments are slave id, data address and number of registers
var builder = RequestBuilderFactory.CreateRequest03<RtuRequest>(1, 0, 4);
// send request and wait for response
var response = await session.SendRequestAsync<ResponseFunc03>(builder);

But! wait, where is ResponseFunc03, it’s just a use-defined structure. You should define what you want to receive by a strucutre.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct ResponseFunc03
    public byte numberOfBytes;

    public short value;

    public ushort dotPosition;

    public short padding1;
    public short padding2;
    //[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U2, SizeConst = 4)]
    //public ushort[] bytes;

Current version supports function code 01, 02, 03, 04, 05 and 06. Also, the library supports both modbus RTU and modbus TCP. Of couse, you can make your owner request and response types by create Builder from RtuRequest or TcpRequest classes.

 var builder = new RtuRequest.Builder()


Copyright © 2022. All rights reserved.