From: Daniel Thompson Date: Thu, 19 Sep 2013 15:39:24 +0000 (+0100) Subject: serial: Only sleep when no characters are received. X-Git-Tag: libsigrok-0.2.2~51 X-Git-Url: http://sigrok.org/gitweb/?a=commitdiff_plain;h=5715e84fe3335e519148d7f6252b046598a982a8;p=libsigrok.git serial: Only sleep when no characters are received. g_usleep(XX) sleeps for *at least* XX microseconds but may sleep for longers (on older kernels the sleep will typically be 10000us). Thus byte receive loops containing an unconditional sleep will perform very poorly (for example it causes the scan in agilent-dmm to timeout prematurely). Even on modern kernels serial_readline() has a 2ms sleep per byte which means it will read at a maximum rate of half a character per millisecond (~4800baud). This is fixed by only sleeping when read() returns no data. Signed-off-by: Daniel Thompson --- diff --git a/hardware/common/serial.c b/hardware/common/serial.c index 306b889c..557022c9 100644 --- a/hardware/common/serial.c +++ b/hardware/common/serial.c @@ -786,7 +786,8 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, if (g_get_monotonic_time() - start > timeout_ms) /* Timeout */ break; - g_usleep(2000); + if (len < 1) + g_usleep(2000); } if (*buflen) sr_dbg("Received %d: '%s'.", *buflen, *buf); @@ -867,7 +868,8 @@ SR_PRIV int serial_stream_detect(struct sr_serial_dev_inst *serial, sr_dbg("Detection timed out after %dms.", time); break; } - g_usleep(byte_delay_us); + if (len < 1) + g_usleep(byte_delay_us); } *buflen = ibuf;