从continuous\u threading.periodicthread获取特定字符串,然后执行操作

guykilcj  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(203)

我是python的新手。我有一个脚本,它使用从arduino到raspberry pi 4的串行通信。我在做一个远程监控系统。我在arduino有一个超声波距离传感器,我通过串行通信把arduino的读数传给raspberry pi。我想得到一个特定的字符串,比如如果传感器在raspberry pi脚本中检测到一个对象靠近传感器。我的代码是这样的:(不要担心tkintergui和所有的导入,它工作得很好)

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英寸的物体,并且readserial输出字符串“objectcloser”,我想展示另一个gui。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题