modbuspp  1.1.40
C++ wrapper for the libmodbus library
Modbus::Slave Class Reference

Slave connected to Modbus. More...

#include <slave.h>

Inheritance diagram for Modbus::Slave:
Inheritance graph

Public Member Functions

int dataAddress (int addr) const
 returns the address in the MODBUS data model corresponding to a PDU address. More...
 
Devicedevice () const
 returns the device used to access the network
 
bool isOpen () const
 returns true if isValid() and device() is opened.
 
bool isValid () const
 returns true if device() and number() are sets
 
int number () const
 Get slave number. More...
 
int pduAddress (int addr) const
 returns the PDU address corresponding to an address in the MODBUS data model. More...
 
bool pduAddressing () const
 Modbus addressing mode. More...
 
int readCoil (int addr, bool &dest)
 Read a single coil (bit) More...
 
virtual int readCoils (int addr, bool *dest, int nb=1)
 Read many coils (bits) More...
 
int readDiscreteInput (int addr, bool &dest)
 Read a single discrete input (input bit) More...
 
virtual int readDiscreteInputs (int addr, bool *dest, int nb=1)
 Read many discrete inputs (input bits) More...
 
int readInputRegister (int addr, uint16_t &dest)
 Read a single input register. More...
 
template<typename T , Endian e>
int readInputRegister (int addr, Data< T, e > &dest)
 Read a single input data. More...
 
virtual int readInputRegisters (int addr, uint16_t *dest, int nb=1)
 Read many input registers. More...
 
template<typename T , Endian e>
int readInputRegisters (int addr, Data< T, e > *dest, int nb=1)
 Read many input data. More...
 
int readRegister (int addr, uint16_t &dest)
 Read a single register. More...
 
template<typename T , Endian e>
int readRegister (int addr, Data< T, e > &dest)
 Read a single holding data. More...
 
virtual int readRegisters (int addr, uint16_t *dest, int nb=1)
 Read many registers. More...
 
template<typename T , Endian e>
int readRegisters (int addr, Data< T, e > *dest, int nb=1)
 Read many holding data. More...
 
virtual int reportSlaveId (uint16_t max_dest, uint8_t *dest)
 returns a description of the controller More...
 
template<typename T , Endian e = EndianBig>
int reportSlaveId (SlaveReport< T, e > &dest)
 returns a description of the controller More...
 
void setDevice (Device *dev)
 Sets the device used to access the network.
 
void setNumber (int n)
 Sets the slave number.
 
void setPduAddressing (bool pduAddressing=true)
 Set the Modbus addressing mode. More...
 
 Slave (int slaveAddr, Device *dev)
 Constructor.
 
 Slave ()
 Default Constructor. More...
 
virtual int writeCoil (int addr, bool src)
 Write a single coil (bit) More...
 
virtual int writeCoils (int addr, const bool *src, int nb)
 Write many coils (bits) More...
 
virtual int writeReadRegisters (int write_addr, const uint16_t *src, int write_nb, int read_addr, uint16_t *dest, int read_nb)
 Write and read many registers in a single transaction. More...
 
virtual int writeRegister (int addr, uint16_t value)
 Write a single register. More...
 
template<typename T , Endian e>
int writeRegister (int addr, Data< T, e > &value)
 Write a single holding data. More...
 
virtual int writeRegisters (int addr, const uint16_t *src, int nb)
 Write many registers. More...
 
template<typename T , Endian e>
int writeRegisters (int addr, Data< T, e > *src, int nb=1)
 Write many holding data. More...
 
virtual ~Slave ()
 Destructor. More...
 

Static Public Member Functions

static void setBoolArray (bool *dest, const uint8_t *src, size_t n)
 Set many booleans from an array of bytes. More...
 

Detailed Description

Constructor & Destructor Documentation

◆ Slave()

Modbus::Slave::Slave ( )

Default Constructor.

object cannot be used without calling setDevice() and setNumber()

Referenced by reportSlaveId().

Here is the caller graph for this function:

◆ ~Slave()

virtual Modbus::Slave::~Slave ( )
virtual

Destructor.

The destructor closes the connection if it is open and releases all affected resources.

Member Function Documentation

◆ dataAddress()

int Modbus::Slave::dataAddress ( int  addr) const

returns the address in the MODBUS data model corresponding to a PDU address.

