Skip to content

Commit 878a1ea

Browse files
print set timeout values
1 parent c33bf3c commit 878a1ea

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/Modbus_TCP_Slave.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ struct timeout_t {
9292
};
9393

9494
static inline timeout_t double_to_timeout_t(double timeout) {
95-
timeout_t ret{};
95+
timeout_t ret {};
9696

9797
ret.sec = static_cast<uint32_t>(timeout);
9898

9999
double fractional = timeout - static_cast<double>(ret.sec);
100-
ret.usec = static_cast<uint32_t>(fractional * 1000.0 * 1000.0);
100+
ret.usec = static_cast<uint32_t>(fractional * 1000.0 * 1000.0);
101101

102102
return ret;
103103
}
104104

105105
void Slave::set_byte_timeout(double timeout) {
106-
const auto T = double_to_timeout_t(timeout);
107-
auto ret = modbus_set_byte_timeout(modbus, T.sec, T.usec);
106+
const auto T = double_to_timeout_t(timeout);
107+
auto ret = modbus_set_byte_timeout(modbus, T.sec, T.usec);
108108

109109
if (ret != 0) {
110110
const std::string error_msg = modbus_strerror(errno);
@@ -113,14 +113,40 @@ void Slave::set_byte_timeout(double timeout) {
113113
}
114114

115115
void Slave::set_response_timeout(double timeout) {
116-
const auto T = double_to_timeout_t(timeout);
117-
auto ret = modbus_set_response_timeout(modbus, T.sec, T.usec);
116+
const auto T = double_to_timeout_t(timeout);
117+
auto ret = modbus_set_response_timeout(modbus, T.sec, T.usec);
118118

119119
if (ret != 0) {
120120
const std::string error_msg = modbus_strerror(errno);
121121
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
122122
}
123123
}
124124

125+
double Slave::get_byte_timeout() {
126+
timeout_t timeout {};
127+
128+
auto ret = modbus_get_byte_timeout(modbus, &timeout.sec, &timeout.usec);
129+
130+
if (ret != 0) {
131+
const std::string error_msg = modbus_strerror(errno);
132+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
133+
}
134+
135+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
136+
}
137+
138+
double Slave::get_response_timeout() {
139+
timeout_t timeout {};
140+
141+
auto ret = modbus_get_response_timeout(modbus, &timeout.sec, &timeout.usec);
142+
143+
if (ret != 0) {
144+
const std::string error_msg = modbus_strerror(errno);
145+
throw std::runtime_error("modbus_receive failed: " + error_msg + ' ' + std::to_string(errno));
146+
}
147+
148+
return static_cast<double>(timeout.sec) + (static_cast<double>(timeout.usec) / (1000.0 * 1000.0));
149+
}
150+
125151
} // namespace TCP
126152
} // namespace Modbus

src/Modbus_TCP_Slave.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace TCP {
1414
//! Modbus TCP slave
1515
class Slave {
1616
private:
17-
modbus_t *modbus; //!< modbus object (see libmodbus library)
17+
modbus_t * modbus; //!< modbus object (see libmodbus library)
1818
modbus_mapping_t *mapping; //!< modbus data object (see libmodbus library)
1919
bool delete_mapping; //!< indicates whether the mapping object was created by this instance
2020
int socket = -1; //!< socket of the modbus connection
@@ -28,7 +28,7 @@ class Slave {
2828
*/
2929
explicit Slave(const std::string &ip = "0.0.0.0",
3030
short unsigned int port = 502,
31-
modbus_mapping_t *mapping = nullptr);
31+
modbus_mapping_t * mapping = nullptr);
3232

3333
/*! \brief destroy the modbus slave
3434
*
@@ -70,6 +70,18 @@ class Slave {
7070
*/
7171
void set_response_timeout(double timeout);
7272

73+
/**
74+
* \brief get byte timeout in seconds
75+
* @return byte timeout
76+
*/
77+
double get_byte_timeout();
78+
79+
/**
80+
* \brief get response timeout in seconds
81+
* @return response timeout
82+
*/
83+
double get_response_timeout();
84+
7385
/*! \brief get the modbus socket
7486
*
7587
* @return socket of the modbus connection

src/main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,18 @@ int main(int argc, char **argv) {
189189

190190
// set timeouts if required
191191
try {
192-
if (args.count("response-timeout")) {
193-
slave->set_response_timeout(args["response-timeout"].as<double>());
194-
}
192+
if (args.count("response-timeout")) { slave->set_response_timeout(args["response-timeout"].as<double>()); }
195193

196-
if (args.count("byte-timeout")) {
197-
slave->set_response_timeout(args["byte-timeout"].as<double>());
198-
}
194+
if (args.count("byte-timeout")) { slave->set_response_timeout(args["byte-timeout"].as<double>()); }
199195
} catch (const std::runtime_error &e) {
200196
std::cerr << e.what() << std::endl;
201197
exit(EX_SOFTWARE);
202198
}
203199

200+
// print timeouts
201+
std::cerr << " Byte timeout: " << slave->get_byte_timeout() << "s" << std::endl;
202+
std::cerr << "Response timeout: " << slave->get_response_timeout() << "s" << std::endl;
203+
204204
// connection loop
205205
do {
206206
// connect client

src/modbus_shm.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Shm_Mapping {
2626
std::string name = std::string(); //!< name of the object
2727
int fd = -1; //!< file descriptor
2828
std::size_t size; //!< size in bytes
29-
void *addr = nullptr; //!< mapped address
29+
void * addr = nullptr; //!< mapped address
3030
};
3131

3232
//! modbus lib storage object

0 commit comments

Comments
 (0)