我正在运行一个带有图表的GUI,该图表更新速度非常快,而且我的工作效果很好。最近我在我的实验中添加了另一个组件,需要通过RS-232进行串行通信。当我使用root.after()
方法来调用从串行设备读取数据的函数时,主循环停止。所以我想我会尝试将它移到另一个线程来试图避免这种情况,但似乎并没有帮助。然后根据评论我将它分成几个不同的回调,以避免昂贵的while循环。性能现在更好,但它仍然停滞不前。如何在不拖延root.mainloop()
?
以下是新代码:已编辑:
def callback_PCreadWrapper():
print 'Starting thread'
thread.start_new_thread(callback_PCread1,())
def callback_PCread1():
global ser, um1, trash, status,delay
ser.write('e')
while ser.inWaiting() > 0:
trash += ser.read(1)
ser.write('A')
root.after(500,callback_PCread2)
def callback_PCread2()
trash=''
trash = ser.read(2)
current_status = ser.read(1)
if current_status == " ":
status = 'No alarms'
elif current_status == "!":
status = 'Sensor Fail'
elif current_status == "$":
status = "Count Alarm"
else:
status = current_status
trash += ser.read(24)
um1 = ser.read(6)
while ser.inWaiting() > 0:
trash += ser.read(1)
print 'Ending Thread'
root.after(10000,callback_PCreadWrapper)
Canvas.bind_all("<End>",callback_end)
root.after(samplePeriod, writeData) # status and 1um are written to a file
root.after(10000, callback_PCreadWrapper)
root.mainloop()
如果你想查看github上的所有代码here is the LONG version