Skip to content

Commit 29d592e

Browse files
committed
serial: Properly flush the input buffer
Calling sp_flush() does not seem to be enough to flush the input buffer with some serial-to-USB adapters. Signed-off-by: Paul Cercueil <[email protected]>
1 parent 2945479 commit 29d592e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

serial.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,10 @@ static struct iio_context * serial_create_context(const char *port_name,
421421
struct sp_port *port;
422422
struct iio_context_pdata *pdata;
423423
struct iio_context *ctx;
424-
char *description;
424+
char *description, *uri, buf[16];
425425
size_t uri_len;
426426
unsigned int i;
427427
int ret;
428-
char *uri;
429428

430429
uri_len = sizeof("serial:,1000000,8n1n") + strnlen(port_name, PATH_MAX);
431430
uri = malloc(uri_len);
@@ -452,8 +451,20 @@ static struct iio_context * serial_create_context(const char *port_name,
452451
goto err_close_port;
453452
}
454453

455-
/* Empty the buffers */
456-
sp_flush(port, SP_BUF_BOTH);
454+
/* Empty the output buffer */
455+
ret = libserialport_to_errno(sp_flush(port, SP_BUF_OUTPUT));
456+
if (ret)
457+
IIO_WARNING("Unable to flush output buffer\n");
458+
459+
/* Drain the input buffer */
460+
do {
461+
ret = libserialport_to_errno(sp_blocking_read(port, buf,
462+
sizeof(buf), 1));
463+
if (ret < 0) {
464+
IIO_WARNING("Unable to drain input buffer\n");
465+
break;
466+
}
467+
} while (ret);
457468

458469
description = serial_get_description(port);
459470
if (!description)

0 commit comments

Comments
 (0)