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