If the PDU addressing mode is not enabled (which is the case by default), the data model address is equal to the PDU address plus 1, otherwise they are equal.

◆ number()

◆ pduAddress()

int Modbus::Slave::pduAddress ( int  addr) const

returns the PDU address corresponding to an address in the MODBUS data model.

If the PDU addressing mode is not enabled (which is the case by default), the PDU address is equal to the MODBUS address minus 1, otherwise they are equal.

◆ pduAddressing()

bool Modbus::Slave::pduAddressing ( ) const

Modbus addressing mode.

This function shall return the Modbus addressing mode used. The address mode used is, by default, that of the data model, that is to say, a numbering of the registers from 1 to n.

The Modbus application protocol defines precisely PDU addressing rules. In a Modbus PDU each data is addressed from 0 to 65535.

It also defines clearly a Modbus data model composed of 4 blocks that comprises several elements numbered from 1 to n.

In the Modbus data Model each element within a data block is numbered from 1 to n.

Afterwards the Modbus data model has to be bound to the device application (IEC -61131 object, or other application model).

Returns
true for Modbus PDU adressing
See also
setPduAddressing()

◆ readCoil()

int Modbus::Slave::readCoil ( int  addr,
bool &  dest 
)
inline

Read a single coil (bit)

This function shall read a signle bit (coil) to the address addr of the device. The result of reading is stored in dest as boolean.

The function uses the Modbus function code 0x01 (read coil status).

Returns
1 if successful. Otherwise it shall return -1 and set errno.
Examples:
server/clock-server-json/main.cpp, and server/clock-server/main.cpp.

References readCoils().

Here is the call graph for this function:

◆ readCoils()

virtual int Modbus::Slave::readCoils ( int  addr,
bool *  dest,
int  nb = 1 
)
virtual

Read many coils (bits)

This function shall read the status of the nb bits (coils) to the address addr of the remote device. The result of reading is stored in dest array as boolean.

The function uses the Modbus function code 0x01 (read coil status).

Returns
the number of read bits if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Examples:
master/read-coils/main.cpp.

Referenced by readCoil().

Here is the caller graph for this function:

◆ readDiscreteInput()

int Modbus::Slave::readDiscreteInput ( int  addr,
bool &  dest 
)
inline

Read a single discrete input (input bit)

This function shall read a single input bits to the address addr of the device. The result of reading is stored in dest as boolean.

The function uses the Modbus function code 0x02 (read input status).

Returns
1 if successful. Otherwise it shall return -1 and set errno.

References readDiscreteInputs().

Here is the call graph for this function:

◆ readDiscreteInputs()

virtual int Modbus::Slave::readDiscreteInputs ( int  addr,
bool *  dest,
int  nb = 1 
)
virtual

Read many discrete inputs (input bits)

This function shall read the content of the nb input bits to the address addr of the remote device. The result of reading is stored in dest array as boolean.

The function uses the Modbus function code 0x02 (read input status).

Returns
the number of read input status if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Referenced by readDiscreteInput().

Here is the caller graph for this function:

◆ readInputRegister() [1/2]

int Modbus::Slave::readInputRegister ( int  addr,
uint16_t &  dest 
)
inline

Read a single input register.

This function shall read a single input register to the address addr of the device.

The result of reading is stored in dest as word values (16 bits).

The function uses the Modbus function code 0x04 (read input registers). The holding registers and input registers have different historical meaning, but nowadays it's more common to use holding registers only.

Returns
1 if successful. Otherwise it shall return -1 and set errno.

References readInputRegisters().

Referenced by Modbus::BufferedSlave::writeInputRegister().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readInputRegister() [2/2]

template<typename T , Endian e>
int Modbus::Slave::readInputRegister ( int  addr,
Data< T, e > &  dest 
)
inline

Read a single input data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall read a single input data to the address addr of the device.

The result of reading is stored in dest as T value.

The function uses the Modbus function code 0x04 (read input registers).

Returns
return the number of read input Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References readInputRegisters(), and Modbus::Data< T, e >::registers().

Here is the call graph for this function:

◆ readInputRegisters() [1/2]

virtual int Modbus::Slave::readInputRegisters ( int  addr,
uint16_t *  dest,
int  nb = 1 
)
virtual

Read many input registers.

This function shall read the content of the nb input registers to the address addr of the remote device.

