Skip to content

Commit

Permalink
serial: Properly flush the input buffer
Browse files Browse the repository at this point in the history
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 <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Jan 30, 2023
1 parent 2945479 commit 29d592e
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,10 @@ static struct iio_context * serial_create_context(const char *port_name,
struct sp_port *port;
struct iio_context_pdata *pdata;
struct iio_context *ctx;
char *description;
char *description, *uri, buf[16];
size_t uri_len;
unsigned int i;
int ret;
char *uri;

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

/* Empty the buffers */
sp_flush(port, SP_BUF_BOTH);
/* Empty the output buffer */
ret = libserialport_to_errno(sp_flush(port, SP_BUF_OUTPUT));
if (ret)
IIO_WARNING("Unable to flush output buffer\n");

/* Drain the input buffer */
do {
ret = libserialport_to_errno(sp_blocking_read(port, buf,
sizeof(buf), 1));
if (ret < 0) {
IIO_WARNING("Unable to drain input buffer\n");
break;
}
} while (ret);

description = serial_get_description(port);
if (!description)
Expand Down

0 comments on commit 29d592e

Please sign in to comment.