Pyserial when should I use flush?

女生的网名这么多〃 提交于 2020-06-17 02:08:07

问题


I'am working with pyserial and trying to write and read data to and from an arduino board. I saw several examples showing that flush should be used while reading and writing. I couldn't understand what is the role of flush even after reading the pyserial documentation. I also noticed that there is:

flushInput()
flushOutput()
flush()

What is the role of each one, why and when should I use it. I'd appreciate the explanation.


回答1:


[Quotes from pySerial API documentation]

flushInput()

"Deprecated since version 3.0: see reset_input_buffer()"
"Flush input buffer, discarding all its contents."

Typically only used after changing the serial port parameters (e.g. port initialization) or for error recovery.


flushOutput()

"Deprecated since version 3.0: see reset_output_buffer()"
"Clear output buffer, aborting the current output and discarding all that is in the buffer.

Note, for some USB serial adapters, this may only flush the buffer of the OS and not all the data that may be present in the USB part."

Typically only used as part of an abort procedure.


Note: the underlying operating system may dictate the use of the above procedures.
On (modern) Linux systems the receive and transmit system buffers are usually cleared when the serial port/terminal is opened. That may not be the case with Windows.


flush()

"Flush of file like objects. In this case, wait until all data is written."

In POSIX termios jargon, this function is known as tcdrain().
Beware that "flush" and "drain" are sometimes (confusingly) used interchangeably for the "wait until all transmiting is done" function.
But the term "flush" can also be used to mean "discard" (e.g. as in the old flushXput() function names).
For example note the continued inconsistent use of the word "flush" in the pySerial API documentation even after the renaming of two functions to reduce confusion.
So whenever you see the word "flush", you should verify what it means (i.e. a destructive discard or nondestructive wait operation) in that context.

This flush() function typically is not needed in programs, especially when blocking I/O is used (i.e. the OS is buffering all data, and will automatically and efficiently have your program wait as necessary).
When using an OS your program is executing asynchronously with respect to the receiving and transmitting of data on the serial port. Only in abnormal circumstances (e.g. initialization, or error recovery, or half-duplex by software) should your program need to explicitly synchronize with actual data transfers.




回答2:


gross oversimplification follows

pyserial (serial in general) uses a communication buffer (actually 2)

one for input [ M | M | M | | | ]

and one for output [ | | | M | M ]

if one of these buffers is full nothing else can go on that buffer, and it might block program execution

flush ... tells the buffer to throw out its data, so new data can start coming in



来源:https://stackoverflow.com/questions/61596242/pyserial-when-should-i-use-flush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!