Commit 89d0dc01372c8648792f3d02b99d86d093755c31

Authored by Stéphane Raimbault
1 parent 33d92434

Fix longstanding limitation of server to wait forever

The change for serial on Windows is temporary. If you're interested of
improving the situation for this, please have a look at the FIXME.
src/modbus-rtu.c
@@ -193,10 +193,18 @@ static int win32_ser_select(struct win32_ser *ws, int max_len, @@ -193,10 +193,18 @@ static int win32_ser_select(struct win32_ser *ws, int max_len,
193 return 1; 193 return 1;
194 } 194 }
195 195
196 - /* Setup timeouts like select() would do */  
197 - msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;  
198 - if (msec < 1)  
199 - msec = 1; 196 + /* Setup timeouts like select() would do.
  197 + FIXME Please someone on Windows can look at this?
  198 + Does it possible to use WaitCommEvent?
  199 + When tv is NULL, MAXDWORD isn't infinite!
  200 + */
  201 + if (tv == NULL) {
  202 + msec = MAXDWORD;
  203 + } else {
  204 + msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  205 + if (msec < 1)
  206 + msec = 1;
  207 + }
200 208
201 comm_to.ReadIntervalTimeout = msec; 209 comm_to.ReadIntervalTimeout = msec;
202 comm_to.ReadTotalTimeoutMultiplier = 0; 210 comm_to.ReadTotalTimeoutMultiplier = 0;
src/modbus.c
@@ -302,6 +302,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) @@ -302,6 +302,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
302 int rc; 302 int rc;
303 fd_set rfds; 303 fd_set rfds;
304 struct timeval tv; 304 struct timeval tv;
  305 + struct timeval *p_tv;
305 int length_to_read; 306 int length_to_read;
306 int msg_length = 0; 307 int msg_length = 0;
307 _step_t step; 308 _step_t step;
@@ -327,16 +328,15 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) @@ -327,16 +328,15 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
327 if (msg_type == MSG_INDICATION) { 328 if (msg_type == MSG_INDICATION) {
328 /* Wait for a message, we don't know when the message will be 329 /* Wait for a message, we don't know when the message will be
329 * received */ 330 * received */
330 - /* FIXME Not infinite */  
331 - tv.tv_sec = 60;  
332 - tv.tv_usec = 0; 331 + p_tv = NULL;
333 } else { 332 } else {
334 tv.tv_sec = ctx->response_timeout.tv_sec; 333 tv.tv_sec = ctx->response_timeout.tv_sec;
335 tv.tv_usec = ctx->response_timeout.tv_usec; 334 tv.tv_usec = ctx->response_timeout.tv_usec;
  335 + p_tv = &tv;
336 } 336 }
337 337
338 while (length_to_read != 0) { 338 while (length_to_read != 0) {
339 - rc = ctx->backend->select(ctx, &rfds, &tv, length_to_read); 339 + rc = ctx->backend->select(ctx, &rfds, p_tv, length_to_read);
340 if (rc == -1) { 340 if (rc == -1) {
341 return -1; 341 return -1;
342 } 342 }
@@ -403,6 +403,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type) @@ -403,6 +403,7 @@ static int receive_msg(modbus_t *ctx, uint8_t *msg, msg_type_t msg_type)
403 byte_timeout */ 403 byte_timeout */
404 tv.tv_sec = ctx->byte_timeout.tv_sec; 404 tv.tv_sec = ctx->byte_timeout.tv_sec;
405 tv.tv_usec = ctx->byte_timeout.tv_usec; 405 tv.tv_usec = ctx->byte_timeout.tv_usec;
  406 + p_tv = &tv;
406 } 407 }
407 } 408 }
408 409