The result of reading is stored in dest array as word values (16 bits).

The function uses the Modbus function code 0x04 (read input registers). The holding registers and input registers have different historical meaning, but nowadays it's more common to use holding registers only.

Returns
return the number of read input registers if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Examples:
master/read-input-registers/main.cpp.

Referenced by readInputRegister(), readInputRegisters(), and Modbus::BufferedSlave::writeInputRegister().

Here is the caller graph for this function:

◆ readInputRegisters() [2/2]

template<typename T , Endian e>
int Modbus::Slave::readInputRegisters ( int  addr,
Data< T, e > *  dest,
int  nb = 1 
)
inline

Read many input data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall read the content of the nb input data to the address addr of the device.

The result of reading is stored in dest array as T values.

The function uses the Modbus function code 0x04 (read input registers).

Returns
return the number of read input Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References readInputRegisters().

Here is the call graph for this function:

◆ readRegister() [1/2]

int Modbus::Slave::readRegister ( int  addr,
uint16_t &  dest 
)
inline

Read a single register.

This function shall read a signle holding register to the address addr of the device.

The result of reading is stored in dest as word values (16 bits).

The function uses the Modbus function code 0x03 (read holding registers).

Returns
1 if successful. Otherwise it shall return -1 and set errno.
Examples:
server/clock-server-json/main.cpp, and server/clock-server/main.cpp.

References readRegisters().

Referenced by Modbus::BufferedSlave::writeInputRegister().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readRegister() [2/2]

template<typename T , Endian e>
int Modbus::Slave::readRegister ( int  addr,
Data< T, e > &  dest 
)
inline

Read a single holding data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall read a single holding data to the address addr of the device.

The result of reading is stored in dest as T value.

The function uses the Modbus function code 0x03 (read holding registers).

Returns
return the number of read holding Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References readRegisters(), and Modbus::Data< T, e >::registers().

Here is the call graph for this function:

◆ readRegisters() [1/2]

virtual int Modbus::Slave::readRegisters ( int  addr,
uint16_t *  dest,
int  nb = 1 
)
virtual

Read many registers.

This function shall read the content of the nb holding registers to the address addr of the remote device.

The result of reading is stored in dest array as word values (16 bits).

The function uses the Modbus function code 0x03 (read holding registers).

Returns
return the number of read registers if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Examples:
master/read-holding-data/main.cpp, and master/rw-holding-json/main.cpp.

Referenced by readRegister(), readRegisters(), and Modbus::BufferedSlave::writeInputRegister().

Here is the caller graph for this function:

◆ readRegisters() [2/2]

template<typename T , Endian e>
int Modbus::Slave::readRegisters ( int  addr,
Data< T, e > *  dest,
int  nb = 1 
)
inline

Read many holding data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall read the content of the nb data to the address addr of the device.

The result of reading is stored in dest array as T values.

The function uses the Modbus function code 0x03 (read holding registers).

Returns
return the number of read holding Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References readRegisters().

Here is the call graph for this function:

◆ reportSlaveId() [1/2]

virtual int Modbus::Slave::reportSlaveId ( uint16_t  max_dest,
uint8_t *  dest 
)
virtual

returns a description of the controller

This function shall send a request to the controller to obtain a description of the controller.

The response stored in dest contains:

  • the slave ID, this unique ID is in reality not unique at all so it’s not possible to depend on it to know how the information are packed in the response.
  • the run indicator status (0x00 = OFF, 0xFF = ON)
  • additional data specific to each controller. For example, libmodbuspp returns the version of the library as a string.

The function writes at most max_dest bytes from the response to dest so you must ensure that dest is large enough.

Returns
return the number of read data if successful. If the output was truncated due to the max_dest limit then the return value is the number of bytes which would have been written to dest if enough space had been available. Thus, a return value greater than max_dest means that the response data was truncated. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Examples:
master/report-slave-id/main.cpp.

Referenced by reportSlaveId(), Modbus::BufferedSlave::writeInputRegister(), and writeRegister().

Here is the caller graph for this function:

◆ reportSlaveId() [2/2]

template<typename T , Endian e = EndianBig>
int Modbus::Slave::reportSlaveId ( SlaveReport< T, e > &  dest)
inline

returns a description of the controller

SlaveReport is a template class for storing and manipulate slave identifier datas returns by the MODBUS 17 function.

