gpt4 book ai didi

python - pySerial:刷新 vs reset_input_buffer + reset_output_buffer

转载 作者:行者123 更新时间:2023-12-05 02:56:09 24 4
gpt4 key购买 nike

我正在尝试使用 pySerial==3.4,但发现关于 serial.Serial.flush() 的文档相当缺乏:

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

Source


问题

  • 什么是“文件类对象”?
  • 正在刷新什么?
  • 什么时候会使用 flush 而不是单独重置输入/输出缓冲区?
serial = Serial("COM3")

# Option 1
serial.flush()

# Option 2
serial.reset_input_buffer()
serial.reset_output_buffer()

相关问题

最佳答案

看起来像这样:

什么是“文件类对象”?

What is exactly a file-like object in Python?

file-like objects are mainly StringIO objects, connected sockets and well.. actual file objects. If everything goes fine, urllib.urlopen() also returns a file-like objekt supporting the necessary methods.

file-like object
A synonym for file object.

file object
An object exposing a file-oriented API (with methods such as read() or write()) to an underlying resource. Depending on the way it was created, a file object can mediate access to a real on-disk file or to another type of storage or communication device (for example standard input/output, in-memory buffers, sockets, pipes, etc.). File objects are also called file-like objects or streams.

There are actually three categories of file objects: raw binary files, buffered binary files and text files. Their interfaces are defined in the io module. The canonical way to create a file object is by using the open() function.

io — Core tools for working with streams

The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These are generic categories, and various backing stores can be used for each of them. A concrete object belonging to any of these categories is called a file object. Other common terms are stream and file-like object.

刷新了什么?

保存在输出缓冲区中的数据。

什么时候会使用刷新而不是单独重置输入/输出缓冲区?

有数据已经​​输出(write()),关闭前调用。

flush() 与输入缓冲区或 reset_input_buffer() 无关。

flush()reset_output_buffer() 有不同的功能。
flush()将输出缓冲区中的所有数据发送给对端,而reset_output_buffer()丢弃输出缓冲区中的数据。

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.

关于python - pySerial:刷新 vs reset_input_buffer + reset_output_buffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com