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

Modbus Response Message. More...

#include <response.h>

Inheritance diagram for Modbus::Response:
Inheritance graph

Public Types

typedef int(* Callback) (Message *msg, Device *sender)
 Message callback function. More...
 

Public Member Functions

uint8_t * adu ()
 Returns the underlying ADU bytes pointer.
 
const uint8_t * adu () const
 
uint8_t adu (uint16_t i)
 Returns the ADU byte corresponding to the index provided. More...
 
uint16_t aduHeaderLength () const
 Return the ADU header length.
 
size_t aduSize () const
 returns the number of bytes of the ADU
 
bool bitValue (uint16_t index) const
 Returns a bit value of the response. More...
 
void bitValues (uint16_t index, uint16_t quantity, bool *values) const
 Returns bit values of the response. More...
 
uint8_t byte (int pduOffset) const
 Returns the PDU byte corresponding to the offset provided. More...
 
void clear ()
 Empties the message and returns it to the initial state.
 
uint16_t crc () const
 Return the CRC read in the message. More...
 
ExceptionCode exceptionCode () const
 Returns the exception code of the response. More...
 
Function function () const
 Returns the function of the request. More...
 
bool isResponse () const
 Returns true if the message is a response.
 
uint16_t maxAduLength () const
 Return the maximum ADU length.
 
Net net () const
 Underlying layer used (backend) More...
 
bool operator!= (const Message &other)
 Difference operator.
 
bool operator== (const Message &other)
 Equality operator.
 
uint8_t * pdu ()
 Returns the underlying ADU bytes pointer.
 
const uint8_t * pdu () const
 
uint8_t pdu (uint16_t i) const
 Returns the ADU byte corresponding to the index provided. More...
 
bool prepareToSend ()
 Prepare the message before sending. More...
 
void print (std::ostream &os=std::cout) const
 Prints all bytes of the message on the output stream provided. More...
 
void print (std::ostream &os, char prefix, char suffix) const
 Prints all bytes of the message on the output stream provided. More...
 
uint16_t quantity () const
 Returns the number of elements of the request. More...
 
uint16_t registerValue (uint16_t index) const
 Returns a register value of the response. More...
 
void registerValues (uint16_t index, uint16_t quantity, uint16_t *values) const
 Returns register values of the response. More...
 
 Response (NetLayer &backend)
 Constructors.
 
 Response (const Request &req)
 Copy Constructors.
 
 Response (Request &&req)
 Move Constructors.
 
void setAduSize (size_t size)
 Change the size of the ADU. More...
 
void setBitValue (uint16_t index, bool value)
 Sets a bit value for the response. More...
 
void setBitValues (uint16_t index, uint16_t quantity, const bool *values)
 Sets bit values for the response. More...
 
void setByte (int pduOffset, uint8_t value)
 Write a byte in the PDU to the supplied offset. More...
 
void setByteCount (uint8_t n)
 Sets the byte count for the response. More...
 
void setExceptionCode (ExceptionCode code)
 Sets the exception code for the response. More...
 
void setFunction (Function func)
 Sets the function of the request. More...
 
void setPdu (const uint8_t *data, size_t len)
 Set the PDU data. More...
 
void setPdu (const Message &src)
 
void setQuantity (uint16_t n)
 Sets the number of elements of the request. More...
 
void setRegisterValue (uint16_t index, uint16_t value)
 Sets a register value for the response. More...
 
void setRegisterValues (uint16_t index, uint16_t quantity, const uint16_t *values)
 Sets register values for the response. More...
 
void setResponseFlag (bool flag=true)
 Sets the response flag. More...
 
void setSize (size_t size)
 Change the size of the PDU. More...
 
void setSlaveId (int id)
 Sets the number of the slave concerned by the request. More...
 
void setStartingAdress (uint16_t addr)
 Sets the starting address of the request. More...
 
void setTransactionIdentifier (uint16_t tid)
 Sets TCP/IP transaction identifier. More...
 
void setWord (int pduOffset, uint16_t value)
 Write a word in the PDU to the supplied offset. More...
 
size_t size () const
 returns the number of bytes of the PDU More...
 
int slave () const
 Returns the number of the slave concerned by the request. More...
 
uint16_t startingAddress () const
 Returns the starting address of the request. More...
 
void swap (Message &other)
 Swaps Message other with this Message. This operation is very fast and never fails.
 
uint16_t transactionIdentifier () const
 Returns TCP/IP transaction identifier. More...
 
uint16_t word (int pduOffset) const
 Returns the PDU word corresponding to the offset provided. More...
 

Detailed Description

Modbus Response Message.

Author
Pascal JEAN, aka epsilonrt
Examples:
server/callback-server-json/main.cpp.

Member Typedef Documentation

◆ Callback

