Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,17 @@ Set the state of a hardware pin of the UART. See *simple_uart_get_pin*
```c
int simple_uart_flush(struct simple_uart *uart);
```
Ensure all outstanding data has been written to the wire and is not being buffered by the OS.
Discard all pending input and output data buffered by the OS without waiting for it to be transmitted.


### Drain
```c
int simple_uart_drain(struct simple_uart *uart);
```
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.

#### Return:
*0* on success. *< 0* on failure.


### Break
Expand Down
22 changes: 17 additions & 5 deletions simple_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,8 @@ int simple_uart_flush(struct simple_uart *uart)
if (!uart)
return -EINVAL;

#if defined(__linux__)
if (ioctl(uart->fd, TCFLSH, TCIOFLUSH) < 0)
return -errno;
#elif defined(__APPLE__)
if (tcdrain(uart->fd) < 0)
#ifndef _WIN32
if (tcflush(uart->fd, TCIOFLUSH) < 0)
return -errno;
#else
DWORD purge_all = PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR;
Expand All @@ -999,3 +996,18 @@ int simple_uart_flush(struct simple_uart *uart)
#endif
return 0;
}

int simple_uart_drain(struct simple_uart *uart)
{
if (!uart)
return -EINVAL;

#ifndef _WIN32
if (tcdrain(uart->fd) < 0)
return -errno;
#else
if (!FlushFileBuffers(uart->port))
return win32_errno();
#endif
return 0;
}
7 changes: 6 additions & 1 deletion simple_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ int simple_uart_get_pin(struct simple_uart *uart, int pin);
int simple_uart_set_pin(struct simple_uart *uart, int pin, bool high);

/**
* Make sure all write data has been flushed out of the UART
* Make sure all read/write data has been flushed (discarded) out of the UART
*/
int simple_uart_flush(struct simple_uart *uart);

/**
* Block until all pending output has been transmitted
*/
int simple_uart_drain(struct simple_uart *uart);

/**
* Send the break symbol
*/
Expand Down