python - PySerial in TkInter - How to time different in and outputs? -
i have serial port receives 1 data point every second.
now use gui tkinter contains graph in matplotlib plots received data points in real time. @ same time still want use gui perform other basic things such inputs , buttons calculations past data points etc.
i wondering efficient way update plot , @ same time listen serial port every second while still allowing gui , python program flawlessly work in background.
i thinking using functions , call getserialdata function every second mainloop since i'm new python don't know how structure works , if guys give me short outline how should embed main code.
thanks!
edit #2: ok, got work. looks messy, there maybe better way this? don't these different objects , self time, or of necessary?
from tkinter import * import serial class serialimport: def __init__(self, master): #master imports root window self.printbutton = button(text="test gui response", command=self.printmessage) self.printbutton.grid(row=0) self.openbutton = button(text="open serial port", command=self.openport) self.openbutton.grid(row=1) self.closebutton = button(text="close serial port", command=self.closeport) self.closebutton.grid(row=2) self.updatebutton = button(text="read , print data", command=self.update) self.updatebutton.grid(row=3) def printmessage(self): print("gui respond!") def openport(self): self.arduinoserialdata = serial.serial('com3', 9600) def closeport(self): self.arduinoserialdata.close() def update(self): while(1==1): if (self.arduinoserialdata.inwaiting()>0): mydata = self.arduinoserialdata.readline() self.temperature = float(mydata) print(self.temperature) root.after(1000, self.update) break root = tk() s = serialimport(root) #root gets treated master in serialimport class root.mainloop()
Comments
Post a Comment