typedef int(* Modbus::Message::Callback) (Message *msg, Device *sender)
inherited

Message callback function.

Parameters
msg
Returns
1 if the message has been completely processed, 0 if the message has not been processed, -1 if error.

Member Function Documentation

◆ adu() [1/2]

const uint8_t* Modbus::Message::adu ( ) const
inherited

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

◆ adu() [2/2]

uint8_t Modbus::Message::adu ( uint16_t  i)
inherited

Returns the ADU byte corresponding to the index provided.

i must be between 0 and maxAduLength()-1

◆ bitValue()

bool Modbus::Response::bitValue ( uint16_t  index) const

Returns a bit value of the response.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02). This value is at the pdu[2 + index*2].

◆ bitValues()

void Modbus::Response::bitValues ( uint16_t  index,
uint16_t  quantity,
bool *  values 
) const

Returns bit values of the response.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02). This value is at the pdu[2 + index*2].

◆ byte()

uint8_t Modbus::Message::byte ( int  pduOffset) const
inherited

Returns the PDU byte corresponding to the offset provided.

pduOffset has a maximum value of MaxPduLength-1, it can have a negative value in order to access the bytes of the ADU that precedes the PDU.

◆ crc()

uint16_t Modbus::Message::crc ( ) const
inherited

Return the CRC read in the message.

throw an std::domain_error exception if net()!=Rtu, an std::invalid_argument exception if aduSize()<4.

◆ exceptionCode()

ExceptionCode Modbus::Response::exceptionCode ( ) const

Returns the exception code of the response.

Can be used for all functions. This value is at the pdu[1].

◆ function()

Function Modbus::Message::function ( ) const
inherited

Returns the function of the request.

Can be used for all functions. This value is at the offset[0] of the PDU

Examples:
server/callback-server-json/main.cpp.

◆ net()

Net Modbus::Message::net ( ) const
inherited

Underlying layer used (backend)

This function allows to know the underlying layer used.

◆ pdu() [1/2]

const uint8_t* Modbus::Message::pdu ( ) const
inherited

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

◆ pdu() [2/2]

uint8_t Modbus::Message::pdu ( uint16_t  i) const
inherited

Returns the ADU byte corresponding to the index provided.

i must be between 0 and (maxAduLength() - aduHeaderLength() - 1)

◆ prepareToSend()

bool Modbus::Message::prepareToSend ( )
inherited

Prepare the message before sending.

This function updates the ADU header or adds the CRC at the end depending on the network used.

If the message is a request transmitted over TCP, it needs a transaction identifier. If the backend is defined by the constructor, the TCP transaction identifier is provided by the backend and will therefore be consistent on the network, otherwise, it is the message that provides this value (which may be inconsistent on the network...)

◆ print() [1/2]

void Modbus::Message::print ( std::ostream &  os = std::cout) const
inherited

Prints all bytes of the message on the output stream provided.

The bytes printed are preceded by the character '<' and followed '>' if the message is a request, and preceded by the character '[' and followed '[' if the message is a response.

for debugging.

◆ print() [2/2]

void Modbus::Message::print ( std::ostream &  os,
char  prefix,
char  suffix 
) const
inherited

Prints all bytes of the message on the output stream provided.

The bytes printed are preceded by the character prefix and followed by the suffix.

for debugging.

◆ quantity()

uint16_t Modbus::Message::quantity ( ) const
inherited

Returns the number of elements of the request.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02), Read Holding Registers(03), Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the offset[3] of the PDU

Examples:
server/callback-server-json/main.cpp.

◆ registerValue()

uint16_t Modbus::Response::registerValue ( uint16_t  index) const

Returns a register value of the response.

Can be used for the functions Read Holding Registers(03) and Read Input Register(04). This value is at the pdu[2 + index*2].

◆ registerValues()

void Modbus::Response::registerValues ( uint16_t  index,
uint16_t  quantity,
uint16_t *  values 
) const

Returns register values of the response.

Can be used for the functions Read Holding Registers(03) and Read Input Register(04). This value is at the pdu[2 + index*2].

◆ setAduSize()

void Modbus::Message::setAduSize ( size_t  size)
inherited

Change the size of the ADU.

Enables the size of the PDU to be forced if the ADU buffer contains information that must be kept.

◆ setBitValue()

void Modbus::Response::setBitValue ( uint16_t  index,
bool  value 
)

Sets a bit value for the response.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02). This value is at the pdu[2 + index*2].

◆ setBitValues()

void Modbus::Response::setBitValues ( uint16_t  index,
uint16_t  quantity,
const bool *  values 
)

Sets bit values for the response.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02). This value is at the pdu[2 + index*2].

◆ setByte()

void Modbus::Message::setByte ( int  pduOffset,
uint8_t  value 
)
inherited

Write a byte in the PDU to the supplied offset.

