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

RTU serial link layer. More...

#include <rtulayer.h>

Inheritance diagram for Modbus::RtuLayer:
Inheritance graph

Public Member Functions

int baud () const
 Return the baudrate.
 
const std::string & connection () const
 Returns the connection used. More...
 
modbus_t * context ()
 libmodbus context More...
 
const modbus_t * context () const
 
uint16_t maxAduLength () const
 Returns the maximum ADU length.
 
Net net () const
 Underlying layer used (backend) More...
 
char parity () const
 Return the parity.
 
const std::string & port () const
 Name of the serial port. More...
 
virtual bool prepareToSend (Message &msg)
 
SerialRts rts ()
 Get the current RTS mode. More...
 
int rtsDelay ()
 Get the current RTS delay. More...
 
 RtuLayer (const std::string &port, const std::string &settings)
 Constructor.
 
virtual int sendRawMessage (const Message *msg)
 
SerialMode serialMode ()
 Get the current serial mode. More...
 
bool setRts (SerialRts rts)
 Set the RTS mode. More...
 
bool setRtsDelay (int us)
 Set the RTS delay. More...
 
bool setSerialMode (SerialMode mode)
 Set the serial mode. More...
 
const std::string & settings () const
 Returns the connection settings. More...
 
int stop () const
 Return the bits of stop.
 

Static Public Member Functions

static int baud (const std::string &settings)
 Extracts the baudrate from a settings string. More...
 
static bool checkMessage (const Message &msg)
 
static uint16_t crc16 (const uint8_t *buf, uint16_t count)
 Performing Modbus CRC16 generation of the buffer buf.
 
static std::string lastError ()
 last error message More...
 
static char parity (const std::string &settings)
 Extracts the parity from a settings string. More...
 
static int stop (const std::string &settings)
 Return the stop bits from a settings string. More...
 

Detailed Description

RTU serial link layer.

This class can not and should not be instantiated by the user. It provides access to properties and methods specific to the RTU layer.

An instance of this class is created by the constructor Device::Device() of the Device class (or its derived classes) if the RTU layer is selected.

Access to this instance is done using the Device::rtu() method.

See also
Device::Device()
Device::rtu()
Author
Pascal JEAN, aka epsilonrt

Member Function Documentation

◆ baud()

static int Modbus::RtuLayer::baud ( const std::string &  settings)
static

Extracts the baudrate from a settings string.

Returns
the baudrate found. if no value is found, returns the default value, ie 19200.

◆ checkMessage()

static bool Modbus::RtuLayer::checkMessage ( const Message msg)
static

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

◆ connection()

const std::string& Modbus::NetLayer::connection ( ) const
inherited

Returns the connection used.

Serial port or host depending on the backend.

◆ context() [1/2]

modbus_t* Modbus::NetLayer::context ( )
inherited

libmodbus context

context is an opaque structure containing all necessary information to establish a connection with other Modbus devices according to the selected variant.

◆ context() [2/2]

const modbus_t* Modbus::NetLayer::context ( ) const
inherited

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

◆ lastError()

static std::string Modbus::NetLayer::lastError ( )
staticinherited

last error message

This function shall return the error message corresponding to the last error. This function must be called right after the instruction that triggered an error.

◆ net()

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

Underlying layer used (backend)

This function allows to know the underlying layer used.

◆ parity()

static char Modbus::RtuLayer::parity ( const std::string &  settings)
static

Extracts the parity from a settings string.

Returns
the parity found. if no value is found, returns the default value, ie E for Even parity.

◆ port()

const std::string& Modbus::RtuLayer::port ( ) const

Name of the serial port.

This property specifies the name of the serial port handled by the OS, eg. "/dev/ttyS0" or "/dev/ttyUSB0".

◆ prepareToSend()

virtual bool Modbus::RtuLayer::prepareToSend ( Message msg)
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::NetLayer.

◆ rts()

SerialRts Modbus::RtuLayer::rts ( )

Get the current RTS mode.

This function shall get the current Request To Send mode

Returns
the current RTS mode if successful. Otherwise it shall return UnknownRts (-1) and set errno.
See also
setRts()

◆ rtsDelay()

int Modbus::RtuLayer::rtsDelay ( )

Get the current RTS delay.

This function shall get the current Request To Send delay period.

Returns
the current RTS delay in microseconds if successful. Otherwise it shall return -1 and set errno.
See also
setRtsDelay()

◆ sendRawMessage()

virtual int Modbus::RtuLayer::sendRawMessage ( const Message msg)
virtual

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

Warning
This function is not supported by Windows !

Reimplemented from Modbus::NetLayer.

◆ serialMode()

SerialMode Modbus::RtuLayer::serialMode ( )

Get the current serial mode.

This function shall return the serial mode currently used.

  • Rs232: the serial line is set for RS232 communication. RS-232 (Recommended Standard 232) is the traditional name for a series of standards for serial binary single-ended data and control signals connecting between a DTE (Data Terminal Equipment) and a DCE (Data Circuit-terminating Equipment). It is commonly used in computer serial ports
  • Rs485: the serial line is set for RS485 communication. EIA-485, also known as TIA/EIA-485 or RS-485, is a standard defining the electrical characteristics of drivers and receivers for use in balanced digital multipoint systems. This standard is widely used for communications in industrial automation because it can be used effectively over long distances and in electrically noisy environments.

This function is only available on Linux kernels 2.6.28 onwards.

Returns
return the current mode if successful. Otherwise it shall return UnknownMode (-1) and set errno.
See also
setSerialMode()

◆ setRts()

bool Modbus::RtuLayer::setRts ( SerialRts  rts)

Set the RTS mode.

This function shall set the Request To Send mode to communicate on a RS485 serial bus.

Returns
true if successful. Otherwise it shall return false and set errno.
See also
rts()
Examples:
router/router-simple/main.cpp.

◆ setRtsDelay()

bool Modbus::RtuLayer::setRtsDelay ( int  us)

Set the RTS delay.

This function shall set the Request To Send delay period in microseconds.

Returns
true if successful. Otherwise it shall return false and set errno.
See also
rtsDelay()

◆ setSerialMode()

bool Modbus::RtuLayer::setSerialMode ( SerialMode  mode)

Set the serial mode.

This function shall set the selected serial mode mode.

Returns
true if successful. Otherwise it shall return false and set errno.
See also
serialMode()
Examples:
router/router-simple/main.cpp.

◆ settings()

const std::string& Modbus::NetLayer::settings ( ) const
inherited

Returns the connection settings.

IP port or speed, parity and stop bit depending on the backend

◆ stop()

static int Modbus::RtuLayer::stop ( const std::string &  settings)
static

Return the stop bits from a settings string.

Returns
the number returned is determined based on the parity found. If the parity is None, this function returns 2, otherwise returns 1.