Эх сурвалжийг харах

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.
Stéphane Raimbault 14 жил өмнө
parent
commit
89d0dc0137
2 өөрчлөгдсөн 17 нэмэгдсэн , 8 устгасан
  1. 12 4
      src/modbus-rtu.c
  2. 5 4
      src/modbus.c

+ 12 - 4
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;

+ 5 - 4
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;
         }
     }