pduOffset has a maximum value of MaxPduLength-1, it can have a negative value in order to access the bytes of the ADU that precedes the PDU.

◆ setByteCount()

void Modbus::Response::setByteCount ( uint8_t  n)

Sets the byte count for the response.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02), Read Holding Registers(03), Read Input Register(04). This value is at the pdu[1].

Examples:
server/callback-server-json/main.cpp.

◆ setExceptionCode()

void Modbus::Response::setExceptionCode ( ExceptionCode  code)

Sets the exception code for the response.

Can be used for all functions. This value is at the pdu[1].

◆ setFunction()

void Modbus::Message::setFunction ( Function  func)
inherited

Sets the function of the request.

Can be used for all functions. This value is at the offset[0] of the PDU

◆ setPdu() [1/2]

void Modbus::Message::setPdu ( const uint8_t *  data,
size_t  len 
)
inherited

Set the PDU data.

Copy data to the PDU message, set the PDU size.

◆ setPdu() [2/2]

void Modbus::Message::setPdu ( const Message src)
inherited

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

◆ setQuantity()

void Modbus::Message::setQuantity ( uint16_t  n)
inherited

Sets the number of elements of the request.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02), Read Holding Registers(03), Read Input Register(04), Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the offset[3] of the PDU

◆ setRegisterValue()

void Modbus::Response::setRegisterValue ( uint16_t  index,
uint16_t  value 
)

Sets a register value for the response.

Can be used for the functions Read Holding Registers(03) and Read Input Register(04). This value is at the pdu[2 + index*2].

Examples:
server/callback-server-json/main.cpp.

◆ setRegisterValues()

void Modbus::Response::setRegisterValues ( uint16_t  index,
uint16_t  quantity,
const uint16_t *  values 
)

Sets register values for the response.

Can be used for the functions Read Holding Registers(03) and Read Input Register(04). This value is at the pdu[2 + index*2].

◆ setResponseFlag()

void Modbus::Message::setResponseFlag ( bool  flag = true)
inherited

Sets the response flag.

This flag indicates that the message is a response, it allows to keep the instance of the request to make a response. This could be useful in order to keep the transaction identifier intact in the case of a TCP ADU.

◆ setSize()

void Modbus::Message::setSize ( size_t  size)
inherited

Change the size of the PDU.

Enables the size of the PDU to be forced if the ADU buffer contains information that must be kept.

Examples:
server/callback-server-json/main.cpp.

◆ setSlaveId()

void Modbus::Message::setSlaveId ( int  id)
inherited

Sets the number of the slave concerned by the request.

Can be used for all functions. This value is at the offset[-1] of the PDU

◆ setStartingAdress()

void Modbus::Message::setStartingAdress ( uint16_t  addr)
inherited

Sets the starting address of the request.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02), Read Holding Registers(03), Read Input Register(04), Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the offset[1] of the PDU

◆ setTransactionIdentifier()

void Modbus::Message::setTransactionIdentifier ( uint16_t  tid)
inherited

Sets TCP/IP transaction identifier.

It is used for transaction pairing, the MODBUS server copies in the response the transaction identifier of the request.

This value is at the offset[-7] of the PDU.

Throw an std::domain_error exception if net()!=Tcp,

◆ setWord()

void Modbus::Message::setWord ( int  pduOffset,
uint16_t  value 
)
inherited

Write a word in the PDU to the supplied offset.

pduOffset has a maximum value of MaxPduLength-2, it can have a negative value in order to access the wordss of the ADU that precedes the PDU.

◆ size()

size_t Modbus::Message::size ( ) const
inherited

returns the number of bytes of the PDU

Note
Use aduSize() to get the number of bytes of the ADU.

◆ slave()

int Modbus::Message::slave ( ) const
inherited

Returns the number of the slave concerned by the request.

Can be used for all functions. This value is at the offset[-1] of the PDU

◆ startingAddress()

uint16_t Modbus::Message::startingAddress ( ) const
inherited

Returns the starting address of the request.

Can be used for the functions Read Coils(01), Read Discrete Inputs(02), Read Holding Registers(03), Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the offset[1] of the PDU

Examples:
server/callback-server-json/main.cpp.

◆ transactionIdentifier()

uint16_t Modbus::Message::transactionIdentifier ( ) const
inherited

Returns TCP/IP transaction identifier.

It is used for transaction pairing, the MODBUS server copies in the response the transaction identifier of the request.

This value is at the offset[-7] of the PDU.

Throw an std::domain_error exception if net()!=Tcp,

◆ word()

uint16_t Modbus::Message::word ( int  pduOffset) const
inherited

Returns the PDU word corresponding to the offset provided.

pduOffset has a maximum value of MaxPduLength-2, it can have a negative value in order to access the words of the ADU that precedes the PDU.