2016-11-07 101 views
1

我有一個程序,充當幾個arduino板卡的GUI。我試圖在動態圖中顯示傳感器的結果。如何多次執行while循環?

問題:爲什麼我的GUI單元不能打印5次? - 它並沒有圖形工作

我的最終結果的想法是這樣的: enter image description here 不幸的是我的結果是:

enter image description here 我似乎無法弄清楚,爲什麼我的代碼沒有打印GUI 5次。 這是我的圖形代碼:

# pre-load dummy data 
    for i in range(0, 30): 
     self.values.append(0) 

    for a in range(0, 30): 
     self.values2.append(0) 

    while True: 
     while (self.serialArduino.inWaiting() == 0): 
      pass 
     self.data = self.serialArduino.readline().decode('utf-8') # maakt connectie met arduino 

     self.valueRead, self.valueRead2 = [int(x.encode()) for x in self.data.split(",")] 

     self.valueInInt = int(self.valueRead) 
     self.valueInInt2 = int(self.valueRead2) 
     print("lux %d" % self.valueInInt) 
     print("temp %d" % self.valueInInt2) 

     self.values.append(self.valueInInt) 
     self.values.pop(0) 
     self.values2.append(self.valueInInt2) 
     self.values2.pop(0) 
     drawnow(self.plotValues) 

     self.f.tight_layout(pad=0.8, w_pad=0.5, h_pad=1.0) 
     self.canvas = FigureCanvasTkAgg(self.f, master=self.frame1,) 
     self.canvas.get_tk_widget().grid(row=8, column=0, columnspan=2) 

def plotValues(self): 
    self.f = plt.Figure(figsize=(4, 5), dpi=90) 
    self.a = self.f.add_subplot(211) 
    self.a.plot(self.values2, 'bx-', label='TEMP') 
    self.a.set_title('Tempertuur') 
    self.a.set_xlabel('Uren') 
    self.a.set_ylabel('Celsius') 
    self.a.grid(True) 


    self.b = self.f.add_subplot(212) 
    self.b.plot(self.values, 'rx-', label='LUX') 
    self.b.set_title('Lux') 
    self.b.set_xlabel('Uren') 
    self.b.set_ylabel('Lichtintensiteit') 
    self.b.grid(True) 

的構造函數類,它包含的圖形:

self.values = [] 
    self.values2 = [] 

    self.plt = plt.ion() 
    self.cnt = 0 

    self.serialArduino = serial.Serial('COM7', 9600) 

    self.frame1 = Frame(master) 
    self.frame1.pack(side=LEFT) 

GUI本身是從不同類別的呼叫:

class GUI_Root: 
def __init__(self): 
    print("hoi") 
    window = Tk() 
    window.title("Project: Embedded Systems") 

    rootframe = Frame(window, width=1800, height=750) 
    rootframe.pack() 
    for y in range(0, 5): 
     Unit(rootframe) 

    window.mainloop() 

回答

0

我已經找到了解決方案。它可能不是代碼,但它可以工作。 每個Gui單元代表一個Arduino單元。所以我檢查一下是否使用了comport。對於每個正在使用的comport,都會創建一個Gui單元。這解決了我的問題。

class GUI_Root: 
def __init__(self): 
    print("hoi") 
    window = Tk() 
    print("test1") 
    window.title("Project: Embedded Systems") 
    print("test2") 
    rootframe = Frame(window, width=1800, height=750) 
    rootframe.pack() 


    try: 
     Unit(rootframe, 'COM3') 

    except: print("Com3 is niet aangesloten") 

    try: 
     Unit(rootframe, 'COM4') 
    except: 
     print("Com4 is niet aangesloten") 

    try: 
     Unit(rootframe, 'COM5') 
    except: 
     print("Com5 is niet aangesloten") 

    try: 
     Unit(rootframe, 'COM6') 
    except: 
     print("Com6 is niet aangesloten") 

    try: 
     Unit(rootframe, 'COM7') 
    except: 
     print("Com7 is niet aangesloten") 

    try: 
     Unit(rootframe, 'COM8') 
    except: 
     print("Com8 is niet aangesloten") 

    window.mainloop() 

該問題出現在GUI_Root類中。