python - pySerial:刷新 vs reset_input_buffer + reset_output_buffer

标签 python serial-port pyserial flush

我正在尝试使用 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/

相关文章:

python - 减少嵌套循环的计算时间

java - Eclipse + Jython : Python module not found using one-to-one object factory

c# - 有没有办法以 2-3 mbps 的速度使用串行端口?

java - Windows10 jssc.SerialPortException : openPort(); Exception type - Port busy

Python 属性错误 : module 'serial' has no attribute 'Serial'

python - 在python中计算十六进制校验和

python - python下 move 文件

python - Pyserial 缓冲区填充速度快于我的阅读速度

Python 序列号 : How to use the read or readline function to read more than 1 character at a time

python - pyserial 中的 inter_byte_timeout (interCharTimeout) 是什么?