modbusMaster class

The class for communicating with modbus devices.

Internal Buffers

static byte responseBuffer
The response buffer for incoming messages from the Modbus slave.
static byte commandBuffer
The command buffer for outgoing messages to the Modbus slave.

Constructors and Begins

Functions to create the modbusMaster object and set up the communication with the Arduino stream connected to the modbus device.

modbusMaster()
Default constructor.
modbusMaster(byte modbusSlaveID, Stream* stream)
Construct a new modbus Master object.
modbusMaster(byte modbusSlaveID, Stream& stream)
Construct a new modbus Master object.
modbusMaster(byte modbusSlaveID, Stream* stream, int8_t enablePin)
Construct a new modbus Master object.
modbusMaster(byte modbusSlaveID, Stream& stream, int8_t enablePin)
Construct a new modbus Master object.
modbusMaster(Stream* stream)
Construct a new modbus Master object.
modbusMaster(Stream& stream)
Construct a new modbus Master object.
modbusMaster(Stream* stream, int8_t enablePin)
Construct a new modbus Master object.
modbusMaster(Stream& stream, int8_t enablePin)
Construct a new modbus Master object.
bool begin(Stream* stream) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(Stream& stream) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(Stream* stream, int8_t enablePin) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(Stream& stream, int8_t enablePin) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(byte modbusSlaveID, Stream* stream) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(byte modbusSlaveID, Stream& stream) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(byte modbusSlaveID, Stream* stream, int8_t enablePin) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.
bool begin(byte modbusSlaveID, Stream& stream, int8_t enablePin) -> bool
Equivalent to a constructor - used to assign members of the modbusMaster object.

Object setters and getters

Functions to set and get properties of the modbusMaster object.

void setSlaveID(byte slaveID)
Set the slave id.
byte getSlaveID() -> byte
Get the byte identifier of the modbus slave device.
void setEnablePin(int8_t enablePin)
Set the enable pin.
int8_t getEnablePin() -> int8_t
Get the enable pin.
void setCommandTimeout(uint32_t timeout)
Set the timeout to wait for a response from a sensor after a modbus command.
uint32_t getCommandTimeout() -> uint32_t
Get the command timeout.
void setFrameTimeout(uint32_t timeout)
Set the frame timeout - the time to wait between characters within a frame (in ms)
uint32_t getFrameTimeout() -> uint32_t
Get the frame timeout - the time to wait between characters within a frame (in ms)
void setCommandRetries(uint8_t retries)
Set the number of times to retry a command before giving up.
uint8_t getCommandRetries() -> uint8_t
Get the current number of times to retry a command before giving up.
void setStream(Stream* stream)
Set the stream for communication.
void setStream(Stream& stream)
Set the stream for communication.
Stream* getStream() -> Stream*
Get a pointer to the stream for communication.

Functions to get and set unsigned 16-bit integer (uint16_t) data

