modbusMaster class

The class for communicating with modbus devices.

Low level functions

static byte responseBuffer
The response buffer for incoming messages from the Modbus slave.
bool getRegisters(byte readCommand, int16_t startRegister, int16_t numRegisters) -> bool
Get data from either holding or input registers.
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).
int sendCommand(byte[] command, int commandLength) -> int
Send a command to 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.

Constructors and Begins

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

bool begin(byte modbusSlaveID, Stream* stream, int8_t enablePin = -1) -> bool
This function sets up the communication with the Arduino stream connected to the modbus device.
bool begin(byte modbusSlaveID, Stream& stream, int8_t enablePin = -1) -> bool
This function sets up the communication with the Arduino stream connected to the modbus device.

High level register fetching functions

These higher-level functions return a variety of data from a single or group of input or holding registers

uint16_t uint16FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> uint16_t
Get the numbered input or holding register and return it as an uint16_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.
float float32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> float
Get two input or holding registers starting at the specifiec number and return them as a 32-bit float.
uint32_t uint32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> uint32_t
Get two input or holding registers starting at the specifiec number and return them as an uint32_t.
int32_t int32FromRegister(byte regType, int regNum, endianness endian = bigEndian) -> int32_t
Get two input or holding registers starting at the specifiec number and return them as an int32_t.
uint32_t TAI64FromRegister(byte regType, int regNum) -> uint32_t
Get four input or holding registers starting at the specifiec number, convert them to a TAI64 (64-bit timestamp), and return the lower 32-bits as a unix timestamp.
uint32_t TAI64NFromRegister(byte regType, int regNum, uint32_t& nanoseconds) -> uint32_t
Get six input or holding registers starting at the specifiec 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 TAI64NAFromRegister(byte regType, int regNum, uint32_t& nanoseconds, uint32_t& attoseconds) -> uint32_t
Get eight input or holding registers starting at the specifiec 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.
byte byteFromRegister(byte regType, int regNum, int byteNum) -> byte
Get the numbered input or holding register and return one byte of it.
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 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.
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.
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.

High level register setting functions

These higher-level functions set data in input registers to a variety of data types

