modbus::internal::adu class

ADU base class.

ADU structure for TCP:

  • Header (7 bytes) — Transaction (2 bytes) — Protocol (2 bytes) — Length (2 bytes) [ unit identifier + PDU ] — Unit (1 byte)
  • PDU — Function (1 byte) — Rest of data... (N byte)

Derived classes

class request
class response

Public types

struct initializer_t

Public static variables

static packet_t::size_type header_length constexpr

Constructors, destructors, conversion operators

adu() explicit noexcept
adu(std::uint8_t function_code, std::uint16_t transaction = 0x00, std::uint8_t unit = 0x00) explicit noexcept
adu(constants::function_code function, std::uint16_t transaction = 0x00, std::uint8_t unit = 0x00) explicit noexcept
adu(std::uint8_t function_code, const initializer_t& initializer) explicit noexcept
adu(constants::function_code function, const initializer_t& initializer) explicit noexcept
adu(std::uint8_t function_code, const header_t& m_header) explicit noexcept
adu(constants::function_code function, const header_t& m_header) explicit noexcept

Public functions

auto encode() -> packet_t pure virtual
void decode(std::string_view packet) virtual
void decode(const packet_t& packet) pure virtual
auto function() const -> constants::function_code
auto transaction() const -> std::uint16_t
auto length() const -> std::uint16_t
auto unit() const -> std::uint8_t
auto header() const -> header_t
auto initialize(const initializer_t& initializer) -> adu&
auto header(const header_t& m_header) -> adu&
auto transaction(std::uint16_t new_transaction) -> adu&
auto calc_length(std::uint16_t pdu_length) -> adu&
auto length(std::uint16_t new_length) -> adu&
auto unit(std::uint8_t new_unit) -> adu&
auto operator==(const adu& other) const -> bool
auto operator<(const adu& other) const -> bool
auto operator>(const adu& other) const -> bool
auto dump(std::ostream& os) const -> std::ostream& virtual

Protected static functions

static auto calc_adu_length(std::uint16_t data_length) -> std::uint16_t constexpr

Protected functions

void decode_header(const packet_t& packet)
auto header_packet() -> packet_t

Protected static variables

static std::uint16_t protocol constexpr
static packet_t::size_type length_idx constexpr
static packet_t::size_type max_length constexpr
static packet_t::size_type max_pdu_size constexpr
static std::string_view header_func_format constexpr

Protected variables

constants::function_code function_
std::underlying_type_t<constants::function_code> function_code_
std::uint16_t transaction_
std::uint16_t length_
std::uint8_t unit_

Friends

template<typename ostream>
auto operator<<(ostream& os, const adu& obj) -> ostream&

Function documentation

modbus::internal::adu::adu() explicit noexcept

ADU constructor

modbus::internal::adu::adu(std::uint8_t function_code, std::uint16_t transaction = 0x00, std::uint8_t unit = 0x00) explicit noexcept

Parameters
function_code modbus function code
transaction transaction id
unit unit id

ADU constructor

modbus::internal::adu::adu(constants::function_code function, std::uint16_t transaction = 0x00, std::uint8_t unit = 0x00) explicit noexcept

Parameters
function modbus function
transaction transaction id
unit unit id

ADU constructor

modbus::internal::adu::adu(std::uint8_t function_code, const initializer_t& initializer) explicit noexcept

Parameters
function_code modbus function code
initializer initializer

ADU constructor

modbus::internal::adu::adu(constants::function_code function, const initializer_t& initializer) explicit noexcept

Parameters
function modbus function
initializer initializer

ADU constructor

modbus::internal::adu::adu(std::uint8_t function_code, const header_t& m_header) explicit noexcept

Parameters
function_code modbus function code
m_header header struct

ADU constructor

modbus::internal::adu::adu(constants::function_code function, const header_t& m_header) explicit noexcept

Parameters
function modbus function
m_header header struct

