From fd8fd11a4e60a06b0d15f2c31df18752ee2d3563 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Wed, 20 Nov 2013 17:22:50 +0000 Subject: [PATCH] Make sp_flush take an option for what to flush. --- libserialport.h.in | 16 ++++++++++++++-- serialport.c | 20 +++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/libserialport.h.in b/libserialport.h.in index fa5ba1a..12e9cba 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -109,6 +109,16 @@ enum sp_mode { SP_MODE_NONBLOCK = 4, }; +/** Buffer selection. */ +enum sp_buffer { + /** Input buffer. */ + SP_BUF_INPUT = 1, + /** Output buffer. */ + SP_BUF_OUTPUT = 2, + /** Both buffers. */ + SP_BUF_BOTH = 3, +}; + /** Parity settings. */ enum sp_parity { /** Special value to indicate setting should be left alone. */ @@ -506,12 +516,14 @@ enum sp_return sp_read(struct sp_port *port, void *buf, size_t count); enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count); /** - * Flush serial port buffers. + * Flush serial port buffers. Data in the selected buffer(s) is discarded. + * + * @param buffers Which buffer(s) to flush. * * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG * if an invalid port is passed. */ -enum sp_return sp_flush(struct sp_port *port); +enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers); /** * @} diff --git a/serialport.c b/serialport.c index 353cd34..92156b4 100644 --- a/serialport.c +++ b/serialport.c @@ -505,17 +505,31 @@ enum sp_return sp_close(struct sp_port *port) return SP_OK; } -enum sp_return sp_flush(struct sp_port *port) +enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers) { CHECK_PORT(); #ifdef _WIN32 + DWORD flags = 0; + if (buffers & SP_BUF_INPUT) + flags |= PURGE_RXCLEAR; + if (buffers & SP_BUF_OUTPUT) + flags |= PURGE_TXCLEAR; + /* Returns non-zero upon success, 0 upon failure. */ - if (PurgeComm(port->hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) + if (PurgeComm(port->hdl, flags) == 0) return SP_ERR_FAIL; #else + int flags = 0; + if (buffers & SP_BUF_BOTH) + flags = TCIOFLUSH; + else if (buffers & SP_BUF_INPUT) + flags = TCIFLUSH; + if (buffers & SP_BUF_OUTPUT) + flags = TCOFLUSH; + /* Returns 0 upon success, -1 upon failure. */ - if (tcflush(port->fd, TCIOFLUSH) < 0) + if (tcflush(port->fd, flags) < 0) return SP_ERR_FAIL; #endif return SP_OK; -- 2.11.4.GIT