Modbus 2.0.0

A free software library to send/receive data with devices which respects the Modbus protocol.

Install-Package Modbus -Version 2.0.0
dotnet add package Modbus --version 2.0.0
paket add Modbus --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Modbus library

To communicate with slaves, you must create an ICommStream (currently, this library supports for SerialStream via serial port). After that, call the request function corresponds witch each function code. The request function will send a request to slave and waiting for the response, at that time, other requests will be pending until the current one is completed.

var serialPort = new SerialPort("COM1");
var stream = new SerialStream(serialPort);  
var responseBytes = stream.RequestFunc3(slaveId, dataAddress, registerCount);  

Supported function codes (example and code)

Read Holding Registers (FC=03)

// send request and waiting for response
// the request needs: slaveId, dataAddress, registerCount
var responseBytes = stream.RequestFunc3(0x11, 0x006B, 0x0003);
// extract the content part (the most important in the response)
var data= responseBytes.ToResponseFunc3().Data;

Read Input Registers (FC=04)

// send request and waiting for response
// the request needs: slaveId, dataAddress, registerCount
var responseBytes = stream.RequestFunc4(0x11, 0x0008, 0x0001);
// extract the content part (the most important in the response)
var data= responseBytes.ToResponseFunc4().Data;

Preset Single Register (FC=06)

// send request and waiting for response
// the request needs: slaveId, dataAddress, writeValue
var responseBytes = stream.RequestFunc6(0x11, 0x0001, 0x0003);
// the reponse bytes should be an echo of the request.

Diagnosis (FC=08)

// send request and waiting for response
// the request needs: slaveId, subFunction, data
var responseBytes = stream.RequestFunc8(0x0B, 0x0000, 0x0203);

Preset Multiple Registers (FC=16)

// send request and waiting for response
// the request needs: slaveId, dataAddress, writeValue
var responseBytes = stream.RequestFunc16(0x11, 0x0001, new byte[] {0x00, 0x0A, 0x01, 0x02});

Modbus library

To communicate with slaves, you must create an ICommStream (currently, this library supports for SerialStream via serial port). After that, call the request function corresponds witch each function code. The request function will send a request to slave and waiting for the response, at that time, other requests will be pending until the current one is completed.

var serialPort = new SerialPort("COM1");
var stream = new SerialStream(serialPort);  
var responseBytes = stream.RequestFunc3(slaveId, dataAddress, registerCount);  

Supported function codes (example and code)

Read Holding Registers (FC=03)

// send request and waiting for response
// the request needs: slaveId, dataAddress, registerCount
var responseBytes = stream.RequestFunc3(0x11, 0x006B, 0x0003);
// extract the content part (the most important in the response)
var data= responseBytes.ToResponseFunc3().Data;

Read Input Registers (FC=04)

// send request and waiting for response
// the request needs: slaveId, dataAddress, registerCount
var responseBytes = stream.RequestFunc4(0x11, 0x0008, 0x0001);
// extract the content part (the most important in the response)
var data= responseBytes.ToResponseFunc4().Data;

Preset Single Register (FC=06)

// send request and waiting for response
// the request needs: slaveId, dataAddress, writeValue
var responseBytes = stream.RequestFunc6(0x11, 0x0001, 0x0003);
// the reponse bytes should be an echo of the request.

Diagnosis (FC=08)

// send request and waiting for response
// the request needs: slaveId, subFunction, data
var responseBytes = stream.RequestFunc8(0x0B, 0x0000, 0x0203);

Preset Multiple Registers (FC=16)

// send request and waiting for response
// the request needs: slaveId, dataAddress, writeValue
var responseBytes = stream.RequestFunc16(0x11, 0x0001, new byte[] {0x00, 0x0A, 0x01, 0x02});

Release Notes

Supports function codes: 3, 4, 6, 8, 16

Version History

Version Downloads Last updated
2.0.0 208 10/19/2018
1.0.0 1,466 6/4/2017