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

Modbus Message. More...

#include <message.h>

Inheritance diagram for Modbus::Message:
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...
 
void clear ()
 Empties the message and returns it to the initial state.
 
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.
 
 Message (NetLayer &backend)
 Constructors.
 
 Message (const Message &other)
 Constructs a copy of other.
 
 Message (Message &&other)
 Move Constructor The object acquires the content managed by other. other becomes an empty object (as if default-constructed).
 
Net net () const
 Underlying layer used (backend) More...
 
bool operator!= (const Message &other)
 Difference operator.
 
Messageoperator= (const Message &other)
 Sets the Message object to be equal to other.
 
Messageoperator= (Message &&other)
 Move assignment.
 
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...
 
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 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 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...
 
virtual ~Message ()
 Destructor.
 

Friends

std::ostream & operator<< (std::ostream &os, const Message &m)
 Inserts the message m byte sequence into os.
 

Detailed Description

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

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

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)

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

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

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

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

Underlying layer used (backend)

This function allows to know the underlying layer used.

◆ pdu() [1/2]

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

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

Returns the ADU byte corresponding to the index provided.

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

◆ prepareToSend()

bool Modbus::Message::prepareToSend ( )

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

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

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

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.

◆ setAduSize()

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

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 
)

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.

◆ setFunction()

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

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 
)

Set the PDU data.

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

◆ setPdu() [2/2]

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

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)

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

◆ setResponseFlag()

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

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)

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)

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)

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)

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 
)

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

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

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

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

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

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.