Skip to content

Commit a857fa3

Browse files
semaphore error counter
1 parent 9711be7 commit a857fa3

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

src/Modbus_TCP_Client_poll.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@
1919
namespace Modbus {
2020
namespace TCP {
2121

22+
//* maximum number of Modbus registers (per type)
2223
static constexpr int MAX_REGS = 0x10000;
2324

25+
//* value to increment error counter if semaphore could not be acquired
26+
static constexpr long SEMAPHORE_ERROR_INC = 10;
27+
28+
//* value to decrement error counter if semaphore could be acquired
29+
static constexpr long SEMAPHORE_ERROR_DEC = 1;
30+
31+
//* maximum value of semaphore error counter
32+
static constexpr long SEMAPHORE_ERROR_MAX = 1000;
33+
2434
//* maximum time to wait for semaphore (100ms)
2535
static constexpr struct timespec SEMAPHORE_MAX_TIME = {0, 100'000};
2636

@@ -379,10 +389,24 @@ Client_Poll::run_t Client_Poll::run(int signal_fd, bool reconnect, int timeout)
379389

380390
// handle request
381391
if (semaphore) {
382-
if (!semaphore->wait(SEMAPHORE_MAX_TIME))
392+
if (!semaphore->wait(SEMAPHORE_MAX_TIME)) {
383393
std::cerr << Print_Time::iso << " WARNING: Failed to acquire semaphore '"
384394
<< semaphore->get_name() << "' within 100ms." << std::endl;
395+
396+
semaphore_error_counter += SEMAPHORE_ERROR_INC;
397+
398+
if (semaphore_error_counter >= SEMAPHORE_ERROR_MAX) {
399+
std::cerr << Print_Time::iso << "ERROR: Repeatedly failed to acquire the semaphore"
400+
<< std::endl;
401+
close_con(client_addrs);
402+
return run_t::semaphore;
403+
}
404+
} else {
405+
semaphore_error_counter -= SEMAPHORE_ERROR_DEC;
406+
if (semaphore_error_counter < 0) semaphore_error_counter = 0;
407+
}
385408
}
409+
386410
int ret = modbus_reply(modbus, query, rc, mapping);
387411
if (semaphore && semaphore->is_acquired()) semaphore->post();
388412

src/Modbus_TCP_Client_poll.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ class Client_Poll {
2222
public:
2323
static constexpr std::size_t MAX_CLIENT_IDS = 256;
2424

25-
enum class run_t {
26-
term_signal,
27-
term_nocon,
28-
ok,
29-
timeout,
30-
interrupted,
31-
};
25+
enum class run_t { ok, term_signal, term_nocon, timeout, interrupted, semaphore };
3226

3327
private:
3428
const std::size_t max_clients;
@@ -43,6 +37,8 @@ class Client_Poll {
4337

4438
std::unique_ptr<cxxsemaphore::Semaphore> semaphore;
4539

40+
long semaphore_error_counter = 0;
41+
4642
public:
4743
/*! \brief create modbus client (TCP server)
4844
*

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ int main(int argc, char **argv) {
413413

414414
switch (ret) {
415415
case Modbus::TCP::Client_Poll::run_t::ok: continue;
416+
case Modbus::TCP::Client_Poll::run_t::semaphore:
416417
case Modbus::TCP::Client_Poll::run_t::term_signal: return;
417418
case Modbus::TCP::Client_Poll::run_t::term_nocon:
418419
std::cerr << Print_Time::iso << " INFO: No more active connections." << std::endl;

0 commit comments

Comments
 (0)