From: Martin Ling Date: Wed, 27 Nov 2013 02:55:18 +0000 (+0000) Subject: Add sp_input_waiting() and sp_output_waiting() functions. X-Git-Tag: libserialport-0.1.0~37 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=3353c22f326c0b5d2db4360b70554b2f00f94cf7;p=libserialport.git Add sp_input_waiting() and sp_output_waiting() functions. --- diff --git a/libserialport.h.in b/libserialport.h.in index ff2aaac..3d38a0d 100644 --- a/libserialport.h.in +++ b/libserialport.h.in @@ -827,6 +827,24 @@ enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t c */ enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count); +/** + * Gets the number of bytes waiting in the input buffer. + * + * @param port Pointer to port structure. + * + * @return Number of bytes waiting on success, a negative error code otherwise. + */ +enum sp_return sp_input_waiting(struct sp_port *port); + +/** + * Gets the number of bytes waiting in the output buffer. + * + * @param port Pointer to port structure. + * + * @return Number of bytes waiting on success, a negative error code otherwise. + */ +enum sp_return sp_output_waiting(struct sp_port *port); + /** * Flush serial port buffers. Data in the selected buffer(s) is discarded. * diff --git a/serialport.c b/serialport.c index e2cf48a..af8f1b1 100644 --- a/serialport.c +++ b/serialport.c @@ -1131,6 +1131,52 @@ enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count #endif } +enum sp_return sp_input_waiting(struct sp_port *port) +{ + TRACE("%p", port); + + CHECK_OPEN_PORT(); + + DEBUG("Checking input bytes waiting on port %s", port->name); + +#ifdef _WIN32 + DWORD errors; + COMSTAT comstat; + + if (ClearCommError(port->hdl, &errors, &comstat) == 0) + RETURN_FAIL("ClearComError() failed"); + RETURN_VALUE("%d", comstat.cbInQue); +#else + int bytes_waiting; + if (ioctl(port->fd, TIOCINQ, &bytes_waiting) < 0) + RETURN_FAIL("TIOCINQ ioctl failed"); + RETURN_VALUE("%d", bytes_waiting); +#endif +} + +enum sp_return sp_output_waiting(struct sp_port *port) +{ + TRACE("%p", port); + + CHECK_OPEN_PORT(); + + DEBUG("Checking output bytes waiting on port %s", port->name); + +#ifdef _WIN32 + DWORD errors; + COMSTAT comstat; + + if (ClearCommError(port->hdl, &errors, &comstat) == 0) + RETURN_FAIL("ClearComError() failed"); + RETURN_VALUE("%d", comstat.cbOutQue); +#else + int bytes_waiting; + if (ioctl(port->fd, TIOCOUTQ, &bytes_waiting) < 0) + RETURN_FAIL("TIOCOUTQ ioctl failed"); + RETURN_VALUE("%d", bytes_waiting); +#endif +} + #ifdef __linux__ static enum sp_return get_baudrate(int fd, int *baudrate) {