Make sp_flush take an option for what to flush.

This commit is contained in:
Martin Ling 2013-11-20 17:22:50 +00:00 committed by Bert Vermeulen
parent a036341bdf
commit fd8fd11a4e
2 changed files with 31 additions and 5 deletions

View File

@ -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);
/**
* @}

View File

@ -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;