modbuspp
1.1.40
C++ wrapper for the libmodbus library
|
Device connected to Modbus. More...
#include <device.h>
Public Member Functions | |
NetLayer & | backend () const |
underlying backend | |
double | byteTimeout () |
Get timeout between bytes. More... | |
bool | byteTimeout (Timeout &timeout) |
Get timeout between bytes. More... | |
virtual void | close () |
Close a Modbus connection. | |
std::string | connection () const |
Returns the connection used. More... | |
bool | debug () const |
Return the debug flag. | |
Device (Net net, const std::string &connection, const std::string &settings) | |
Constructor. More... | |
Device (const std::string &jsonfile, const std::string &key=std::string()) | |
constructor from a JSON file More... | |
Device () | |
Default constructor. More... | |
int | flush () |
Flush non-transmitted data. More... | |
double | indicationTimeout () |
Get timeout used to wait for an indication (request received by a server) More... | |
bool | indicationTimeout (Timeout &timeout) |
Get timeout used to wait for an indication (request received by a server) More... | |
bool | isConnected () const |
Returns true if the device is connected; otherwise returns false. More... | |
virtual bool | isOpen () const |
Returns true if the device is open; otherwise returns false. More... | |
bool | isValid () const |
returns true if backend is set | |
Net | net () const |
Underlying layer used (backend) More... | |
virtual bool | open () |
Establish a Modbus connection. More... | |
bool | recoveryLink () const |
Returns true if link recovery mode is set; otherwise returns false. | |
double | responseTimeout () |
Get timeout for response. More... | |
bool | responseTimeout (Timeout &timeout) |
Get timeout for response. More... | |
RtuLayer & | rtu () |
underlying RTU layer (backend) More... | |
int | sendRawMessage (Message *msg, bool prepareBefore=false) |
Send a request/response msg via the socket of the context() More... | |
int | sendRawMessage (Message &msg, bool prepareBefore=false) |
bool | setBackend (Net net, const std::string &connection, const std::string &setting) |
Sets the backend for the net network. More... | |
bool | setByteTimeout (const double &timeout) |
Set timeout between bytes. More... | |
bool | setByteTimeout (const Timeout &timeout) |
Set timeout between bytes. More... | |
bool | setConfig (const std::string &jsonfile, const std::string &key=std::string()) |
Set configuration from a JSON file. More... | |
bool | setDebug (bool debug=true) |
Set debug flag. More... | |
bool | setIndicationTimeout (const double &timeout) |
Set timeout for indication. More... | |
bool | setIndicationTimeout (const Timeout &timeout) |
Set timeout for indication. More... | |
virtual bool | setRecoveryLink (bool recovery=true) |
Set the link recovery mode after disconnection. More... | |
bool | setResponseTimeout (const double &timeout) |
Set timeout for response. More... | |
bool | setResponseTimeout (const Timeout &timeout) |
Set timeout for response. More... | |
std::string | settings () const |
Returns the connection settings. More... | |
TcpLayer & | tcp () |
underlying TCP layer (backend) More... | |
virtual | ~Device () |
Destructor. More... | |
Static Public Member Functions | |
static std::string | lastError () |
last error message More... | |
This class is the base class for Master and Slave. It groups together their common properties and methods.
Modbus::Device::Device | ( | Net | net, |
const std::string & | connection, | ||
const std::string & | settings | ||
) |
Constructor.
Constructs a Modbus device for the net network.
For the Tcp backend :
For the Rtu backend :
settings specifies communication settings as a string in the format BBBBPS. BBBB specifies the baud rate of the communication, PS specifies the parity and the bits of stop, e.g. 38400E1 for 38400 baud with even parity and 1 stop.
According to Modbus RTU specifications :
An exception std::invalid_argument is thrown if one of the parameters is incorrect.
|
explicit |
constructor from a JSON file
The file describes the configuration to apply, its format is as follows:
Only the first 3 elements are mandatory : mode, connection and settings, the others are optional. In this example "modbuspp-device" is the key to the JSON object that should be used. If the key provided is empty, the file contains only one object corresponding to the configuration.
jsonfile | JSON file path |
key | name of the object key in the JSON file corresponding to the configuration to be applied |
Modbus::Device::Device | ( | ) |
Default constructor.
object cannot be used without calling setBackend() or setConfig()
|
virtual |
Destructor.
The destructor closes the connection if it is open and releases all affected resources.
double Modbus::Device::byteTimeout | ( | ) |
bool Modbus::Device::byteTimeout | ( | Timeout & | timeout | ) |
Get timeout between bytes.
This function shall store the timeout interval between two consecutive bytes of the same message in the timeout argument.
timeout | reference on the variable that will contain the timeout value |
std::string Modbus::Device::connection | ( | ) | const |
Returns the connection used.
Serial port or host depending on the backend.
For the Tcp backend :
For the Rtu backend :
int Modbus::Device::flush | ( | ) |
Flush non-transmitted data.
This function shall discard data received but not read to the socket or file descriptor associated to this connection.
double Modbus::Device::indicationTimeout | ( | ) |
Get timeout used to wait for an indication (request received by a server)
This function shall return the timeout interval used to wait for an indication in timeout. Indication is the term used by the Modbus protocol to designate a request received by the server.
The default value is zero, it means the server will wait forever.
bool Modbus::Device::indicationTimeout | ( | Timeout & | timeout | ) |
Get timeout used to wait for an indication (request received by a server)
This function shall return the timeout interval used to wait for an indication in timeout. Indication is the term used by the Modbus protocol to designate a request received by the server.
The default value is zero, it means the server will wait forever.
timeout | reference on the variable that will contain the timeout value |
bool Modbus::Device::isConnected | ( | ) | const |
Returns true if the device is connected; otherwise returns false.
in RTU mode, this function returns the same value as isOpen().
in TCP mode, this function returns true if a peer-to-peer TCP connection is currently established. Indeed, in server mode (slave), calling the open() function puts the Device in passive waiting mode, so that it is open but not connected. It is when a client connects that the function returns true.
|
virtual |
Returns true if the device is open; otherwise returns false.
|
static |
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 Modbus::Device::net | ( | ) | const |
Underlying layer used (backend)
This function allows to know the underlying layer used.
|
virtual |
Establish a Modbus connection.
This function shall establish a connection to a Modbus server, a network or a bus
double Modbus::Device::responseTimeout | ( | ) |
bool Modbus::Device::responseTimeout | ( | Timeout & | timeout | ) |
Get timeout for response.
This function shall return the timeout interval used to wait for a response in the timeout argument.
timeout | reference on the variable that will contain the timeout value |
RtuLayer& Modbus::Device::rtu | ( | ) |
underlying RTU layer (backend)
This function shall return the RTU layer if it is the layer used by the device. If it does not, a std::domain_error exception is thrown.
int Modbus::Device::sendRawMessage | ( | Message * | msg, |
bool | prepareBefore = false |
||
) |
Send a request/response msg via the socket of the context()
This function can be used to send message not handled by the library. The message is transmitted "raw", without any modification if prepareBefore is false, otherwise the message is modified to add a header (for TCP-IP) or a CRC (for RTU).
int Modbus::Device::sendRawMessage | ( | Message & | msg, |
bool | prepareBefore = false |
||
) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
bool Modbus::Device::setBackend | ( | Net | net, |
const std::string & | connection, | ||
const std::string & | setting | ||
) |
Sets the backend for the net network.
This function does nothing if isValid() is true and returns false.
For the Tcp backend :
For the Rtu backend :
settings specifies communication settings as a string in the format BBBBPS. BBBB specifies the baud rate of the communication, PS specifies the parity and the bits of stop, e.g. 38400E1 for 38400 baud with even parity and 1 stop.
According to Modbus RTU specifications :
An exception std::invalid_argument is thrown if one of the parameters is incorrect.
bool Modbus::Device::setByteTimeout | ( | const double & | timeout | ) |
Set timeout between bytes.
This function shall set the timeout interval between two consecutive bytes of the same message. The timeout is an upper bound on the amount of time elapsed before select() returns, if the time elapsed is longer than the defined timeout, an ETIMEDOUT
error will be raised by the function waiting for a response.
timeout | reference on the variable that will contain the new timeout in seconds. |
bool Modbus::Device::setByteTimeout | ( | const Timeout & | timeout | ) |
Set timeout between bytes.
This function shall set the timeout interval between two consecutive bytes of the same message. The timeout is an upper bound on the amount of time elapsed before select() returns, if the time elapsed is longer than the defined timeout, an ETIMEDOUT
error will be raised by the function waiting for a response.
timeout | reference on the variable that will contain the new timeout value |
bool Modbus::Device::setConfig | ( | const std::string & | jsonfile, |
const std::string & | key = std::string() |
||
) |
Set configuration from a JSON file.
This function does nothing if isValid() is true and returns false.
The file describes the configuration to apply, its format is as follows:
Only the first 3 elements are mandatory : mode, connection and settings, the others are optional. In this example "modbuspp-device" is the key to the JSON object that should be used. If the key provided is empty, the file contains only one object corresponding to the configuration.
jsonfile | JSON file path |
key | name of the object key in the JSON file corresponding to the configuration to be applied |
bool Modbus::Device::setDebug | ( | bool | debug = true | ) |
Set debug flag.
This function function shall set the debug flag by using the argument flag. By default, the boolean flag is set to false. When the flag value is set to true, many verbose messages are displayed on stdout and stderr. For example, this flag is useful to display the bytes of the Modbus messages :
bool Modbus::Device::setIndicationTimeout | ( | const double & | timeout | ) |
Set timeout for indication.
This function shall set the timeout interval used by a server to wait for a request from a client. If timeout is zero, this timeout will not be used at all. In this case, the server will wait forever.
timeout | reference on the variable that will contain the new timeout in seconds. |
bool Modbus::Device::setIndicationTimeout | ( | const Timeout & | timeout | ) |
Set timeout for indication.
This function shall set the timeout interval used by a server to wait for a request from a client. If timeout is zero, this timeout will not be used at all. In this case, the server will wait forever.
timeout | reference on the variable that will contain the new timeout value |
|
virtual |
Set the link recovery mode after disconnection.
When is set, the library will attempt an immediate reconnection when the connection is reset by peer.
Reimplemented in Modbus::Master.
bool Modbus::Device::setResponseTimeout | ( | const double & | timeout | ) |
Set timeout for response.
This function shall set the timeout interval used to wait for a response. When a byte timeout is set, if elapsed time for the first byte of response is longer than the given timeout, an ETIMEDOUT
error will be raised by the function waiting for a response. When byte timeout is disabled, the full confirmation response must be received before expiration of the response timeout.
timeout | reference on the variable that will contain the new timeout in seconds. |
bool Modbus::Device::setResponseTimeout | ( | const Timeout & | timeout | ) |
Set timeout for response.
This function shall set the timeout interval used to wait for a response. When a byte timeout is set, if elapsed time for the first byte of response is longer than the given timeout, an ETIMEDOUT
error will be raised by the function waiting for a response. When byte timeout is disabled, the full confirmation response must be received before expiration of the response timeout.
timeout | reference on the variable that will contain the new timeout value |
std::string Modbus::Device::settings | ( | ) | const |
Returns the connection settings.
IP port or speed, parity and stop bit depending on the backend
For the Tcp backend :
For the Rtu backend :
settings specifies communication settings as a string in the format BBBBPS. BBBB specifies the baud rate of the communication, PS specifies the parity and the bits of stop, e.g. 38400E1 for 38400 baud with even parity and 1 stop.
According to Modbus RTU specifications :
TcpLayer& Modbus::Device::tcp | ( | ) |
underlying TCP layer (backend)
This function shall return the TCP layer if it is the layer used by the device. If it does not, a std::domain_error exception is thrown.