modbuspp
1.1.40
C++ wrapper for the libmodbus library
|
Router connected to Modbus. More...
#include <router.h>
Public Member Functions | |
Master & | addMaster (const std::string &name, Net net, const std::string &connection, const std::string &settings) |
Adds a master for the net network (inside). More... | |
Master & | addMaster (const std::string &name) |
BufferedSlave & | addSlave (int slaveAddr, Device *master=0) |
Adds a slave. More... | |
NetLayer & | backend () const |
underlying backend | |
double | byteTimeout () |
Get timeout between bytes. More... | |
bool | byteTimeout (Timeout &timeout) |
Get timeout between bytes. More... | |
virtual void | close () |
std::string | connection () const |
Returns the connection used. More... | |
bool | debug () const |
Return the debug flag. | |
int | flush () |
Flush non-transmitted data. More... | |
bool | hasMaster (const std::string &name) const |
Check if the master at the given name exists. | |
bool | hasSlave (int slaveAddr) const |
Check if the slave at the given address slaveAddrexists. | |
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 | isRunning () const |
Returns if the server is launched in a thread. | |
bool | isValid () const |
returns true if backend is set | |
Master & | master (const std::string &name) |
Returns the master whose name is provided. More... | |
const Master & | master (const std::string &name) const |
Master * | masterPtr (const std::string &name) |
Returns a pointer to the master whose name is provided. More... | |
const Master * | masterPtr (const std::string &name) const |
const std::map< std::string, std::shared_ptr< Master > > & | masters () const |
Returns the list of masters as a map indexed by name. | |
Message::Callback | messageCallback () const |
Return the message callback function. | |
Net | net () const |
Underlying layer used (backend) More... | |
virtual bool | open () |
Establish a Modbus connection. More... | |
Master & | operator[] (const std::string &) |
Master table access operator. More... | |
const Master & | operator[] (const std::string &) const |
BufferedSlave & | operator[] (int) |
Slave table access operator. More... | |
const BufferedSlave & | operator[] (int) const |
int | poll (long timeoutMs=0) |
Performs all server operations. 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... | |
Router (Net net, const std::string &connection, const std::string &settings) | |
Constructor. More... | |
Router (const std::string &jsonfile, const std::string &key=std::string()) | |
constructor from a JSON file More... | |
Router () | |
Default constructor. More... | |
RtuLayer & | rtu () |
underlying RTU layer (backend) More... | |
bool | run () |
Start the server in a new thread. 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... | |
void | setMessageCallback (Message::Callback cb) |
Set the message callback function cb. 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... | |
BufferedSlave & | slave (int slaveAddr=-1) |
Returns the slave whose address is provided. More... | |
const BufferedSlave & | slave (int slaveAddr=-1) const |
BufferedSlave * | slavePtr (int slaveAddr=-1) |
Returns a pointer to the slave whose address is provided. More... | |
const BufferedSlave * | slavePtr (int slaveAddr=-1) const |
const std::map< int, std::shared_ptr< BufferedSlave > > & | slaves () const |
Returns the list of slaves as a map indexed by identifier number. | |
TcpLayer & | tcp () |
underlying TCP layer (backend) More... | |
void | terminate () |
Shutdown the connection then stops the server if it is started in a thread. | |
virtual | ~Router () |
Destructor. More... | |
Static Public Member Functions | |
static std::string | lastError () |
last error message More... | |
To use, simply perform the following actions:
Modbus::Router::Router | ( | Net | net, |
const std::string & | connection, | ||
const std::string & | settings | ||
) |
Constructor.
Constructs a Modbus router for the net network (outside).
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.
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-router" 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.
For slave objects, only the id field is required.
In blocks objects, the table and quantity members are the only mandatory.
jsonfile | JSON file path |
key | name of the object key in the JSON file corresponding to the configuration to be applied |
Modbus::Router::Router | ( | ) |
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.
Master& Modbus::Router::addMaster | ( | const std::string & | name, |
Net | net, | ||
const std::string & | connection, | ||
const std::string & | settings | ||
) |
Adds a master for the net network (inside).
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.
According to Modbus RTU specifications :
An exception std::invalid_argument is thrown if one of the parameters is incorrect.
name | name of the master (must be unique) |
Master& Modbus::Router::addMaster | ( | const std::string & | name | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
master cannot be used without calling setBackend() or setConfig()
|
inherited |
Adds a slave.
This function shall add a slave with the slaveAddr value.
The behavior depends of network and the role of the device:
TCP: The slave number is only required in TCP if the message must reach a device on a serial link. Some not compliant devices or software (such as modpoll) uses the slave ID as unit identifier, that's incorrect (cf page 23 of Modbus Messaging Implementation Guide v1.0b) but without the slave value, the faulty remote device or software drops the requests !
The special value TcpSlave (255) can be used in TCP mode to restore the default value.
|
inherited |
|
inherited |
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 |
|
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::Server.
|
inherited |
Returns the connection used.
Serial port or host depending on the backend.
For the Tcp backend :
For the Rtu backend :
|
inherited |
Flush non-transmitted data.
This function shall discard data received but not read to the socket or file descriptor associated to this connection.
|
inherited |
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.
|
inherited |
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 |
|
inherited |
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.
|
virtualinherited |
Returns true if the device is open; otherwise returns false.
|
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.
Master& Modbus::Router::master | ( | const std::string & | name | ) |
Returns the master whose name is provided.
The master must have been added with addMaster() else a std::out_of_range exception is thrown.
const Master& Modbus::Router::master | ( | const std::string & | name | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Master* Modbus::Router::masterPtr | ( | const std::string & | name | ) |
Returns a pointer to the master whose name is provided.
The master must have been added with addMaster() else a a nullptr is returned.
const Master* Modbus::Router::masterPtr | ( | const std::string & | name | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
Underlying layer used (backend)
This function allows to know the underlying layer used.
|
virtualinherited |
Establish a Modbus connection.
This function shall establish a connection to a Modbus server, a network or a bus
Master& Modbus::Router::operator[] | ( | const std::string & | ) |
const Master& Modbus::Router::operator[] | ( | const std::string & | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
Performs all server operations.
Wait at most timeoutMs ms for a request from a client and then perform the necessary operations before responding.
|
inherited |
|
inherited |
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 |
|
inherited |
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.
|
inherited |
Start the server in a new thread.
|
inherited |
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).
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
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.
|
inherited |
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. |
|
inherited |
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 |
|
inherited |
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 |
|
inherited |
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 :
|
inherited |
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. |
|
inherited |
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 |
|
inherited |
Set the message callback function cb.
Used to intercept received messages. The function thus installed will be called if the slave address (unit identifier) does not correspond to any slave registered by Server::addSlave().This will make it possible to process MODBUS messages not supported by libmodbus or, to route the messages to links not supported by libmodbus.
|
virtualinherited |
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.
|
inherited |
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. |
|
inherited |
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 |
|
inherited |
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 :
|
inherited |
Returns the slave whose address is provided.
The slave must have been added with addSlave() else a std::out_of_range exception is thrown.
If the Device that drives the slave to an RTU backend, we can access to the general call through the slave at address 0 (added by the constructor).
If the Device that drives the slave to a TCP backend, we can access to TCP messages through the slave at address 255 (added by the constructor).
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
Returns a pointer to the slave whose address is provided.
The slave must have been added with addSlave() else a a nullptr is returned.
If the Device that drives the slave to an RTU backend, we can access to the general call through the slave at address 0 (added by the constructor).
If the Device that drives the slave to a TCP backend, we can access to TCP messages through the slave at address 255 (added by the constructor).
|
inherited |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inherited |
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.