Parameters
destdescription of the controller
Returns
return the number of read data bytes if successful. Otherwise it shall return -1 and set errno.

References reportSlaveId(), and Slave().

Here is the call graph for this function:

◆ setBoolArray()

static void Modbus::Slave::setBoolArray ( bool *  dest,
const uint8_t *  src,
size_t  n 
)
static

Set many booleans from an array of bytes.

All the bits of the bytes read from the first position of the array src are written as booleans in the dest array.

Parameters
dest
src
n

Referenced by writeRegister().

Here is the caller graph for this function:

◆ setPduAddressing()

void Modbus::Slave::setPduAddressing ( bool  pduAddressing = true)

Set the Modbus addressing mode.

Parameters
pduAddressingtrue for Modbus PDU adressing
See also
pduAddressing()

◆ writeCoil()

virtual int Modbus::Slave::writeCoil ( int  addr,
bool  src 
)
virtual

Write a single coil (bit)

This function shall write the status of src at the address addr of the remote device.

The function uses the Modbus function code 0x05 (force single coil).

Returns
1 if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

◆ writeCoils()

virtual int Modbus::Slave::writeCoils ( int  addr,
const bool *  src,
int  nb 
)
virtual

Write many coils (bits)

This function shall write the status of the nb bits (coils) from src at the address addr of the remote device. The src array must contains booelans.

The function uses the Modbus function code 0x0F (force multiple coils).

Returns
the number of written bits if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

◆ writeReadRegisters()

virtual int Modbus::Slave::writeReadRegisters ( int  write_addr,
const uint16_t *  src,
int  write_nb,
int  read_addr,
uint16_t *  dest,
int  read_nb 
)
virtual

Write and read many registers in a single transaction.

This function shall write the content of the write_nb holding registers from the array 'src' to the address write_addr of the remote device then shall read the content of the read_nb holding registers to the address read_addr of the remote device. The result of reading is stored in dest array as word values (16 bits).

You must take care to allocate enough memory to store the results in dest (at least nb * sizeof(uint16_t)).

The function uses the Modbus function code 0x17 (write/read registers).

Returns
the number of read registers if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

◆ writeRegister() [1/2]

virtual int Modbus::Slave::writeRegister ( int  addr,
uint16_t  value 
)
virtual

Write a single register.

This function shall write the value of value holding registers at the address addr of the remote device.

The function uses the Modbus function code 0x06 (preset single register).

Returns
1 if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Referenced by Modbus::BufferedSlave::writeInputRegister().

Here is the caller graph for this function:

◆ writeRegister() [2/2]

template<typename T , Endian e>
int Modbus::Slave::writeRegister ( int  addr,
Data< T, e > &  value 
)
inline

Write a single holding data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall write a single holding data from value at address addr of the device.

The function uses the Modbus function code 0x10 (preset multiple registers).

Returns
number of written holding Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References Modbus::Data< T, e >::registers(), reportSlaveId(), setBoolArray(), and writeRegisters().

Here is the call graph for this function:

◆ writeRegisters() [1/2]

virtual int Modbus::Slave::writeRegisters ( int  addr,
const uint16_t *  src,
int  nb 
)
virtual

Write many registers.

This function shall write the content of the nb holding registers from the array src at address addr of the remote device.

The function uses the Modbus function code 0x10 (preset multiple registers).

Returns
number of written registers if successful. Otherwise it shall return -1 and set errno.

Reimplemented in Modbus::BufferedSlave.

Examples:
master/rw-holding-json/main.cpp, and master/write-holding-data/main.cpp.

Referenced by Modbus::BufferedSlave::writeInputRegister(), writeRegister(), and writeRegisters().

Here is the caller graph for this function:

◆ writeRegisters() [2/2]

template<typename T , Endian e>
int Modbus::Slave::writeRegisters ( int  addr,
Data< T, e > *  src,
int  nb = 1 
)
inline

Write many holding data.

Data is a template class for storing, transmitting, and receiving arithmetic data in multiple 16-bit Modbus registers.

This function shall write the content of the nb holding data from the array src at address addr of the device.

The function uses the Modbus function code 0x10 (preset multiple registers).

Returns
number of written holding Modbus registers (16-bit) if successful. Otherwise it shall return -1 and set errno.

References writeRegisters().

Here is the call graph for this function: