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

Buffered Slave. More...

#include <bufferedslave.h>

Inheritance diagram for Modbus::BufferedSlave:
Inheritance graph

Public Member Functions

Message::Callback afterReplyCallback () const
 Return the after reply callback function cb of this slave.
 
Message::Callback beforeReplyCallback () const
 Return the before reply callback function cb of this slave.
 
 BufferedSlave (int slaveAddr, Device *dev=0)
 Constructor. More...
 
 BufferedSlave ()
 Default Constructor. More...
 
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 number() is 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)
 
int readDiscreteInput (int addr, bool &dest)
 Read a single discrete input (input bit) More...
 
virtual int readDiscreteInputs (int addr, bool *dest, int nb=1)
 
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)
 
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)
 
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)
 
template<typename T , Endian e = EndianBig>
int reportSlaveId (SlaveReport< T, e > &dest)
 returns a description of the controller More...
 
void setAfterReplyCallback (Message::Callback cb)
 Set the after reply callback function cb of this slave. More...
 
void setBeforeReplyCallback (Message::Callback cb)
 Set the before reply callback function cb of this slave. More...
 
int setBlock (Table t, int nmemb, int startAddr=-1)
 Setting a block of data in the memory map. 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...
 
int updateBlockFromSlave (Table t)
 Update data block t from the real slave. More...
 
bool updateBlockFromSlave ()
 Update all data blocks from the real slave. More...
 
int updateSlaveFromBlock (Table t)
 Update the real slave from data block t. More...
 
bool updateSlaveFromBlock ()
 Update the real slave from coils and holding registers data blocks. More...
 
virtual int writeCoil (int addr, bool src)
 
virtual int writeCoils (int addr, const bool *src, int nb)
 
int writeDiscreteInput (int addr, bool src)
 Write a single discrete input (bit) More...
 
int writeDiscreteInputs (int addr, const bool *src, int nb)
 Read many discrete inputs (bits) More...
 
int writeInputRegister (int addr, uint16_t value)
 Write a single register. More...
 
template<typename T , Endian e>
int writeInputRegister (int addr, Data< T, e > &value)
 Write a single input data. More...
 
int writeInputRegisters (int addr, const uint16_t *src, int nb)
 Write many input registers. More...
 
template<typename T , Endian e>
int writeInputRegisters (int addr, Data< T, e > *src, int nb=1)
 Write many input data. More...
 
virtual int writeReadRegisters (int write_addr, const uint16_t *src, int write_nb, int read_addr, uint16_t *dest, int read_nb)
 
virtual int writeRegister (int addr, uint16_t value)
 
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)
 
template<typename T , Endian e>
int writeRegisters (int addr, Data< T, e > *src, int nb=1)
 Write many holding data. More...
 
virtual ~BufferedSlave ()
 Destructor.
 

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

Buffered Slave.

Author
Pascal JEAN, aka epsilonrt
Examples:
router/router-simple/main.cpp, server/clock-server-json/main.cpp, and server/clock-server/main.cpp.

Constructor & Destructor Documentation

◆ BufferedSlave() [1/2]

Modbus::BufferedSlave::BufferedSlave ( int  slaveAddr,
Device dev = 0 
)

Constructor.

Constructor of a new buffered slave with the slaveAddr identifier. If the device dev is provided, usually of the class Master and dev->isOpen() returns true:

  • The requested data is actually read, then stored in the memory buffer before being returned.
  • The data provided is actually written, after being stored in the memory buffer.

◆ BufferedSlave() [2/2]

Modbus::BufferedSlave::BufferedSlave ( )

Default Constructor.

object cannot be used without calling setNumber()

Referenced by writeInputRegister().

Here is the caller graph for this function:

Member Function Documentation

◆ dataAddress()

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

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
inherited

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
inherited

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 
)
inlineinherited

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 Modbus::Slave::readCoils().

Here is the call graph for this function:

◆ readCoils()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ readDiscreteInput()

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

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 Modbus::Slave::readDiscreteInputs().

Here is the call graph for this function:

◆ readDiscreteInputs()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ readInputRegister() [1/2]

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

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 Modbus::Slave::readInputRegisters().

Referenced by 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 
)
inlineinherited

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 Modbus::Slave::readInputRegisters(), and Modbus::Data< T, e >::registers().

Here is the call graph for this function:

◆ readInputRegisters() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ readInputRegisters() [2/2]

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

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 Modbus::Slave::readInputRegisters().

Here is the call graph for this function:

◆ readRegister() [1/2]

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

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 Modbus::Slave::readRegisters().

Referenced by 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 
)
inlineinherited

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 Modbus::Slave::readRegisters(), and Modbus::Data< T, e >::registers().

Here is the call graph for this function:

◆ readRegisters() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ readRegisters() [2/2]

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

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 Modbus::Slave::readRegisters().

Here is the call graph for this function:

◆ reportSlaveId() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ reportSlaveId() [2/2]

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

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 Modbus::Slave::reportSlaveId(), and Modbus::Slave::Slave().

