From: Martin Ling Date: Wed, 20 Nov 2013 17:30:50 +0000 (+0000) Subject: Add sp_drain() function. X-Git-Tag: libserialport-0.1.0~84 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=69a3739ca07a87d56e376a02f3bc1f0a0a9b64e7;p=libserialport.git Add sp_drain() function. --- diff --git a/libserialport.h.in b/libserialport.h.in index e866cab..79f315f 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -526,6 +526,14 @@ enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count); */ enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers); +/** + * Wait for buffered data to be transmitted. + * + * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + * if an invalid port is passed. + */ +enum sp_return sp_drain(struct sp_port *port); + /** * @} * @defgroup Errors Obtaining error information diff --git a/serialport.c b/serialport.c index 92156b4..6104f24 100644 --- a/serialport.c +++ b/serialport.c @@ -535,6 +535,23 @@ enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers) return SP_OK; } +enum sp_return sp_drain(struct sp_port *port) +{ + CHECK_PORT(); + +#ifdef _WIN32 + /* Returns non-zero upon success, 0 upon failure. */ + if (FlushFileBuffers(port->hdl) == 0) + return SP_ERR_FAIL; +#else + /* Returns 0 upon success, -1 upon failure. */ + if (tcdrain(port->fd) < 0) + return SP_ERR_FAIL; +#endif + + return SP_OK; +} + enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count) { CHECK_PORT();