bool uint16ToRegister(int regNum, uint16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to a uint16_t.
bool int16ToRegister(int regNum, int16_t value, endianness endian = bigEndian, bool forceMultiple = false) -> bool
Set a holding register to an int16_t.
bool float32ToRegister(int regNum, float value, endianness endian = bigEndian) -> bool
Set two holding registers to a 32-bit float.
bool uint32ToRegister(int regNum, uint32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to an uint32_t.
bool int32ToRegister(int regNum, int32_t value, endianness endian = bigEndian) -> bool
Set two holding registers to an int32_t.
bool TAI64ToRegister(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 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 byteToRegister(int regNum, int byteNum, byte value, bool forceMultiple = false) -> bool
Set one byte of a holding register.
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 StringToRegister(int regNum, String value, bool forceMultiple = false) -> bool
Set a series of holding registers to the characters in a String.
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.

Mid-level data frame result fetching functions

These mid-level functions return a variety of data from an input modbus "frame." Currently, the only "frame" available is the response buffer. Using these functions will be helpful if you wish to decrease the serial traffic by sending one "getRegisters" request for many registers and then parse that result into many different results.

uint16_t uint16FromFrame(endianness endian = bigEndian, int start_index = 3) -> uint16_t
Read an uint16_t out of the response buffer frame.
int16_t int16FromFrame(endianness endian = bigEndian, int start_index = 3) -> int16_t
Read an int16_t out of the response buffer frame.
float float32FromFrame(endianness endian = bigEndian, int start_index = 3) -> float
Read a 32-bit float out of the response buffer frame.
uint32_t uint32FromFrame(endianness endian = bigEndian, int start_index = 3) -> uint32_t
Read an uint32_t out of the response buffer frame.
int32_t int32FromFrame(endianness endian = bigEndian, int start_index = 3) -> int32_t
Read an int32_t out of the response buffer frame.
uint32_t TAI64FromFrame(int start_index = 3) -> uint32_t
Read a TAI64 (64-bit timestamp) out of the response buffer frame and return the lower 32-bits as a unix timestamp.
uint32_t TAI64NFromFrame(uint32_t& nanoseconds, int start_index = 3) -> uint32_t
Read a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) out of the response buffer frame and return an equivalent 32-bits unix timestamp.
uint32_t TAI64NAFromFrame(uint32_t& nanoseconds, uint32_t& attoseconds, int start_index = 3) -> uint32_t
Read a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count) out of the response buffer frame and return an equivalent 32-bits unix timestamp.
byte byteFromFrame(int start_index = 3) -> byte
Read a single byte out of the response buffer frame.
uint16_t pointerFromFrame(endianness endian = bigEndian, int start_index = 3) -> uint16_t
Read a 16-bit pointer out of the response buffer frame.
int8_t pointerTypeFromFrame(endianness endian = bigEndian, int start_index = 3) -> int8_t
Read a 16-bit pointer out of the response buffer frame.
String StringFromFrame(int charLength, int start_index = 3) -> String
Read a String out of the response buffer frame.
void charFromFrame(char[] outChar, int charLength, int start_index = 3)
Read characters out of the response buffer frame and put them into a character array.

Data frame setting functions

These insert values into a longer modbus data frame. These are useful in creating a single long frame which can be sent out in one "setRegisters" command.

void uint16ToFrame(uint16_t value, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert an uint16_t into the working byte frame.
void int16ToFrame(int16_t value, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert an int16_t into the working byte frame.
void float32ToFrame(float value, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert a 32-bit float into the working byte frame.
void uint32ToFrame(uint32_t value, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert an uint32_t into the working byte frame.
void int32ToFrame(int32_t value, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert an int32_t into the working byte frame.
void TAI64ToFrame(uint32_t seconds, byte[] modbusFrame, int start_index = 0)
Insert a TAI64 (64-bit timestamp) into the working byte frame.
void TAI64NToFrame(uint32_t seconds, uint32_t nanoseconds, byte[] modbusFrame, 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[] modbusFrame, 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.
void byteToFrame(byte value, int byteNum, byte[] modbusFrame, int start_index = 0)
Insert a single byte into the working byte frame.
void pointerToFrame(uint16_t value, pointerType point, endianness endian, byte[] modbusFrame, int start_index = 0)
Insert a 16-bit pointer into the working byte frame.
void StringToFrame(String value, byte[] modbusFrame, int start_index = 0)
Insert a String into the working byte frame.
void charToFrame(char[] inChar, int charLength, byte[] modbusFrame, int start_index = 0)
Insert a character array into the working byte frame.

Function documentation

bool modbusMaster::getRegisters(byte readCommand, int16_t startRegister, int16_t numRegisters)

Get data from either holding or input registers.

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.
Returns bool True if the modbus slave returned the expected number of register values; false if there was a failure.

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 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.

Input registers cannot be written by a Modbus controller/master


int 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 int 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 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

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

This function sets up the communication with the Arduino stream connected to the modbus device.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A pointer 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. Optional with a default value of -1.
Returns bool True if the starting communication was successful, false if not.

It should be run during the arduino "setup" function. 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!

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

This function sets up the communication with the Arduino stream connected to the modbus device.

Parameters
modbusSlaveID The byte identifier of the modbus slave device.
stream A 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. Optional with a default value of -1.
Returns bool True if the starting communication was successful, false if not.

It should be run during the arduino "setup" function. 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!

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

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

Parameters
regType The register type; use 0x03 for a holding register (read only) or 0x04 for an input register (read/write)
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.

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 only) or 0x04 for an input register (read/write)
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.

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

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

Parameters
regType The register type; use 0x03 for a holding register (read only) or 0x04 for an input register (read/write)
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.

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

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

Parameters
regType The register type; use 0x03 for a holding register (read only) or 0x04 for an input register (read/write)
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.

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

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

Parameters
regType The register type; use 0x03 for a holding register (read only) or 0x04 for an input register (read/write)
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.

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

Get four input or holding registers starting at the specifiec 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 only) or 0x04 for an input register (read/write)
regNum The number of the first of the four registers of interest.
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 specifiec 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 only) or 0x04 for an input register (read/write)
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::TAI64NAFromRegister(byte regType, int regNum, uint32_t& nanoseconds, uint32_t& attoseconds)

Get eight input or holding registers starting at the specifiec 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 only) or 0x04 for an input register (read/write)
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.

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 only) or 0x04 for an input register (read/write)
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.

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 only) or 0x04 for an input register (read/write)
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::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 only) or 0x04 for an input register (read/write)
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.


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 only) or 0x04 for an input register (read/write)
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.

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 only) or 0x04 for an input register (read/write)
regNum The number of the first of the registers of interest.
outChar 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::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::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::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::uint32ToRegister(int regNum, uint32_t value, endianness endian = bigEndian)

Set two holding registers to an 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::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::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::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::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::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::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::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 charactes 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, 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 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 charactes or less. Optional with a default value of false.
Returns bool True if the registers were successfully set, false if not.

uint16_t modbusMaster::uint16FromFrame(endianness endian = bigEndian, int start_index = 3)

Read an uint16_t out of the response buffer 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.
Returns uint16_t The uint16_t held in the buffer frame.

int16_t modbusMaster::int16FromFrame(endianness endian = bigEndian, int start_index = 3)

Read an int16_t out of the response buffer 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.
Returns int16_t The int16_t held in the buffer frame.

float modbusMaster::float32FromFrame(endianness endian = bigEndian, int start_index = 3)

