python - 从 Continuous_threading.PeriodicThread 获取特定字符串,然后执行操作
问题描述
我是python的新手。我有一个脚本,它使用从 Arduino 到 Raspberry Pi 4 的串行通信。我正在开发一个远程监控系统。我在 Arduino 中有一个超声波距离传感器,我通过串行通信将读数从 Arduino 传递到树莓派。我想获得一个特定的字符串,例如传感器是否检测到对象靠近树莓派脚本中的传感器。我的代码是这样的:(不要担心 tkinter GUI 和所有导入,它工作正常)
ser = serial.Serial('dev/ttyACM0', 19200)
val1 = 0
index = []
def readserial():
global val1
ser_bytes = ser.readline()
print(ser_bytes.rstrip())
val1 = ser_bytes
index.append(val1)
if len(index) == 1:
display1 = tk.Label(screen, text = index[0])
index.clear() # I have only one line in the GUI so it replaces the text every 0.5 interval
screen = Tk()
screen.title("Anti-collision")
t1 = continuous_threading.PeriodicThread(0.5, readserial)
t1.start()
screen.mainloop()
如果传感器检测到距离传感器更近 1 英寸的物体并且读取串行输出字符串“OBJECT CLOSER”,我想显示另一个 GUI。
解决方案
推荐阅读
- javascript - 遍历嵌套的 JSON 对象
- mysql - 如何获取每个用户的最后一行
- c# - 如何从列表中获取指定值
和 T 是一个泛型类? - c++ - VS2013 探查器不断关闭我的应用程序
- c# - 为什么在尝试播放功能性 .wav 时 SoundPlayer 会静默失败?
- python - Pandas - 在列中找到某个值后选择行
- influxdb - InfluxDB 2.0 如何找出正在执行的查询数量
- rust - Rust 社区中迭代结构的字段并更改循环中的另一个字段的标准方法是什么?
- python - 可以在 odoo 视图中显示当前的 partner.id
- python - 如何从兄弟类配置 tkinter wiget 中的文本?