tags:

views:

607

answers:

2

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.

A: 

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)

Yes but if you use read file you are dependant on COMMTIMEOUTS, and these slow my application. There is junk left in the buffers that I don't want to read. If I use read file the read will wait, or I will have to continually change COMMTIMEOUTS which windows only does so fast.
the terms flush and dump in context refer to actually moving or saving the buffer not invalidating/purging it, especially considering how you used flush in connection with the transmit buffer, which absolutely does not just invalidate the buffer but rather guarantees transmission
+1  A: 

`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.

RBerteig