Pyserial 什么时候应该使用flush? [英] Pyserial when should I use flush?

查看:251
本文介绍了Pyserial 什么时候应该使用flush?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pyserial 并尝试在 arduino 板上写入和读取数据.我看到几个例子表明在阅读和写作时应该使用flush.即使阅读了 pyserial 文档,我也无法理解 flush 的作用是什么.我还注意到有:

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.

推荐答案

[引自 pySerial API 文档]

flushInput()

flushInput()

自 3.0 版起已弃用:请参阅 reset_input_buffer()"
刷新输入缓冲区,丢弃其所有内容."

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

刷新输出()

自 3.0 版起已弃用:请参阅 reset_output_buffer()"
"清除输出缓冲区,中止当前输出并丢弃缓冲区中的所有内容.

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

请注意,对于某些 USB 串行适配器,这可能只会刷新操作系统的缓冲区,而不是 USB 部分中可能存在的所有数据."

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.

注意:底层操作系统可能会规定使用上述程序.
在(现代)Linux 系统上,当串行端口/终端打开时,接收和发送系统缓冲区通常会被清除.Windows 可能不是这种情况.

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 of file like objects. In this case, wait until all data is written."

在 POSIX termios 术语中,此函数称为 tcdrain().
请注意,冲洗"和排出"有时(令人困惑地)可互换用于等待所有传输完成"功能.
但术语flush"也可以用来表示丢弃"(例如,在旧的 flushXput() 函数名称中).
例如,请注意即使在重命名两个函数以减少混淆之后,pySerial API 文档中flush"一词的使用仍然不一致.
因此,每当您看到刷新"一词时,您都应该验证它在该上下文中的含义(即破坏性丢弃或非破坏性等待操作).

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.

这个 flush() 函数通常在程序中是不需要的,尤其是在使用阻塞 I/O 时(即操作系统正在缓冲所有数据,并且会自动有效地让您的程序在必要时等待).
使用操作系统时,您的程序在串行端口上接收和传输数据时异步执行.只有在异常情况下(例如初始化、错误恢复或软件半双工),您的程序才需要与实际数据传输显式同步.

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.

这篇关于Pyserial 什么时候应该使用flush?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