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;