myforwik
myforwik

Reputation:

Flush communications handle receive buffer?

In Win32 C is there an API call to flush (dump) the contents of a COM port recieve buffer? I could only find functions to flush the transmit buffers.

Upvotes: 3

Views: 9763

Answers (2)

RBerteig
RBerteig

Reputation: 43356

`PurgeComm()' can drop all characters in either or both the Tx and Rx buffers, and abort any pending read and/or write operations on the port. To do everything to a port, say something like:

PurgeComm(hPort, PURGE_RXABORT|PURGE_TXABORT|PURGE_RXCLEAR|PURGE_TXCLEAR) 

You may also want to make sure that you've handled or explicitly ignored any pending errors on the port as well, probably with ClearCommError().

ReadFile() can be used to empty just the Rx buffer and FIFO by reading all available bytes into a waste buffer. Note that you might need to have "unnatural" knowledge in order to size that buffer correctly, or repeat the ReadFile() call until it has no more to say.

However, reading the buffer to flush it will only make sense if you have the COMMTIMEOUTS set "rationally" first or the read will block until the buffer is filled.

Upvotes: 8

mark
mark

Reputation:

flushing a receive buffer doesn't make sense, to get data out of a com port receive buffer just call ReadFile on the handle to the com port

FlushFileBuffers synchronously forces the transmission of data in the transmit buffers

PurgeComm empties the buffer without transmission or reception (its a delete basically)

Upvotes: 0

Related Questions