首页 > 解决方案 > 从 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。

标签: pythonmultithreading

解决方案


推荐阅读