Modbus is an industrial communication protocol. The full details of the Modbus protocol can be found at modbus.org. A good summary can also be found on Wikipedia.
This is an Arduino library for processing Modbus requests. It enables an Arduino, or arduino compatible, board to process requests from a Modbus master/client and formulate an appropriate response based on the library instance's configuration.
This library is able to service the following function codes:
- 1 (Read Coils)
- 2 (Read Discrete Inputs)
- 3 (Read Holding Registers)
- 4 (Read Input Registers)
- 5 (Write Single Coil)
- 6 (Write Single Holding Register)
- 15 (Write Multiple Coils)
- 16 (Write Multiple Holding Registers).
This library works with ModbusADU
objects. See ModbusADU for details.
This library updates coil, descrete input, holding register, and input register arrays based on the PDU portion of the input ADU. It will also update the PDU portion of the ADU to be the appropriate response.
ModbusSlaveLogic
Creates a
ModbusSlaveLogic
object.# include <ModbusSlaveLogic.h> ModbusSlaveLogic modbusLogic;
configureCoils()
Tells the library where coil data is stored and the number of coils. If this function is not run, the library will assume there are no coils.
modbusLogic.configureCoils(coils, numCoils)
modbusLogic
: aModbusSlaveLogic
object.
coils
: an array of coil values. Allowed data types: array ofbool
.
numCoils
: the number of coils. This value must not be larger than the size of the array. Allowed data types:uint16_t
.
configureDiscreteInputs()
Tells the library where to read discrete input data and the number of discrete inputs. If this function is not run, the library will assume there are no discrete inputs.
modbusLogic.configureDiscreteInputs(discreteInputs, numDiscreteInputs)
modbusLogic
: aModbusSlaveLogic
object.
discreteInputs
: an array of discrete input values. Allowed data types: array ofbool
.
numDiscreteInputs
: the number of discrete inputs. This value must not be larger than the size of the array. Allowed data types:uint16_t
.
configureHoldingRegisters()
Tells the library where holding register data is stored and the number of holding registers. If this function is not run, the library will assume there are no holding registers.
modbusLogic.configureHoldingRegisters(holdingRegisters, numHoldingRegisters)
modbusLogic
: aModbusSlaveLogic
object.
holdingRegisters
: an array of holding register values. Allowed data types: array ofuint16_t
.
numHoldingRegisters
: the number of holding registers. This value must not be larger than the size of the array. Allowed data types:uint16_t
.
configureInputRegisters()
Tells the library where to read input register data and the number of input registers. If this function is not run, the library will assume there are no input registers.
modbusLogic.configureInputRegisters(inputRegisters, numInputRegisters)
modbusLogic
: aModbusSlaveLogic
object.
inputRegisters
: an array of input register values. Allowed data types: array ofuint16_t
.
numInputRegisters
: the number of input registers. This value must not be larger than the size of the array. Allowed data types:uint16_t
.
processPdu()
If a valid write request is submitted in the adu, it will update the appropriate data array, and modify the adu to be the correct acknowledgment response. If a valid read request is submitted, it will modify the adu to be the correct response with the requested data from the appropriate array. If an invalid request is submitted, it will modify the adu to be an exception response as per the Modbus specification.
modbusLogic.processPdu(adu)
modbusLogic
: aModbusSlaveLogic
object.
adu
: aModbusADU
object.