From 89d0dc01372c8648792f3d02b99d86d093755c31 Mon Sep 17 00:00:00 2001 From: Stéphane Raimbault Date: Fri, 6 May 2011 19:06:59 +0200 Subject: [PATCH] Fix longstanding limitation of server to wait forever --- src/modbus-rtu.c | 16 ++++++++++++---- src/modbus.c | 9 +++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/modbus-rtu.c b/src/modbus-rtu.c index 2e0ca77..a21ea28 100644 --- a/src/modbus-rtu.c +++ b/src/modbus-rtu.c @@ -193,10 +193,18 @@ static int win32_ser_select(struct win32_ser *ws, int max_len, return 1; } - /* Setup timeouts like select() would do */ - msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; - if (msec < 1) - msec = 1; + /* Setup timeouts like select() would do. + FIXME Please someone on Windows can look at this? + Does it possible to use WaitCommEvent? + When tv is NULL, MAXDWORD isn't infinite! + */ + if (tv == NULL) { + msec = MAXDWORD; + } else { + msec = tv->tv_sec * 1000 + tv->tv_usec / 1000; + if (msec < 1) + msec = 1; + } comm_to.ReadIntervalTimeout = msec; comm_to.ReadTotalTimeoutMultiplier = 0; diff --git a/src/modbus.c b/src/modbus.c index 8f210ce..51ff551 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -302,6 +302,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) int rc; fd_set rfds; struct timeval tv; + struct timeval *p_tv; int length_to_read; int msg_length = 0; _step_t step; @@ -327,16 +328,15 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) if (msg_type == MSG_INDICATION) { /* Wait for a message, we don't know when the message will be * received */ - /* FIXME Not infinite */ - tv.tv_sec = 60; - tv.tv_usec = 0; + p_tv = NULL; } else { tv.tv_sec = ctx->response_timeout.tv_sec; tv.tv_usec = ctx->response_timeout.tv_usec; + p_tv = &tv; } while (length_to_read != 0) { - rc = ctx->backend->select(ctx, &rfds, &tv, length_to_read); + rc = ctx->backend->select(ctx, &rfds, p_tv, length_to_read); if (rc == -1) { return -1; } @@ -403,6 +403,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) byte_timeout */ tv.tv_sec = ctx->byte_timeout.tv_sec; tv.tv_usec = ctx->byte_timeout.tv_usec; + p_tv = &tv; } } -- libgit2 0.21.4