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

Modbus Request Message. More...

#include <request.h>

Inheritance diagram for Modbus::Request:
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
 
uint8_t byte (int pduOffset) const
 Returns the PDU byte corresponding to the offset provided. More...
 
uint8_t byteCount () const
 Returns the byte count of the request. More...
 
void clear ()
 Empties the message and returns it to the initial state.
 
bool coilValue () const
 Returns the coil value of the request. More...
 
bool coilValue (uint16_t index) const
 Returns a coil value of the request. More...
 
void coilValues (uint16_t index, uint16_t quantity, bool *values) const
 Returns coil values of the request. More...
 
uint16_t crc () const
 Return the CRC read in the message. 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 () const
 Returns the register value of the request. More...
 
uint16_t registerValue (uint16_t index) const
 Returns a register value of the request. More...
 
void registerValues (uint16_t index, uint16_t quantity, uint16_t *values) const
 Returns a register values of the request. More...
 
 Request (NetLayer &backend)
 Constructors.
 
 Request (const Request &other)
 Constructs a copy of other.
 
 Request (Request &&other)
 Move Constructor The object acquires the content managed by other. other becomes an empty object (as if default-constructed).
 
void setAduSize (size_t size)
 Change the size of the ADU. 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 request. More...
 
void setCoilValue (bool value)
 Sets the coil value for the request. More...
 
void setCoilValue (uint16_t index, bool value)
 Sets the coil value for the request. More...
 
void setCoilValues (uint16_t index, uint16_t quantity, const bool *values)
 Sets register values for the request. 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 value)
 Sets the register value for the request. More...
 
void setRegisterValue (uint16_t index, uint16_t value)
 Sets a register value for the request. More...
 
void setRegisterValues (uint16_t index, uint16_t quantity, const uint16_t *values)
 Sets register values for the request. 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 Request Message.

Author
Pascal JEAN, aka epsilonrt

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

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

◆ byteCount()

uint8_t Modbus::Request::byteCount ( ) const

Returns the byte count of the request.

Can be used for the functions Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the pdu[5].

◆ coilValue() [1/2]

bool Modbus::Request::coilValue ( ) const

Returns the coil value of the request.

Can be used for function Write Single Coil(05). This value is at the pdu[3].

◆ coilValue() [2/2]

bool Modbus::Request::coilValue ( uint16_t  index) const

Returns a coil value of the request.

Can be used for function Write Multiple Coils(15). This values are at the pdu[6+index/8].

◆ coilValues()

void Modbus::Request::coilValues ( uint16_t  index,
uint16_t  quantity,
bool *  values 
) const

Returns coil values of the request.

Can be used for function Write Multiple Coils(15). This values are at the pdu[6+index/8].

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

◆ 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() [1/2]

uint16_t Modbus::Request::registerValue ( ) const

Returns the register value of the request.

Can be used for function Write Single Register(06). This value is at the pdu[3].

◆ registerValue() [2/2]

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

Returns a register value of the request.

Can be used for function Write Multiple registers(16). This value is at the pdu[6+index].

◆ registerValues()

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

Returns a register values of the request.

Can be used for function Write Multiple registers(16). This values are at the pdu[6+index].

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

◆ 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::Request::setByteCount ( uint8_t  n)

Sets the byte count for the request.

Can be used for the functions Write Multiple Coils(15) and Write Multiple Registers(16). This value is at the pdu[5].

◆ setCoilValue() [1/2]

void Modbus::Request::setCoilValue ( bool  value)

Sets the coil value for the request.

Can be used for function Write Single Coil(05). This value is at the pdu[3].

◆ setCoilValue() [2/2]

void Modbus::Request::setCoilValue ( uint16_t  index,
bool  value 
)

Sets the coil value for the request.

Can be used for function Write Multiple Coils(15). This values are at the pdu[6+index/8].

◆ setCoilValues()

void Modbus::Request::setCoilValues ( uint16_t  index,
uint16_t  quantity,
const bool *  values 
)

Sets register values for the request.

Can be used for function Write Multiple Coils(15). This values are at the pdu[6+index/8].

◆ 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() [1/2]

void Modbus::Request::setRegisterValue ( uint16_t  value)

Sets the register value for the request.

Can be used for function Write Single Register(06). This value is at the pdu[3].

◆ setRegisterValue() [2/2]

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

Sets a register value for the request.

Can be used for function Write Multiple registers(16). This value is at the pdu[6+index].

◆ setRegisterValues()

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

Sets register values for the request.

Can be used for function Write Multiple registers(16). This value is at the pdu[6+index].

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