uint16_t uint16FromInputRegister(int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered input register and return it as a uint16_t.
uint16_t uint16FromHoldingRegister(int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered holding register and return it as a uint16_t.
uint16_t uint16FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered input or holding register and return it as a uint16_t.
bool uint16ToRegister(int regNum, uint16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to a uint16_t.
bool uint16ToHoldingRegister(int regNum, uint16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to a uint16_t.
uint16_t uint16FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> uint16_t
Read a uint16_t out of a modbus response frame.
void uint16ToFrame(uint16_t value, endianness endian, byte* destFrame, int start_index = 0)
Insert a uint16_t into the working byte frame.

Functions to get and set signed 16-bit integer (int16_t) data

int16_t int16FromInputRegister(int regNum, endianness endian = bigEndian) -> int16_t
Get the numbered input register and return it as an int16_t.
int16_t int16FromHoldingRegister(int regNum, endianness endian = bigEndian) -> int16_t
Get the numbered holding register and return it as an int16_t.
int16_t int16FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> int16_t
Get the numbered input or holding register and return it as an int16_t.
bool int16ToRegister(int regNum, int16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to an int16_t.
bool int16ToHoldingRegister(int regNum, int16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to an int16_t.
int16_t int16FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> int16_t
Read an int16_t out of a modbus response frame.
void int16ToFrame(int16_t value, endianness endian, byte* destFrame, int start_index = 0)
Insert an int16_t into the working byte frame.

Functions to get and set 32-bit float data

float float32FromInputRegister(int regNum, endianness endian = bigEndian) -> float
Get two input registers starting at the specified number and return them as a 32-bit float.
float float32FromHoldingRegister(int regNum, endianness endian = bigEndian) -> float
Get two holding registers starting at the specified number and return them as a 32-bit float.
float float32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> float
Get two input or holding registers starting at the specified number and return them as a 32-bit float.
bool float32ToRegister(int regNum, float value, endianness endian = bigEndian) -> bool
Set two holding registers to a 32-bit float.
bool float32ToHoldingRegister(int regNum, float value, endianness endian = bigEndian) -> bool
Set two holding registers to a 32-bit float.
float float32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> float
Read a 32-bit float out of a modbus response frame.
void float32ToFrame(float value, endianness endian, byte* destFrame, int start_index = 0)
Insert a 32-bit float into the working byte frame.

Functions to get and set unsigned 32-bit integer (uint32_t) data

uint32_t uint32FromInputRegister(int regNum, endianness endian = bigEndian) -> uint32_t
Get two input registers starting at the specified number and return them as a uint32_t.
uint32_t uint32FromHoldingRegister(int regNum, endianness endian = bigEndian) -> uint32_t
Get two holding registers starting at the specified number and return them as a uint32_t.
uint32_t uint32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> uint32_t
Get two input or holding registers starting at the specified number and return them as a uint32_t.
bool uint32ToRegister(int regNum, uint32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to a uint32_t.
bool uint32ToHoldingRegister(int regNum, uint32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to a uint32_t.
void uint32ToFrame(uint32_t value, endianness endian, byte* destFrame, int start_index = 0)
Insert a uint32_t into the working byte frame.
uint32_t uint32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> uint32_t
Read a uint32_t out of a modbus response frame.

Functions to get and set signed 32-bit integer (int32_t) data

int32_t int32FromInputRegister(int regNum, endianness endian = bigEndian) -> int32_t
Get two input registers starting at the specified number and return them as an int32_t.
int32_t int32FromHoldingRegister(int regNum, endianness endian = bigEndian) -> int32_t
Get two holding registers starting at the specified number and return them as an int32_t.
int32_t int32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> int32_t
Get two input or holding registers starting at the specified number and return them as an int32_t.
bool int32ToRegister(int regNum, int32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to an int32_t.
bool int32ToHoldingRegister(int regNum, int32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to an int32_t.
int32_t int32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> int32_t
Read an int32_t out of a modbus response frame.
void int32ToFrame(int32_t value, endianness endian, byte* destFrame, int start_index = 0)
Insert an int32_t into the working byte frame.

Functions to get and set bytes (half registers)

byte byteFromInputRegister(int regNum, int byteNum) -> byte
Get the numbered input register and return one byte of it.
byte byteFromHoldingRegister(int regNum, int byteNum) -> byte
Get the numbered holding register and return one byte of it.
byte byteFromRegister(byte regType, int regNum, int byteNum) -> byte
Get the numbered input or holding register and return one byte of it.
bool byteToRegister(int regNum, int byteNum, byte value, bool forceMultiple = false) -> bool
Set one byte of a holding register.
bool byteToHoldingRegister(int regNum, int byteNum, byte value, bool forceMultiple = false) -> bool
Set one byte of a holding register.
byte byteFromFrame(int start_index = 3, byte* sourceFrame = responseBuffer) -> byte
Read a single byte out of a modbus response frame.
void byteToFrame(byte value, int byteNum, byte* destFrame, int start_index = 0)
Insert a single byte into the working byte frame.

Functions to get and set 64-bit timestamp (TAI64) data

Common parameters for TAI64 reading functions:

  • nanoseconds - A reference to another uint32_t to populate with the nanoseconds - for TAI64N and TAI64NA
  • attoseconds - A reference to another uint32_t to populate with the attoseconds - for TAI64NA
  • return - The lower 32 bits of the TAI64 timestamp.

Common parameters for TAI64 writing functions:

  • seconds - The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
  • nanoseconds - The 32-bit nanosecond count.
  • attoseconds - The 32-bit attoseconds count.
uint32_t TAI64FromInputRegister(int regNum) -> uint32_t
Get four input registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.
uint32_t TAI64FromHoldingRegister(int regNum) -> uint32_t
Get four holding registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.
uint32_t TAI64FromRegister(byte regType, int regNum) -> uint32_t
Get four input or holding registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.
uint32_t TAI64NFromInputRegister(int regNum, uint32_t& nanoseconds) -> uint32_t
Get six input registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NFromHoldingRegister(int regNum, uint32_t& nanoseconds) -> uint32_t
Get six holding registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NFromRegister(byte regType, int regNum, uint32_t& nanoseconds) -> uint32_t
Get six input or holding registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NAFromInputRegister(int regNum, uint32_t& nanoseconds, uint32_t& attoseconds) -> uint32_t
Get eight input registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NAFromHoldingRegister(int regNum, uint32_t& nanoseconds, uint32_t& attoseconds) -> uint32_t
Get eight holding registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NAFromRegister(byte regType, int regNum, uint32_t& nanoseconds, uint32_t& attoseconds) -> uint32_t
Get eight input or holding registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.
bool TAI64ToRegister(int regNum, uint32_t seconds) -> bool
Set four holding registers to a TAI64 (64-bit timestamp)
bool TAI64ToHoldingRegister(int regNum, uint32_t seconds) -> bool
Set four holding registers to a TAI64 (64-bit timestamp)
bool TAI64NToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds) -> bool
Set six holding registers to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count)
bool TAI64NToHoldingRegister(int regNum, uint32_t seconds, uint32_t nanoseconds) -> bool
Set six holding registers to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count)
bool TAI64NAToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds) -> bool
Set eight holding registers to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count)
bool TAI64NAToHoldingRegister(int regNum, uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds) -> bool
Set eight holding registers to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count)
uint32_t TAI64FromFrame(int start_index = 3, byte* sourceFrame = responseBuffer) -> uint32_t
Read a TAI64 (64-bit timestamp) out of a modbus response frame and return the lower 32-bits as a unix timestamp.
uint32_t TAI64NFromFrame(uint32_t& nanoseconds, int start_index = 3, byte* sourceFrame = responseBuffer) -> uint32_t
Read a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) out of a modbus response frame and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NAFromFrame(uint32_t& nanoseconds, uint32_t& attoseconds, int start_index = 3, byte* sourceFrame = responseBuffer) -> uint32_t
Read a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count) out of a modbus response frame and return an equivalent 32-bits unix timestamp.
void TAI64ToFrame(uint32_t seconds, byte* destFrame, int start_index = 0)
Insert a TAI64 (64-bit timestamp) into the working byte frame.
void TAI64NToFrame(uint32_t seconds, uint32_t nanoseconds, byte* destFrame, int start_index = 0)
Insert a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) into the working byte frame.
void TAI64NAToFrame(uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds, byte* destFrame, int start_index = 0)
Insert a TAI64NA (64-bit timestamp followed by a 2-bit nanosecond count and then a 32-bit attosecond count) into the working byte frame.

Functions to get and set pointers to other registers

uint16_t pointerFromInputRegister(int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered input register and return it as an 16-bit pointer.
uint16_t pointerFromHoldingRegister(int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered holding register and return it as an 16-bit pointer.
uint16_t pointerFromRegister(byte regType, int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered input or holding register and return it as an 16-bit pointer.
int8_t pointerTypeFromInputRegister(int regNum, endianness endian = bigEndian) -> int8_t
Get the numbered input register and return it as a 8-bit pointer type.
int8_t pointerTypeFromHoldingRegister(int regNum, endianness endian = bigEndian) -> int8_t
Get the numbered holding register and return it as a 8-bit pointer type.
int8_t pointerTypeFromRegister(byte regType, int regNum, endianness endian = bigEndian) -> int8_t
Get the numbered input or holding register and return it as a 8-bit pointer type.
bool pointerToRegister(int regNum, uint16_t value, pointerType point, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to a 16-bit pointer.
bool pointerToHoldingRegister(int regNum, uint16_t value, pointerType point, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to a 16-bit pointer.
uint16_t pointerFromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> uint16_t
Read a 16-bit pointer out of a modbus response frame.
int8_t pointerTypeFromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer) -> int8_t
Read a 8-bit pointer type out of a modbus response frame.
void pointerToFrame(uint16_t value, pointerType point, endianness endian, byte* destFrame, int start_index = 0)
Insert a 16-bit pointer into the working byte frame.

Functions to get and set Strings

String StringFromInputRegister(int regNum, int charLength) -> String
Get a group of input registers, convert them to characters, combine them, and return a single String.
String StringFromHoldingRegister(int regNum, int charLength) -> String
Get a group of holding registers, convert them to characters, combine them, and return a single String.
String StringFromRegister(byte regType, int regNum, int charLength) -> String
Get a group of input or holding registers, convert them to characters, combine them, and return a single String.
bool StringToRegister(int regNum, String value, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a String.
bool StringToHoldingRegister(int regNum, String value, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a String.
String StringFromFrame(int charLength, int start_index = 3, byte* sourceFrame = responseBuffer) -> String
Read a String out of a modbus response frame.
void StringToFrame(String value, byte* destFrame, int start_index = 0)
Insert a String into the working byte frame.

Functions to get and set character arrays

void charFromInputRegister(int regNum, char* outChar, int charLength)
Get a group of input registers, convert them to characters and put them into the given character array.
void charFromHoldingRegister(int regNum, char* outChar, int charLength)
Get a group of holding registers, convert them to characters and put them into the given character array.
void charFromRegister(byte regType, int regNum, char* outChar, int charLength)
Get a group of input or holding registers, convert them to characters and put them into the given character array.
void charFromRegister(byte regType, int regNum, const char* outChar, int charLength)
Get a group of input or holding registers, convert them to characters and put them into the given character array.
void charFromInputRegister(int regNum, const char* outChar, int charLength)
Get a group of input registers, convert them to characters and put them into the given character array.
void charFromHoldingRegister(int regNum, const char* outChar, int charLength)
Get a group of holding registers, convert them to characters and put them into the given character array.
bool charToRegister(int regNum, char* inChar, int charLength, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a character array.
bool charToHoldingRegister(int regNum, char* inChar, int charLength, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a character array.
bool charToRegister(int regNum, const char* inChar, int charLength, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a character array.
bool charToHoldingRegister(int regNum, const char* inChar, int charLength, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a character array.
void charFromFrame(char* outChar, int charLength, int start_index = 3, byte* sourceFrame = responseBuffer)
Read characters out of a modbus response frame and put them into a character array.
void charFromFrame(const char* outChar, int charLength, int start_index = 3, byte* sourceFrame = responseBuffer)
Read characters out of a modbus response frame and put them into a character array.
void charToFrame(char* inChar, int charLength, byte* destFrame, int start_index = 0)
Insert a character array into the working byte frame.
void charToFrame(const char* inChar, int charLength, byte* destFrame, int start_index = 0)
Insert a character array into the working byte frame.

Data Getters

Functions to get data from one or more registers, coils, or discrete inputs.

int16_t getRegisters(byte readCommand, int16_t startRegister, int16_t numRegisters, byte* buff = responseBuffer) -> int16_t
Get data from either holding or input registers and copy the output to the supplied buffer.
bool getCoil(int16_t coilAddress) -> bool
Get the status of a single output coil.
int16_t getCoils(int16_t startCoil, int16_t numCoils, byte* buff = responseBuffer) -> int16_t
Get the data from a range of output coils. Each coil is a single bit.
bool getDiscreteInput(int16_t inputAddress) -> bool
Get the status of a single discrete input.
int16_t getDiscreteInputs(int16_t startInput, int16_t numInputs, byte* buff = responseBuffer) -> int16_t
Get a range of discrete inputs.

Data Setters

Functions to set data to one or more holding registers or coils.

bool setRegisters(int16_t startRegister, int16_t numRegisters, byte* value, bool forceMultiple = false) -> bool
Set the value of one or more holding registers using Modbus commands 0x06 or 0x10 (16).
bool setCoil(int16_t coilAddress, bool value) -> bool
Set the value of a single output coil using Modbus command 0x05.
bool setCoils(int16_t startCoil, int16_t numCoils, byte* value) -> bool
Set the value of one or more output coils using modbus command 0x0F.

Low level functions

int16_t getModbusData(byte slaveId, byte readCommand, int16_t startAddress, int16_t numChunks, uint8_t expectedReturnBytes = 0) -> int16_t
A generic get data function that can be used for any data type or size.
uint16_t sendCommand(byte* command, int commandLength) -> uint16_t
Send a command to the modbus slave.

Debugging functions

These are purely debugging functions to print out the raw hex data sent between the Arduino and the modbus slave.

void setDebugStream(Stream* stream)
Set a stream for debugging information to go to.
void setDebugStream(Stream& stream)
Set a stream for debugging information to go to.
void stopDebugging(void)
Un-set the stream for debugging information to go to; stop debugging.

Error functions

Functions to monitor the error codes.

modbusErrorCode getLastError(void) -> modbusErrorCode
Get last modbus error code.
void printLastError(void)
Prints information about the last error to the debugging stream.

Function documentation

modbusMaster::modbusMaster(byte modbusSlaveID, Stream* stream)

Construct a new modbus Master object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.

modbusMaster::modbusMaster(byte modbusSlaveID, Stream& stream)

Construct a new modbus Master object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.

modbusMaster::modbusMaster(byte modbusSlaveID, Stream* stream, int8_t enablePin)

Construct a new modbus Master object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.

modbusMaster::modbusMaster(byte modbusSlaveID, Stream& stream, int8_t enablePin)

Construct a new modbus Master object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.

modbusMaster::modbusMaster(Stream* stream)

Construct a new modbus Master object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.

modbusMaster::modbusMaster(Stream& stream)

Construct a new modbus Master object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.

modbusMaster::modbusMaster(Stream* stream, int8_t enablePin)

Construct a new modbus Master object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.

modbusMaster::modbusMaster(Stream& stream, int8_t enablePin)

Construct a new modbus Master object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.

bool modbusMaster::begin(Stream* stream)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
Returns bool Always returns true

bool modbusMaster::begin(Stream& stream)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
Returns bool Always returns true

bool modbusMaster::begin(Stream* stream, int8_t enablePin)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.
Returns bool Always returns true

bool modbusMaster::begin(Stream& stream, int8_t enablePin)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.
Returns bool Always returns true

bool modbusMaster::begin(byte modbusSlaveID, Stream* stream)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
Returns bool Always returns true

bool modbusMaster::begin(byte modbusSlaveID, Stream& stream)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
Returns bool Always returns true

bool modbusMaster::begin(byte modbusSlaveID, Stream* stream, int8_t enablePin)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.
Returns bool Always returns true

bool modbusMaster::begin(byte modbusSlaveID, Stream& stream, int8_t enablePin)

Equivalent to a constructor - used to assign members of the modbusMaster object.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer or reference to the Arduino stream object to communicate with.
enablePin A pin on the Arduino processor to use to send an enable signal to an RS485 to TTL adapter. Use a negative number if this does not apply.
Returns bool Always returns true

void modbusMaster::setSlaveID(byte slaveID)

Set the slave id.

Parameters
slaveID The byte identifier of the modbus slave device.

byte modbusMaster::getSlaveID()

Get the byte identifier of the modbus slave device.

Returns byte The byte identifier of the modbus slave device.

void modbusMaster::setEnablePin(int8_t enablePin)

Set the enable pin.

Parameters
enablePin The pin connected the the enable (direction control) of an RS485 to TTL adapter.

int8_t modbusMaster::getEnablePin()

Get the enable pin.

Returns int8_t The pin connected the the enable (direction control) of an RS485 to TTL adapter.

void modbusMaster::setCommandTimeout(uint32_t timeout)

Set the timeout to wait for a response from a sensor after a modbus command.

Parameters
timeout The timeout value in milliseconds.

By default, this is MODBUS_TIMEOUT (500 milliseconds).


uint32_t modbusMaster::getCommandTimeout()

Get the command timeout.

Returns uint32_t The command timeout value in milliseconds.

By default, this is MODBUS_TIMEOUT (500 milliseconds).


void modbusMaster::setFrameTimeout(uint32_t timeout)

Set the frame timeout - the time to wait between characters within a frame (in ms)

Parameters
timeout The timeout value in milliseconds.

The modbus protocol defines that there can be no more than 1.5 characters of silence between characters in a frame and any space over 3.5 characters defines a new frame.

By default, this is MODBUS_FRAME_TIMEOUT (4 milliseconds).


uint32_t modbusMaster::getFrameTimeout()

Get the frame timeout - the time to wait between characters within a frame (in ms)

Returns uint32_t The frame timeout value in milliseconds.

By default, this is MODBUS_FRAME_TIMEOUT (4 milliseconds).


void modbusMaster::setCommandRetries(uint8_t retries)

Set the number of times to retry a command before giving up.

Parameters
retries The number of times to retry a command before giving up

By default, this is 10.


uint8_t modbusMaster::getCommandRetries()

Get the current number of times to retry a command before giving up.

Returns uint8_t The number of times to retry a command before giving up

By default, this is 10.


void modbusMaster::setStream(Stream* stream)

Set the stream for communication.

Parameters
stream A pointer to the Arduino stream object to communicate with.

The "stream" device must be initialized prior to running this. Per modbus specifications, the stream must have:

  • 1 start bit
  • 8 data bits, least significant bit sent first
  • 1 stop bit if parity is used - 2 bits if no parity Note that neither SoftwareSerial, AltSoftSerial, nor NeoSoftwareSerial will support either even or odd parity!

void modbusMaster::setStream(Stream& stream)

Set the stream for communication.

Parameters
stream A reference to the Arduino stream object to communicate with.

Per modbus specifications, the stream must have:

  • 1 start bit
  • 8 data bits, least significant bit sent first
  • 1 stop bit if parity is used - 2 bits if no parity Note that neither SoftwareSerial, AltSoftSerial, nor NeoSoftwareSerial will support either even or odd parity!

Stream* modbusMaster::getStream()

Get a pointer to the stream for communication.

Returns Stream* A pointer to the Arduino stream object used for communication.

uint16_t modbusMaster::uint16FromInputRegister(int regNum, endianness endian = bigEndian)

Get the numbered input register and return it as a uint16_t.

Parameters
regNum The register number of interest.
endian The endianness of the uint16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The uint16_t held in the register.

uint16_t modbusMaster::uint16FromHoldingRegister(int regNum, endianness endian = bigEndian)

Get the numbered holding register and return it as a uint16_t.

Parameters
regNum The register number of interest.
endian The endianness of the uint16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The uint16_t held in the register.

uint16_t modbusMaster::uint16FromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get the numbered input or holding register and return it as a uint16_t.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The register number of interest.
endian The endianness of the uint16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The uint16_t held in the register.

bool modbusMaster::uint16ToRegister(int regNum, uint16_t value, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to a uint16_t.

Parameters
regNum The register number of interest.
value The value to set the register to.
endian The endianness used to write the uint16_t. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

bool modbusMaster::uint16ToHoldingRegister(int regNum, uint16_t value, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to a uint16_t.

Parameters
regNum The register number of interest.
value The value to set the register to.
endian The endianness used to write the uint16_t. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

uint16_t modbusMaster::uint16FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a uint16_t out of a modbus response frame.

Parameters
endian The endianness of the uint16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the uint16_t in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint16_t The uint16_t starting at the given byte index in the modbus frame.

void modbusMaster::uint16ToFrame(uint16_t value, endianness endian, byte* destFrame, int start_index = 0)

Insert a uint16_t into the working byte frame.

Parameters
value The value to add to the frame.
endian The endianness used to write the uint16_t. Optional with a default of big endian, which is required by modbus specifications.
destFrame The byte array to write to
start_index The starting position of the uint16_t in the response frame. Optional with a default of 0.

int16_t modbusMaster::int16FromInputRegister(int regNum, endianness endian = bigEndian)

Get the numbered input register and return it as an int16_t.

Parameters
regNum The register number of interest.
endian The endianness of the int16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int16_t The int16_t held in the register.

int16_t modbusMaster::int16FromHoldingRegister(int regNum, endianness endian = bigEndian)

Get the numbered holding register and return it as an int16_t.

Parameters
regNum The register number of interest.
endian The endianness of the int16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int16_t The int16_t held in the register.

int16_t modbusMaster::int16FromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get the numbered input or holding register and return it as an int16_t.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The register number of interest.
endian The endianness of the int16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int16_t The int16_t held in the register.

bool modbusMaster::int16ToRegister(int regNum, int16_t value, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to an int16_t.

Parameters
regNum The register number of interest.
value The value to set the register to.
endian The endianness used to write the int16_t. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

bool modbusMaster::int16ToHoldingRegister(int regNum, int16_t value, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to an int16_t.

Parameters
regNum The register number of interest.
value The value to set the register to.
endian The endianness used to write the int16_t. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

int16_t modbusMaster::int16FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read an int16_t out of a modbus response frame.

Parameters
endian The endianness of the int16_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the int16_t in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns int16_t The int16_t starting at the given byte index in the modbus frame.

void modbusMaster::int16ToFrame(int16_t value, endianness endian, byte* destFrame, int start_index = 0)

Insert an int16_t into the working byte frame.

Parameters
value The value to add to the frame.
endian The endianness used to write the int16_t. Optional with a default of big endian, which is required by modbus specifications.
destFrame The byte array to write to
start_index The starting position of the int16_t in the response frame. Optional with a default of 0.

float modbusMaster::float32FromInputRegister(int regNum, endianness endian = bigEndian)

Get two input registers starting at the specified number and return them as a 32-bit float.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns float The 32 bit float held in the register.

float modbusMaster::float32FromHoldingRegister(int regNum, endianness endian = bigEndian)

Get two holding registers starting at the specified number and return them as a 32-bit float.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns float The 32 bit float held in the register.

float modbusMaster::float32FromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get two input or holding registers starting at the specified number and return them as a 32-bit float.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the two registers of interest.
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns float The 32 bit float held in the register.

bool modbusMaster::float32ToRegister(int regNum, float value, endianness endian = bigEndian)

Set two holding registers to a 32-bit float.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::float32ToHoldingRegister(int regNum, float value, endianness endian = bigEndian)

Set two holding registers to a 32-bit float.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

float modbusMaster::float32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a 32-bit float out of a modbus response frame.

Parameters
endian The endianness of the 32-bit float in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the 32-bit float in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns float The 32-bit float starting at the given byte index in the modbus frame.

void modbusMaster::float32ToFrame(float value, endianness endian, byte* destFrame, int start_index = 0)

Insert a 32-bit float into the working byte frame.

Parameters
value The value to add to the frame.
endian The endianness used to write the 32-bit float. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
destFrame The byte array to write to
start_index The starting position of the 32-bit float in the response frame. Optional with a default of 0.

uint32_t modbusMaster::uint32FromInputRegister(int regNum, endianness endian = bigEndian)

Get two input registers starting at the specified number and return them as a uint32_t.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns uint32_t The uint32_t held in the register.

uint32_t modbusMaster::uint32FromHoldingRegister(int regNum, endianness endian = bigEndian)

Get two holding registers starting at the specified number and return them as a uint32_t.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns uint32_t The uint32_t held in the register.

uint32_t modbusMaster::uint32FromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get two input or holding registers starting at the specified number and return them as a uint32_t.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the two registers of interest.
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns uint32_t The uint32_t held in the register.

bool modbusMaster::uint32ToRegister(int regNum, uint32_t value, endianness endian = bigEndian)

Set two holding registers to a uint32_t.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::uint32ToHoldingRegister(int regNum, uint32_t value, endianness endian = bigEndian)

Set two holding registers to a uint32_t.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

void modbusMaster::uint32ToFrame(uint32_t value, endianness endian, byte* destFrame, int start_index = 0)

Insert a uint32_t into the working byte frame.

Parameters
value The value to add to the frame.
endian The endianness used to write the uint32_t. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
destFrame The byte array to write to
start_index The starting position of the uint32_t in the response frame. Optional with a default of 0.

uint32_t modbusMaster::uint32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a uint32_t out of a modbus response frame.

Parameters
endian The endianness of the uint32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the uint32_t in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint32_t The uint32_t starting at the given byte index in the modbus frame.

int32_t modbusMaster::int32FromInputRegister(int regNum, endianness endian = bigEndian)

Get two input registers starting at the specified number and return them as an int32_t.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns int32_t The int32_t held in the register.

int32_t modbusMaster::int32FromHoldingRegister(int regNum, endianness endian = bigEndian)

Get two holding registers starting at the specified number and return them as an int32_t.

Parameters
regNum The number of the first of the two registers of interest.
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns int32_t The int32_t held in the register.

int32_t modbusMaster::int32FromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get two input or holding registers starting at the specified number and return them as an int32_t.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the two registers of interest.
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns int32_t The int32_t held in the register.

bool modbusMaster::int32ToRegister(int regNum, int32_t value, endianness endian = bigEndian)

Set two holding registers to an int32_t.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::int32ToHoldingRegister(int regNum, int32_t value, endianness endian = bigEndian)

Set two holding registers to an int32_t.

Parameters
regNum The number of first of the two registers of interest.
value The value to set the register to.
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
Returns bool True if the registers were successfully set, false if not.

int32_t modbusMaster::int32FromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read an int32_t out of a modbus response frame.

Parameters
endian The endianness of the int32_t in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the int32_t in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns int32_t The int32_t starting at the given byte index in the modbus frame.

void modbusMaster::int32ToFrame(int32_t value, endianness endian, byte* destFrame, int start_index = 0)

Insert an int32_t into the working byte frame.

Parameters
value The value to add to the frame.
endian The endianness used to write the int32_t. Optional with a default of big endian, which is required by modbus specifications. Only big and little endian are supported. Mixed endianness is NOT supported.
destFrame The byte array to write to
start_index The starting position of the int32_t in the response frame. Optional with a default of 0.

byte modbusMaster::byteFromInputRegister(int regNum, int byteNum)

Get the numbered input register and return one byte of it.

Parameters
regNum The register number of interest.
byteNum The byte number to return (1 for upper or 2 for lower)
Returns byte The byte held in the register.

byte modbusMaster::byteFromHoldingRegister(int regNum, int byteNum)

Get the numbered holding register and return one byte of it.

Parameters
regNum The register number of interest.
byteNum The byte number to return (1 for upper or 2 for lower)
Returns byte The byte held in the register.

byte modbusMaster::byteFromRegister(byte regType, int regNum, int byteNum)

Get the numbered input or holding register and return one byte of it.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The register number of interest.
byteNum The byte number to return (1 for upper or 2 for lower)
Returns byte The byte held in the register.

bool modbusMaster::byteToRegister(int regNum, int byteNum, byte value, bool forceMultiple = false)

Set one byte of a holding register.

Parameters
regNum The register number of interest.
byteNum The byte number to set (1 for upper or 2 for lower)
value The value to set the byte to.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

The byte will be inserted as a full 16-bit register with the unused byte set to 0.


bool modbusMaster::byteToHoldingRegister(int regNum, int byteNum, byte value, bool forceMultiple = false)

Set one byte of a holding register.

Parameters
regNum The register number of interest.
byteNum The byte number to set (1 for upper or 2 for lower)
value The value to set the byte to.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

The byte will be inserted as a full 16-bit register with the unused byte set to 0.


byte modbusMaster::byteFromFrame(int start_index = 3, byte* sourceFrame = responseBuffer)

Read a single byte out of a modbus response frame.

Parameters
start_index The starting position of the byte in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns byte The byte starting at the given byte index in the modbus frame.

void modbusMaster::byteToFrame(byte value, int byteNum, byte* destFrame, int start_index = 0)

Insert a single byte into the working byte frame.

Parameters
value The byte to write
byteNum The byte number to set (1 for upper or 2 for lower)
destFrame The byte array to write to
start_index The starting position of the byte in the response frame. Optional with a default of 0.

The byte will be inserted as a 16-bit value with the unused byte set to 0.


uint32_t modbusMaster::TAI64FromInputRegister(int regNum)

Get four input registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.

Parameters
regNum The number of the first of the four registers of interest.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64FromHoldingRegister(int regNum)

Get four holding registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.

Parameters
regNum The number of the first of the four registers of interest.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64FromRegister(byte regType, int regNum)

Get four input or holding registers starting at the specified number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the four registers of interest.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NFromInputRegister(int regNum, uint32_t& nanoseconds)

Get six input registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regNum The number of the first of the six registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NFromHoldingRegister(int regNum, uint32_t& nanoseconds)

Get six holding registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regNum The number of the first of the six registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NFromRegister(byte regType, int regNum, uint32_t& nanoseconds)

Get six input or holding registers starting at the specified number, convert them to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the six registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NAFromInputRegister(int regNum, uint32_t& nanoseconds, uint32_t& attoseconds)

Get eight input registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regNum The number of the first of the eight registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
attoseconds A reference to another uint32_t to populate with the attoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NAFromHoldingRegister(int regNum, uint32_t& nanoseconds, uint32_t& attoseconds)

Get eight holding registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regNum The number of the first of the eight registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
attoseconds A reference to another uint32_t to populate with the attoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NAFromRegister(byte regType, int regNum, uint32_t& nanoseconds, uint32_t& attoseconds)

Get eight input or holding registers starting at the specified number, convert them to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count), and return an equivalent 32-bits unix timestamp.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the eight registers of interest.
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
attoseconds A reference to another uint32_t to populate with the attoseconds.
Returns uint32_t The equivalent 32-bit unix timestamp.

bool modbusMaster::TAI64ToRegister(int regNum, uint32_t seconds)

Set four holding registers to a TAI64 (64-bit timestamp)

Parameters
regNum The number of first of the four registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::TAI64ToHoldingRegister(int regNum, uint32_t seconds)

Set four holding registers to a TAI64 (64-bit timestamp)

Parameters
regNum The number of first of the four registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::TAI64NToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds)

Set six holding registers to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count)

Parameters
regNum The number of first of the six registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::TAI64NToHoldingRegister(int regNum, uint32_t seconds, uint32_t nanoseconds)

Set six holding registers to a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count)

Parameters
regNum The number of first of the six registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::TAI64NAToRegister(int regNum, uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds)

Set eight holding registers to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count)

Parameters
regNum The number of first of the eight registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
attoseconds The 32-bit attoseconds count.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::TAI64NAToHoldingRegister(int regNum, uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds)

Set eight holding registers to a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count)

Parameters
regNum The number of first of the eight registers of interest.
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
attoseconds The 32-bit attoseconds count.
Returns bool True if the registers were successfully set, false if not.

uint32_t modbusMaster::TAI64FromFrame(int start_index = 3, byte* sourceFrame = responseBuffer)

Read a TAI64 (64-bit timestamp) out of a modbus response frame and return the lower 32-bits as a unix timestamp.

Parameters
start_index The starting position of the TAI64 in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NFromFrame(uint32_t& nanoseconds, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) out of a modbus response frame and return an equivalent 32-bits unix timestamp.

Parameters
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
start_index The starting position of the TAI64N in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NAFromFrame(uint32_t& nanoseconds, uint32_t& attoseconds, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count) out of a modbus response frame and return an equivalent 32-bits unix timestamp.

Parameters
nanoseconds A reference to another uint32_t to populate with the nanoseconds.
attoseconds A reference to another uint32_t to populate with the attoseconds.
start_index The starting position of the TAI64NA in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint32_t The equivalent 32-bit unix timestamp.

void modbusMaster::TAI64ToFrame(uint32_t seconds, byte* destFrame, int start_index = 0)

Insert a TAI64 (64-bit timestamp) into the working byte frame.

Parameters
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
destFrame The byte array to write to
start_index The starting position of the TAI64 in the response frame. Optional with a default of 0.

void modbusMaster::TAI64NToFrame(uint32_t seconds, uint32_t nanoseconds, byte* destFrame, int start_index = 0)

Insert a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) into the working byte frame.

Parameters
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
destFrame The byte array to write to
start_index The starting position of the TAI64N in the response frame. Optional with a default of 0.

void modbusMaster::TAI64NAToFrame(uint32_t seconds, uint32_t nanoseconds, uint32_t attoseconds, byte* destFrame, int start_index = 0)

Insert a TAI64NA (64-bit timestamp followed by a 2-bit nanosecond count and then a 32-bit attosecond count) into the working byte frame.

Parameters
seconds The lower 32-bits of the timestamp. The upper 32-bits will always be set to 0x40000000, which will be the correct value until the year 2106.
nanoseconds The 32-bit nanosecond count.
attoseconds The 32-bit attoseconds count.
destFrame The byte array to write to
start_index The starting position of the TAI64N in the response frame. Optional with a default of 0.

uint16_t modbusMaster::pointerFromInputRegister(int regNum, endianness endian = bigEndian)

Get the numbered input register and return it as an 16-bit pointer.

Parameters
regNum The register number of interest.
endian The endianness of the 16-bit pointer in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The 16-bit pointer held in the register.

This should be a pointer to another registry address within the modbus registers.


uint16_t modbusMaster::pointerFromHoldingRegister(int regNum, endianness endian = bigEndian)

Get the numbered holding register and return it as an 16-bit pointer.

Parameters
regNum The register number of interest.
endian The endianness of the 16-bit pointer in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The 16-bit pointer held in the register.

This should be a pointer to another registry address within the modbus registers.


uint16_t modbusMaster::pointerFromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get the numbered input or holding register and return it as an 16-bit pointer.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The register number of interest.
endian The endianness of the 16-bit pointer in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns uint16_t The 16-bit pointer held in the register.

This should be a pointer to another registry address within the modbus registers.


int8_t modbusMaster::pointerTypeFromInputRegister(int regNum, endianness endian = bigEndian)

Get the numbered input register and return it as a 8-bit pointer type.

Parameters
regNum The register number of interest.
endian The endianness of the pointer type in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int8_t The 8-bit pointer type held in the register. This will be an object of type pointerType.

This should be the type of register pointed to by pointer contained within a different modbus register.


int8_t modbusMaster::pointerTypeFromHoldingRegister(int regNum, endianness endian = bigEndian)

Get the numbered holding register and return it as a 8-bit pointer type.

Parameters
regNum The register number of interest.
endian The endianness of the pointer type in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int8_t The 8-bit pointer type held in the register. This will be an object of type pointerType.

This should be the type of register pointed to by pointer contained within a different modbus register.


int8_t modbusMaster::pointerTypeFromRegister(byte regType, int regNum, endianness endian = bigEndian)

Get the numbered input or holding register and return it as a 8-bit pointer type.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The register number of interest.
endian The endianness of the pointer type in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
Returns int8_t The 8-bit pointer type held in the register. This will be an object of type pointerType.

This should be the type of register pointed to by pointer contained within a different modbus register.


bool modbusMaster::pointerToRegister(int regNum, uint16_t value, pointerType point, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to a 16-bit pointer.

Parameters
regNum The register number of interest.
value The value to set the register to.
point The type of the pointer, (pointerType) ie, which section of the modbus memory is being pointed to.
endian The endianness used to write the 16-bit pointer. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

bool modbusMaster::pointerToHoldingRegister(int regNum, uint16_t value, pointerType point, endianness endian = bigEndian, bool forceMultiple = false)

Set a holding register to a 16-bit pointer.

Parameters
regNum The register number of interest.
value The value to set the register to.
point The type of the pointer, (pointerType) ie, which section of the modbus memory is being pointed to.
endian The endianness used to write the 16-bit pointer. Optional with a default of big endian, which is required by modbus specifications.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). Optional with a default value of false.
Returns bool True if the register was successfully set, false if not.

uint16_t modbusMaster::pointerFromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a 16-bit pointer out of a modbus response frame.

Parameters
endian The endianness of the 16-bit pointer in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the 16-bit pointer in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns uint16_t The 16-bit pointer starting at the given byte index in the register array.

This should be a pointer to another registry address within the modbus registers.


int8_t modbusMaster::pointerTypeFromFrame(endianness endian = bigEndian, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a 8-bit pointer type out of a modbus response frame.

Parameters
endian The endianness of the pointer type in the modbus register. Optional with a default of big endian, which is required by modbus specifications.
start_index The starting position of the 8-bit pointer type in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns int8_t The 8-bit pointer type held in the buffer frame. This will be an object of type pointerType.

This should be the type of register pointed to by pointer contained within a different modbus register.


void modbusMaster::pointerToFrame(uint16_t value, pointerType point, endianness endian, byte* destFrame, int start_index = 0)

Insert a 16-bit pointer into the working byte frame.

Parameters
value The value of the 16-bit pointer
point The type of the pointer, (pointerType) ie, which section of the modbus memory is being pointed to.
endian The endianness used to write the 16-bit pointer. Optional with a default of big endian, which is required by modbus specifications.
destFrame The byte array to write to
start_index The starting position of the byte in the response frame. Optional with a default of 0.

String modbusMaster::StringFromInputRegister(int regNum, int charLength)

Get a group of input registers, convert them to characters, combine them, and return a single String.

Parameters
regNum The number of the first of the registers of interest.
charLength The number of characters to return. NOTE: There are TWO characters per register!
Returns String The text from the registers.

String modbusMaster::StringFromHoldingRegister(int regNum, int charLength)

Get a group of holding registers, convert them to characters, combine them, and return a single String.

Parameters
regNum The number of the first of the registers of interest.
charLength The number of characters to return. NOTE: There are TWO characters per register!
Returns String The text from the registers.

String modbusMaster::StringFromRegister(byte regType, int regNum, int charLength)

Get a group of input or holding registers, convert them to characters, combine them, and return a single String.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the registers of interest.
charLength The number of characters to return. NOTE: There are TWO characters per register!
Returns String The text from the registers.

bool modbusMaster::StringToRegister(int regNum, String value, bool forceMultiple = false)

Set a series of holding registers to the characters in a String.

Parameters
regNum The first of the registers of interest
value The String to set the registers to.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the String is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::StringToHoldingRegister(int regNum, String value, bool forceMultiple = false)

Set a series of holding registers to the characters in a String.

Parameters
regNum The first of the registers of interest
value The String to set the registers to.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the String is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

String modbusMaster::StringFromFrame(int charLength, int start_index = 3, byte* sourceFrame = responseBuffer)

Read a String out of a modbus response frame.

Parameters
charLength The number of characters to return.
start_index The starting position of the characters in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.
Returns String The text from the registers.

void modbusMaster::StringToFrame(String value, byte* destFrame, int start_index = 0)

Insert a String into the working byte frame.

Parameters
value The string to insert.
destFrame The byte array to write to
start_index The starting position of the byte in the response frame. Optional with a default of 0.

void modbusMaster::charFromInputRegister(int regNum, char* outChar, int charLength)

Get a group of input registers, convert them to characters and put them into the given character array.

Parameters
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


void modbusMaster::charFromHoldingRegister(int regNum, char* outChar, int charLength)

Get a group of holding registers, convert them to characters and put them into the given character array.

Parameters
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


void modbusMaster::charFromRegister(byte regType, int regNum, char* outChar, int charLength)

Get a group of input or holding registers, convert them to characters and put them into the given character array.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


void modbusMaster::charFromRegister(byte regType, int regNum, const char* outChar, int charLength)

Get a group of input or holding registers, convert them to characters and put them into the given character array.

Parameters
regType The register type; use 0x03 for a holding register (read/write) or 0x04 for an input register (read only)
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


void modbusMaster::charFromInputRegister(int regNum, const char* outChar, int charLength)

Get a group of input registers, convert them to characters and put them into the given character array.

Parameters
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


void modbusMaster::charFromHoldingRegister(int regNum, const char* outChar, int charLength)

Get a group of holding registers, convert them to characters and put them into the given character array.

Parameters
regNum The number of the first of the registers of interest.
outChar A pointer or constant pointer to a character array to fill with the content of the registers.
charLength The number of characters to return. NOTE: There are TWO characters per register!

There is no return from this function.


bool modbusMaster::charToRegister(int regNum, char* inChar, int charLength, bool forceMultiple = false)

Set a series of holding registers to the characters in a character array.

Parameters
regNum The first of the registers of interest
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to set from in the array.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the character array is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::charToHoldingRegister(int regNum, char* inChar, int charLength, bool forceMultiple = false)

Set a series of holding registers to the characters in a character array.

Parameters
regNum The first of the registers of interest
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to set from in the array.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the character array is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::charToRegister(int regNum, const char* inChar, int charLength, bool forceMultiple = false)

Set a series of holding registers to the characters in a character array.

Parameters
regNum The first of the registers of interest
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to set from in the array.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the character array is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

bool modbusMaster::charToHoldingRegister(int regNum, const char* inChar, int charLength, bool forceMultiple = false)

Set a series of holding registers to the characters in a character array.

Parameters
regNum The first of the registers of interest
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to set from in the array.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if the character array is two characters or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

void modbusMaster::charFromFrame(char* outChar, int charLength, int start_index = 3, byte* sourceFrame = responseBuffer)

Read characters out of a modbus response frame and put them into a character array.

Parameters
outChar A pointer to a character array to fill with the content of the response buffer.
charLength The number of characters to return.
start_index The starting position of the characters in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.

There is no return from this function.


void modbusMaster::charFromFrame(const char* outChar, int charLength, int start_index = 3, byte* sourceFrame = responseBuffer)

Read characters out of a modbus response frame and put them into a character array.

Parameters
outChar A pointer to a character array to fill with the content of the response buffer.
charLength The number of characters to return.
start_index The starting position of the characters in the response frame. Optional with a default of 3.
sourceFrame The byte array to read from. Optional with a default of the built in response buffer.

There is no return from this function.


void modbusMaster::charToFrame(char* inChar, int charLength, byte* destFrame, int start_index = 0)

Insert a character array into the working byte frame.

Parameters
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to copy from in the array.
destFrame The byte array to write to
start_index The starting position of the byte in the response frame. Optional with a default of 0.

void modbusMaster::charToFrame(const char* inChar, int charLength, byte* destFrame, int start_index = 0)

Insert a character array into the working byte frame.

Parameters
inChar A pointer or constant pointer to the character array to set the registers to.
charLength The number of characters to copy from in the array.
destFrame The byte array to write to
start_index The starting position of the byte in the response frame. Optional with a default of 0.

int16_t modbusMaster::getRegisters(byte readCommand, int16_t startRegister, int16_t numRegisters, byte* buff = responseBuffer)

Get data from either holding or input registers and copy the output to the supplied buffer.

Parameters
readCommand The command to use to read data. For a holding register readCommand = 0x03. For an input register readCommand = 0x04.
startRegister The starting register number.
numRegisters The number of registers to read.
buff The buffer to copy the output data to.
Returns int16_t Zero if the response didn't return the expected number of bytes or if there was an error in the modbus response; otherwise, the number of bytes in the response.
  • If you provide a buffer, this command puts only the content of the registers into the buffers. It does not add the full returned modbus frame. The data in the provided buffer will be stripped of the modbus protocol characters.
  • If no buffer is provided, the full modbus response is put into the internal library buffer and is not stripped of the modbus protocol characters.

bool modbusMaster::getCoil(int16_t coilAddress)

Get the status of a single output coil.

Parameters
coilAddress The address of the coil to read.
Returns bool The status of the coil (true for ON, false for OFF).

The read command for output coils is 0x01.


int16_t modbusMaster::getCoils(int16_t startCoil, int16_t numCoils, byte* buff = responseBuffer)

Get the data from a range of output coils. Each coil is a single bit.

Parameters
startCoil The starting coil number.
numCoils The number of coils to read.
buff A pre-allocated buffer to store the retrieved coil values.
Returns int16_t Zero if the response didn't return the expected number of bytes or if there was an error in the modbus response; otherwise, the number of bytes in the response.

The read command for output coils is 0x01.

  • If you provide a buffer, this command puts only the content of the coils into the buffers. It does not add the full returned modbus frame. The data in the provided buffer will be stripped of the modbus protocol characters.
  • If no buffer is provided, the full modbus response is left in the internal library buffer and is not stripped of the modbus protocol characters.

bool modbusMaster::getDiscreteInput(int16_t inputAddress)

Get the status of a single discrete input.

Parameters
inputAddress The address of the discrete input to read.
Returns bool The status of the discrete input (true for ON, false for OFF).

int16_t modbusMaster::getDiscreteInputs(int16_t startInput, int16_t numInputs, byte* buff = responseBuffer)

Get a range of discrete inputs.

Parameters
startInput The starting input number.
numInputs The number of discrete inputs to read.
buff A pre-allocated buffer to store the retrieved coil values.
Returns int16_t Zero if the response didn't return the expected number of bytes or if there was an error in the modbus response; otherwise, the number of bytes in the response.
  • If you provide a buffer, this command puts only the content of the discrete inputs into the buffers. It does not add the full returned modbus frame. The data in the provided buffer will be stripped of the modbus protocol characters.
  • If no buffer is provided, the full modbus response is put into the internal library buffer and is not stripped of the modbus protocol characters.

bool modbusMaster::setRegisters(int16_t startRegister, int16_t numRegisters, byte* value, bool forceMultiple = false)

Set the value of one or more holding registers using Modbus commands 0x06 or 0x10 (16).

Parameters
startRegister The starting register number.
numRegisters The number of registers to write.
value A pointer to the byte array with the values to write.
forceMultiple Set the forceMultiple boolean flag to 'true' to force the use of the Modbus command for setting multiple resisters (0x10). This only applies if a single register is being set. Optional with a default value of false.
Returns bool True if the modbus slave returned the expected number of input values; false if there was a failure.

Input registers cannot be written by a Modbus controller/master


bool modbusMaster::setCoil(int16_t coilAddress, bool value)

Set the value of a single output coil using Modbus command 0x05.

Parameters
coilAddress The address of the coil to set.
value The value to set the coil to (true for ON, false for OFF).
Returns bool True if the proper modbus slave correctly responded to the command; false otherwise.

Output coils are single-bit values that can be either ON (1) or OFF (0). Input (discrete) contacts cannot be written by a Modbus controller/master.


bool modbusMaster::setCoils(int16_t startCoil, int16_t numCoils, byte* value)

Set the value of one or more output coils using modbus command 0x0F.

Parameters
startCoil The address of the first coil to set.
numCoils The number of coils to set.
value A pointer to a byte array containing the values to set the coils to.
Returns bool True if the proper modbus slave correctly responded to the command; false otherwise.

Input (discrete) contacts cannot be written by a Modbus controller/master.


int16_t modbusMaster::getModbusData(byte slaveId, byte readCommand, int16_t startAddress, int16_t numChunks, uint8_t expectedReturnBytes = 0)

A generic get data function that can be used for any data type or size.

Parameters
slaveId The modbus slave ID of the device to communicate with.
readCommand The command to use to read data
startAddress The first address to read from
numChunks The number of chunks of data to read
expectedReturnBytes The expected return size in bytes- set to 0 to have the size calculated based on the command and the number of chunks requested.
Returns int16_t Zero if the response didn't return the expected number of bytes or if there was an error in the modbus response; otherwise, the number of bytes in the response.

Use these commands:

  • For a coil readCommand = 0x01
  • For a discrete input readCommand = 0x02
  • For a holding register readCommand = 0x03
  • For an input register readCommand = 0x04.

A register command will return 2 bytes per register - the size should be number registers x 2.

A coil or discrete input command will return 1 bit per coil - the size should be number coils / 8.


uint16_t modbusMaster::sendCommand(byte* command, int commandLength)

Send a command to the modbus slave.

Parameters
command The fully formed command to send to the Modbus slave.
commandLength The length of the outgoing command.
Returns uint16_t The number of bytes received from the Modbus slave.

This is the lowest level function.

This takes a command, adds the proper CRC, sends it to the sensor bus, and listens for a response.

If it receives a response from the correct slave with the correct CRC, it returns the number of bytes received and put into the responseBuffer.

If it receives a response from the wrong slave, an incorrect CRC, or an exception, it will print notice of the error to the debugging stream and return 0xn0FF where n is the error code.

If no response is received, this returns 0.


void modbusMaster::setDebugStream(Stream* stream)

Set a stream for debugging information to go to.

Parameters
stream An Arduino stream object

void modbusMaster::setDebugStream(Stream& stream)

Set a stream for debugging information to go to.

Parameters
stream An Arduino stream object

modbusErrorCode modbusMaster::getLastError(void)

Get last modbus error code.

Returns modbusErrorCode The last modbus error code

void modbusMaster::printLastError(void)

Prints information about the last error to the debugging stream.