ADU constructor

packet_t modbus::internal::adu::encode() pure virtual

Returns packet that has been encoded

Encode packet

void modbus::internal::adu::decode(std::string_view packet) virtual

Parameters
packet packet to be decoded

Decode packet

void modbus::internal::adu::decode(const packet_t& packet) pure virtual

Parameters
packet packet to be decoded

Decode packet

constants::function_code modbus::internal::adu::function() const

Returns function code

Getter Get function code

std::uint16_t modbus::internal::adu::transaction() const

Returns transaction id

Get transaction id

std::uint16_t modbus::internal::adu::length() const

Returns length

Get length

std::uint8_t modbus::internal::adu::unit() const

Returns unit id

Get unit id

header_t modbus::internal::adu::header() const

Returns header

Get header

adu& modbus::internal::adu::initialize(const initializer_t& initializer)

Parameters
initializer initializer
Returns instance of ADU

Setter Iniitalize ADU

adu& modbus::internal::adu::header(const header_t& m_header)

Parameters
m_header header struct
Returns instance of ADU

Set header

adu& modbus::internal::adu::transaction(std::uint16_t new_transaction)

Parameters
new_transaction new transaction id
Returns instance of ADU

Set transaction id

adu& modbus::internal::adu::calc_length(std::uint16_t pdu_length)

Parameters
pdu_length PDU length
Returns instance of ADU

Calculate length of ADU given PDU length

adu& modbus::internal::adu::length(std::uint16_t new_length)

Parameters
new_length new length
Returns instance of ADU

Set length of ADU

adu& modbus::internal::adu::unit(std::uint8_t new_unit)

Parameters
new_unit new unit id
Returns instance of ADU

Set unit id

bool modbus::internal::adu::operator==(const adu& other) const

Parameters
other other ADU
Returns true if transaction_id is equal to other transaction_id

Operator Equality operator

bool modbus::internal::adu::operator<(const adu& other) const

Parameters
other other ADU
Returns true if transaction_id is less than other transaction_id

Less-than operator

bool modbus::internal::adu::operator>(const adu& other) const

Parameters
other other ADU
Returns true if transaction_id is more than other transaction_id

More-than operator

std::ostream& modbus::internal::adu::dump(std::ostream& os) const virtual

Parameters
os ostream
Returns ostream

Dump to string

static std::uint16_t modbus::internal::adu::calc_adu_length(std::uint16_t data_length) protected constexpr

Returns ADU length

Response size

header_length (containing unit_id) + 1 (function) + data_length

void modbus::internal::adu::decode_header(const packet_t& packet) protected

Parameters
packet packet to be decoded
Returns instance of ADU

Decode packet header

packet_t modbus::internal::adu::header_packet() protected

Returns packet consists of header

Get header packet

template<typename ostream>
ostream& operator<<(ostream& os, const adu& obj)

Parameters
os ostream
obj
Returns stream

Ostream operator

Variable documentation

static packet_t::size_type modbus::internal::adu::header_length constexpr

Header length

static std::uint16_t modbus::internal::adu::protocol protected constexpr

Protocol ID

static packet_t::size_type modbus::internal::adu::length_idx protected constexpr

Length index

static packet_t::size_type modbus::internal::adu::max_length protected constexpr

Max length

static packet_t::size_type modbus::internal::adu::max_pdu_size protected constexpr

Max PDU length

static std::string_view modbus::internal::adu::header_func_format protected constexpr

Header struct with function format

constants::function_code modbus::internal::adu::function_ protected

Function

std::underlying_type_t<constants::function_code> modbus::internal::adu::function_code_ protected

Function code

std::uint16_t modbus::internal::adu::transaction_ protected

Transaction id

std::uint16_t modbus::internal::adu::length_ protected

Length of packet [ unit identifier + PDU ]

std::uint8_t modbus::internal::adu::unit_ protected

Unit id