windows 刷新通信句柄接收缓冲区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/707613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:14:52  来源:igfitidea点击:

Flush communications handle receive buffer?

cwindowswinapi

提问by RBerteig

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.

在 Win32 C 中是否有一个 API 调用来刷新(转储)COM 端口接收缓冲区的内容?我只能找到刷新传输缓冲区的函数。

回答by RBerteig

`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()'可以删除 Tx 和 Rx 缓冲区中的一个或两个中的所有字符,并中止端口上任何挂起的读和/或写操作。要对端口执行所有操作,请说以下内容:

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().

您可能还想确保您已经处理或明确忽略了端口上的任何未决错误,可能使用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.

ReadFile()可用于通过将所有可用字节读入废物缓冲区来仅清空 Rx 缓冲区和 FIFO。请注意,您可能需要具有“非自然”知识才能正确调整缓冲区大小,或者重复ReadFile()调用直到它无话可说。

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

但是,读取缓冲区以刷新它只有在您首先“合理地”设置COMMTIMEOUTS时才有意义,否则读取将阻塞直到缓冲区被填满。

回答by RBerteig

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

刷新接收缓冲区没有意义,要从 com 端口接收缓冲区中获取数据,只需在 com 端口的句柄上调用 ReadFile

FlushFileBuffers synchronously forces the transmission of data in the transmit buffers

FlushFileBuffers 同步强制传输缓冲区中的数据

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

PurgeComm 在没有传输或接收的情况下清空缓冲区(基本上是删除)