- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在读取这样的串行数据:
connected = False
port = 'COM4'
baud = 9600
ser = serial.Serial(port, baud, timeout=0)
while not connected:
#serin = ser.read()
connected = True
while True:
print("test")
reading = ser.readline().decode()
问题在于它阻止了其他任何东西的执行,包括bottle py web框架。添加 sleep()
不会有帮助。
将 "while True""更改为 "while ser.readline():"不会打印 "test",这很奇怪,因为它在 Python 2.7 中有效。有什么想法可能是错误的吗?
理想情况下,我应该只有在可用时才能读取串行数据。每 1000 毫秒发送一次数据。
最佳答案
完全没有必要使用单独的线程。只需按照下面的示例进行无限 while 循环即可。
我在我的 eRCaGuy_PyTerm 串行终端程序 here (search the code for inWaiting()
or in_waiting
) 中使用了这种技术。
注意事项:
要检查你的 python3 版本,运行这个:
python3 --version
当我第一次编写和测试这个答案时,我的输出是 Python 3.2.3
。
要检查你的 pyserial 库(serial
模块)版本,运行这个——我第一次学习这个 here :
python3 -c 'import serial; \
print("serial.__version__ = {}".format(serial.__version__))'
这只是导入 serial
模块并打印其 serial.__version__
属性。截至 2022 年 10 月,我的输出是:serial.__version__ = 3.5
。
如果您的 pyserial 版本是 3.0 或更高版本,请在下面的代码中使用 property in_waiting
。如果您的 pyserial 版本是 < 3.0,请在下面的代码中使用 function inWaiting()
。在此处查看官方 pyserial 文档:https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.in_waiting。
import serial
import time # Optional (required if using time.sleep() below)
ser = serial.Serial(port='COM4', baudrate=9600)
while (True):
# Check if incoming bytes are waiting to be read from the serial input
# buffer.
# NB: for PySerial v3.0 or later, use property `in_waiting` instead of
# function `inWaiting()` below!
if (ser.inWaiting() > 0):
# read the bytes and convert from binary array to ASCII
data_str = ser.read(ser.inWaiting()).decode('ascii')
# print the incoming string without putting a new-line
# ('\n') automatically after every print()
print(data_str, end='')
# Put the rest of your code you want here
# Optional, but recommended: sleep 10 ms (0.01 sec) once per loop to let
# other threads on your PC run during this time.
time.sleep(0.01)
这样你只有在有东西的时候才能阅读和打印。你说,“理想情况下,我应该只有在可用时才能读取串行数据。”这正是上面的代码所做的。如果没有可读取的内容,它会跳到 while 循环中的其余代码。完全无阻塞。
(此答案最初在此处发布和调试:Python 3 non-blocking read with pySerial (Cannot get pySerial's "in_waiting" property to work))
pySerial 文档:http://pyserial.readthedocs.io/en/latest/pyserial_api.html
更新:
in_waiting
与 inWaiting()
的评论。感谢@FurkanTürkal 在下面的评论中指出这一点。请参阅此处的文档:https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.in_waiting。尽管读取串行数据,如上所示,不需要需要使用多个线程,但以非阻塞方式读取键盘输入需要。因此,为了完成非阻塞键盘输入读取,我写了这个答案:How to read keyboard input?。
serial.Serial()
类API - https://pyserial.readthedocs.io/en/latest/pyserial_api.html 关于python - PySerial 非阻塞读取循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17553543/
我正在尝试使用 python 和 PySerial 编写一个在 Linux PC 中使用串行端口的应用程序。但是在这台 PC 中还有其他使用串行端口的应用程序。 在尝试使用某个端口之前,如何知道其他应
我这辈子都不知道如何使用我的 Raspberry Pi 在 Python 3 中进行非阻塞串行读取。 这是我的代码: import serial #for pySerial ser = serial.
我正在尝试从 PySerial 文档运行此示例程序以打开串行端口。来源:http://pyserial.sourceforge.net/shortintro.html我尝试在 python 版本 2.
我正在尝试从通过 USB 连接到运行 ubuntu 的 Linux 计算机的硬件设备中捕获数据。这是我目前拥有的非常简单的脚本: import serial ser = serial.Serial('
我正在尝试使用 PySerial 来接受来自 RFID 阅读器的输入。根据 here: 的答案,我尝试使用 WinObj 并发现一些奇怪的东西:COM3 文件夹中没有 GLOBAL??? 端口指向“更
我有一个函数,它接受一组值并将其转换为字节字符串。例如,我需要: input_array = ['E9', '01','06','57','4A','01','F4','01','01','EF']
我有一个函数,它接受一组值并将其转换为字节字符串。例如,我需要: input_array = ['E9', '01','06','57','4A','01','F4','01','01','EF']
大家好,我正在编写一个脚本,该脚本将使用数据通信标准从主机获取数据(开发者:视觉委员会数据通信标准委员会镜头处理部门),通过串行端口并将数据传递到 ModBus 协议(protocol)中,以便设备执
目前我有一个树莓派端口连接到两个串行设备。这些设备是 USB 到 RS485/RS422 转换器。两者都用电线连接到适当的端子(设备A的端子1到设备B的端子2,设备A的端子2到设备B的端子1)。 我能
我正在尝试编写一个 pyserial (v2.6) 代码,该代码应该无限期地等待来自使用 inWaiting() 指定的端口的任何输入,然后使用 read() 读取它 但没有运气,根本没有输出。我究竟
如果 COM 端口未连接,我试图让 python 失败: import serial ser = serial print("ermrmrmrr") try: ser = serial.Ser
通过 pySerial 打开串行/控制台端口时遇到一个小问题。我的程序旨在获取事件的 com 端口,打开控制台连接,然后发送数据。当程序运行并插入 RS232 USB 时,我收到 SerialExce
我对 Python 和一般编程很陌生。我正在使用 Arduino 和 python 为 Spotify 制作 Remote 。每当按下 Arduino 上的按钮时,就会通过串行发送一个字符。有一个 p
我正在尝试编写一个 python 程序,它可以使用 PySerial 模块通过串行接口(interface)进行通信,如下所示: import serial if __name__ == '__mai
有什么方法可以检测 Pyserial 中的 RS232 BREAK 条件吗?我可以看到发送中断的选项,但没有接收选项。 谢谢 最佳答案 我不知道有什么简单的方法可以做到这一点,尤其是在 python
我正在尝试通过 OS X 上的 Pyserial 与我的 Arduino 进行交互。我正在通过发送 0 到 9 之间的数字来控制 LED。代码如 import serial arduino = ser
我正在尝试编写一个连续读取串行输入的函数。该函数必须能够处理与串行端口的意外断开连接并在可能时重新连接。尽管阅读了 stackOverflow 上的几个问题帖子并查看了 pySerial 文档,但我还
我正在尝试以 Python 允许我通过串行端口的速度进行写入和读取。问题是我正在快速写入并且它不起作用。我相信问题出在串行缓冲区上,而且我可能过度写入了输入缓冲区。我可以增加输入和输出缓冲区吗?它们是
在没有任何波特率的情况下使用 python pyserial 初始化串行连接时,它会进入默认的 9600 波特配置 ser = serial.Serial('/dev/ttyUSB0') 这个 pys
我正在使用 Pyserial(一个 Python 串行库)从树莓派上的串行端口读取十六进制数据,操作系统是 raspbian。 我需要每次从串口读取一个字节,并立即处理数据,没有任何时间延迟。我现在这
我是一名优秀的程序员,十分优秀!