从continuous_threading.PeriodicThread 获取特定字符串然后执行操作

问题描述

我是 Python 新手。我有一个脚本,它使用从 Arduino 到 RaspBerry Pi 4 的串行通信。我正在研究一个距离监控系统。我在 Arduino 中有一个超声波距离传感器,我正在通过串行通信将读数从 Arduino 传递到树莓派。我想获得一个特定的字符串,就像传感器在 raspBerry pi 的脚本中检测到物体靠近传感器一样。我的代码是这样的:(不要担心 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。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)