Here is the call graph for this function:

◆ setAfterReplyCallback()

void Modbus::BufferedSlave::setAfterReplyCallback ( Message::Callback  cb)

Set the after reply callback function cb of this slave.

This function is called after the main message manager responds to the master by sending it a response from the contents of the memory map.

◆ setBeforeReplyCallback()

void Modbus::BufferedSlave::setBeforeReplyCallback ( Message::Callback  cb)

Set the before reply callback function cb of this slave.

This function is called before the main message manager responds to the master by sending it a response from the contents of the memory map.

◆ setBlock()

int Modbus::BufferedSlave::setBlock ( Table  t,
int  nmemb,
int  startAddr = -1 
)

Setting a block of data in the memory map.

A single block of type t can be defined for a given slave. The block has nmeb elements and starts at startAddr.

Returns
number of elements actually allocated, -1 if error
Examples:
router/router-simple/main.cpp, and server/clock-server/main.cpp.

◆ setBoolArray()

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

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 Modbus::Slave::writeRegister().

Here is the caller graph for this function:

◆ setPduAddressing()

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

Set the Modbus addressing mode.

Parameters
pduAddressingtrue for Modbus PDU adressing
See also
pduAddressing()

◆ updateBlockFromSlave() [1/2]

int Modbus::BufferedSlave::updateBlockFromSlave ( Table  t)

Update data block t from the real slave.

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

◆ updateBlockFromSlave() [2/2]

bool Modbus::BufferedSlave::updateBlockFromSlave ( )

Update all data blocks from the real slave.

Returns
true successful. Otherwise it shall return false and set errno.

Referenced by writeInputRegister().

Here is the caller graph for this function:

◆ updateSlaveFromBlock() [1/2]

int Modbus::BufferedSlave::updateSlaveFromBlock ( Table  t)

Update the real slave from data block t.

Warning
Only coils and holding registers may be update from data blocks.
Returns
number of written Modbus items if successful. Otherwise it shall return -1 and set errno.

◆ updateSlaveFromBlock() [2/2]

bool Modbus::BufferedSlave::updateSlaveFromBlock ( )

Update the real slave from coils and holding registers data blocks.

Returns
true successful. Otherwise it shall return false and set errno.

Referenced by writeInputRegister().

Here is the caller graph for this function:

◆ writeCoil()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

Examples:
server/clock-server-json/main.cpp, and server/clock-server/main.cpp.

◆ writeCoils()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ writeDiscreteInput()

int Modbus::BufferedSlave::writeDiscreteInput ( int  addr,
bool  src 
)

Write a single discrete input (bit)

This function shall write the status of src at the address addr of the memory map.

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

◆ writeDiscreteInputs()

int Modbus::BufferedSlave::writeDiscreteInputs ( int  addr,
const bool *  src,
int  nb 
)

Read many discrete inputs (bits)

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

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

◆ writeInputRegister() [1/2]

int Modbus::BufferedSlave::writeInputRegister ( int  addr,
uint16_t  value 
)

Write a single register.

This function shall write the value of value input register at the address addr of the memory map.

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

◆ writeInputRegister() [2/2]

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

Write 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 write a single input data from value at address addr of the device.

Returns
number of written input Modbus registers (16-bit) if successful. Otherwise it shall return -1.

References BufferedSlave(), Modbus::Slave::readInputRegister(), Modbus::Slave::readInputRegisters(), Modbus::Slave::readRegister(), Modbus::Slave::readRegisters(), Modbus::Data< T, e >::registers(), Modbus::Slave::reportSlaveId(), updateBlockFromSlave(), updateSlaveFromBlock(), writeInputRegisters(), Modbus::Slave::writeRegister(), and Modbus::Slave::writeRegisters().

Here is the call graph for this function:

◆ writeInputRegisters() [1/2]

int Modbus::BufferedSlave::writeInputRegisters ( int  addr,
const uint16_t *  src,
int  nb 
)

Write many input registers.

This function shall write the content of the nb input registers from the array src at address addr of the memory map.

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

Referenced by writeInputRegister(), and writeInputRegisters().

Here is the caller graph for this function:

◆ writeInputRegisters() [2/2]

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

Write many input 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 input data from the array src at address addr of the memory map.

Returns
number of written input Modbus registers (16-bit) if successful. Otherwise it shall return -1.

References writeInputRegisters().

Here is the call graph for this function:

◆ writeReadRegisters()

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ writeRegister() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

Examples:
server/clock-server-json/main.cpp, and server/clock-server/main.cpp.

◆ writeRegister() [2/2]

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

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(), Modbus::Slave::reportSlaveId(), Modbus::Slave::setBoolArray(), and Modbus::Slave::writeRegisters().

Here is the call graph for this function:

◆ writeRegisters() [1/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Reimplemented from Modbus::Slave.

◆ writeRegisters() [2/2]

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

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 Modbus::Slave::writeRegisters().

Here is the call graph for this function: