Skip to content

This is an Arduino library for processing Modbus requests.

License

Notifications You must be signed in to change notification settings

CMB27/ModbusSlaveLogic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ModbusSlaveLogic

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.

Methods

ModbusSlaveLogic

Description

Creates a ModbusSlaveLogic object.

Example

# include <ModbusSlaveLogic.h>

ModbusSlaveLogic modbusLogic;
configureCoils()

Description

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.

Syntax

modbusLogic.configureCoils(coils, numCoils)

Parameters

  • modbusLogic: a ModbusSlaveLogic object.

  • coils: an array of coil values. Allowed data types: array of bool.

  • numCoils: the number of coils. This value must not be larger than the size of the array. Allowed data types: uint16_t.

configureDiscreteInputs()

Description

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.

Syntax

modbusLogic.configureDiscreteInputs(discreteInputs, numDiscreteInputs)

Parameters

  • modbusLogic: a ModbusSlaveLogic object.

  • discreteInputs: an array of discrete input values. Allowed data types: array of bool.

  • numDiscreteInputs: the number of discrete inputs. This value must not be larger than the size of the array. Allowed data types: uint16_t.

configureHoldingRegisters()

Description

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.

Syntax

modbusLogic.configureHoldingRegisters(holdingRegisters, numHoldingRegisters)

Parameters

  • modbusLogic: a ModbusSlaveLogic object.

  • holdingRegisters: an array of holding register values. Allowed data types: array of uint16_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()

Description

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.

Syntax

modbusLogic.configureInputRegisters(inputRegisters, numInputRegisters)

Parameters

  • modbusLogic: a ModbusSlaveLogic object.

  • inputRegisters: an array of input register values. Allowed data types: array of uint16_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()

Description

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.

Syntax

modbusLogic.processPdu(adu)

Parameters

  • modbusLogic: a ModbusSlaveLogic object.

  • adu: a ModbusADU object.