From: Martin Ling Date: Sun, 21 Sep 2014 18:35:39 +0000 (+0100) Subject: serial_readline: Implement using sp_blocking_read. X-Git-Tag: libsigrok-0.4.0~985 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=bf505eed6b01f8283d9d1c526199d8e6128cf95a;p=libsigrok.git serial_readline: Implement using sp_blocking_read. --- diff --git a/src/serial.c b/src/serial.c index 4943bb10..83df6a34 100644 --- a/src/serial.c +++ b/src/serial.c @@ -530,7 +530,7 @@ SR_PRIV int serial_set_paramstr(struct sr_serial_dev_inst *serial, SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, int *buflen, gint64 timeout_ms) { - gint64 start; + gint64 start, remaining; int maxlen, len; if (!serial) { @@ -543,8 +543,8 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, return -1; } - timeout_ms *= 1000; start = g_get_monotonic_time(); + remaining = timeout_ms; maxlen = *buflen; *buflen = len = 0; @@ -552,7 +552,7 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, len = maxlen - *buflen - 1; if (len < 1) break; - len = serial_read(serial, *buf + *buflen, 1); + len = sp_blocking_read(serial->data, *buf + *buflen, 1, remaining); if (len > 0) { *buflen += len; *(*buf + *buflen) = '\0'; @@ -563,7 +563,9 @@ SR_PRIV int serial_readline(struct sr_serial_dev_inst *serial, char **buf, break; } } - if (g_get_monotonic_time() - start > timeout_ms) + /* Reduce timeout by time elapsed. */ + remaining = timeout_ms - ((g_get_monotonic_time() - start) / 1000); + if (remaining <= 0) /* Timeout */ break; if (len < 1)