Read a 32-bit float out of the response buffer 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.
Returns float The 32-bit float held in the buffer frame.

uint32_t modbusMaster::uint32FromFrame(endianness endian = bigEndian, int start_index = 3)

Read an uint32_t out of the response buffer 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.
Returns uint32_t The uint32_t held in the buffer frame.

int32_t modbusMaster::int32FromFrame(endianness endian = bigEndian, int start_index = 3)

Read an int32_t out of the response buffer 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.
Returns int32_t The int32_t held in the buffer frame.

uint32_t modbusMaster::TAI64FromFrame(int start_index = 3)

Read a TAI64 (64-bit timestamp) out of the response buffer 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.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NFromFrame(uint32_t& nanoseconds, int start_index = 3)

Read a TAI64N (64-bit timestamp followed by a 32-bit nanosecond count) out of the response buffer 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.
Returns uint32_t The equivalent 32-bit unix timestamp.

uint32_t modbusMaster::TAI64NAFromFrame(uint32_t& nanoseconds, uint32_t& attoseconds, int start_index = 3)

Read a TAI64NA (64-bit timestamp followed by a 32-bit nanosecond count and then a 32-bit attosecond count) out of the response buffer 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.
Returns uint32_t The equivalent 32-bit unix timestamp.

byte modbusMaster::byteFromFrame(int start_index = 3)

Read a single byte out of the response buffer frame.

Parameters
start_index The starting position of the byte in the response frame. Optional with a default of 3.
Returns byte The byte held in the buffer frame.

uint16_t modbusMaster::pointerFromFrame(endianness endian = bigEndian, int start_index = 3)

Read a 16-bit pointer out of the response buffer 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.
Returns uint16_t The 16-bit pointer held in the buffer frame.

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


int8_t modbusMaster::pointerTypeFromFrame(endianness endian = bigEndian, int start_index = 3)

Read a 16-bit pointer out of the response buffer 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 16-bit pointer in the response frame. Optional with a default of 3.
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.


String modbusMaster::StringFromFrame(int charLength, int start_index = 3)

Read a String out of the response buffer 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.
Returns String The text from the registers.

void modbusMaster::charFromFrame(char[] outChar, int charLength, int start_index = 3)

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

Parameters
outChar 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.

There is no return from this function.


void modbusMaster::uint16ToFrame(uint16_t value, endianness endian, byte[] modbusFrame, int start_index = 0)

Insert an 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.
modbusFrame The working byte frame
start_index The starting position of the uint16_t in the response frame. Optional with a default of 0.

void modbusMaster::int16ToFrame(int16_t value, endianness endian, byte[] modbusFrame, 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.
modbusFrame The working byte frame
start_index The starting position of the int16_t in the response frame. Optional with a default of 0.

void modbusMaster::float32ToFrame(float value, endianness endian, byte[] modbusFrame, 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.
modbusFrame The working byte frame
start_index The starting position of the 32-bit float in the response frame. Optional with a default of 0.

void modbusMaster::uint32ToFrame(uint32_t value, endianness endian, byte[] modbusFrame, int start_index = 0)

Insert an 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.
modbusFrame The working byte frame
start_index The starting position of the uint32_t in the response frame. Optional with a default of 0.

void modbusMaster::int32ToFrame(int32_t value, endianness endian, byte[] modbusFrame, 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.
modbusFrame The working byte frame
start_index The starting position of the int32_t in the response frame. Optional with a default of 0.

void modbusMaster::TAI64ToFrame(uint32_t seconds, byte[] modbusFrame, 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.
modbusFrame The working byte frame
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[] modbusFrame, 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.
modbusFrame The working byte frame
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[] modbusFrame, 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.
modbusFrame The working byte frame
start_index The starting position of the TAI64N in the response frame. Optional with a default of 0.

void modbusMaster::byteToFrame(byte value, int byteNum, byte[] modbusFrame, 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)
modbusFrame The working byte frame
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.


void modbusMaster::pointerToFrame(uint16_t value, pointerType point, endianness endian, byte[] modbusFrame, 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.
modbusFrame The working byte frame
start_index The starting position of the byte in the response frame. Optional with a default of 0.

void modbusMaster::StringToFrame(String value, byte[] modbusFrame, int start_index = 0)

Insert a String into the working byte frame.

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

void modbusMaster::charToFrame(char[] inChar, int charLength, byte[] modbusFrame, int start_index = 0)

Insert a character array into the working byte frame.

Parameters
inChar The character array to insert.
charLength The number of characters to copy from in the array.
modbusFrame The working byte frame
start_index The starting position of the byte in the response frame. Optional with a default of 0.


Variable documentation

static byte modbusMaster::responseBuffer

The response buffer for incoming messages from the Modbus slave.

This needs to be bigger than the largest response. For 8 parameters with 8 registers each: 64 registers * 2 bytes per register + 5 frame bytes