Skip to content

Commit 0fec5bf

Browse files
authored
Separate flush/drain a bit better (#58)
1 parent 773bb16 commit 0fec5bf

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,17 @@ Set the state of a hardware pin of the UART. See *simple_uart_get_pin*
204204
```c
205205
int simple_uart_flush(struct simple_uart *uart);
206206
```
207-
Ensure all outstanding data has been written to the wire and is not being buffered by the OS.
207+
Discard all pending input and output data buffered by the OS without waiting for it to be transmitted.
208+
209+
210+
### Drain
211+
```c
212+
int simple_uart_drain(struct simple_uart *uart);
213+
```
214+
Block until all pending output data has been transmitted. Unlike *simple_uart_flush*, this does not discard data — it waits for the transmit buffer to empty.
215+
216+
#### Return:
217+
*0* on success. *< 0* on failure.
208218
209219
210220
### Break

simple_uart.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,8 @@ int simple_uart_flush(struct simple_uart *uart)
986986
if (!uart)
987987
return -EINVAL;
988988

989-
#if defined(__linux__)
990-
if (ioctl(uart->fd, TCFLSH, TCIOFLUSH) < 0)
991-
return -errno;
992-
#elif defined(__APPLE__)
993-
if (tcdrain(uart->fd) < 0)
989+
#ifndef _WIN32
990+
if (tcflush(uart->fd, TCIOFLUSH) < 0)
994991
return -errno;
995992
#else
996993
DWORD purge_all = PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR;
@@ -999,3 +996,18 @@ int simple_uart_flush(struct simple_uart *uart)
999996
#endif
1000997
return 0;
1001998
}
999+
1000+
int simple_uart_drain(struct simple_uart *uart)
1001+
{
1002+
if (!uart)
1003+
return -EINVAL;
1004+
1005+
#ifndef _WIN32
1006+
if (tcdrain(uart->fd) < 0)
1007+
return -errno;
1008+
#else
1009+
if (!FlushFileBuffers(uart->port))
1010+
return win32_errno();
1011+
#endif
1012+
return 0;
1013+
}

simple_uart.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ int simple_uart_get_pin(struct simple_uart *uart, int pin);
7777
int simple_uart_set_pin(struct simple_uart *uart, int pin, bool high);
7878

7979
/**
80-
* Make sure all write data has been flushed out of the UART
80+
* Make sure all read/write data has been flushed (discarded) out of the UART
8181
*/
8282
int simple_uart_flush(struct simple_uart *uart);
8383

84+
/**
85+
* Block until all pending output has been transmitted
86+
*/
87+
int simple_uart_drain(struct simple_uart *uart);
88+
8489
/**
8590
* Send the break symbol
8691
*/

0 commit comments

Comments
 (0)