0

I am having difficulty reading serial data coming from an arduino over a serial connection. In order to get around the issue of serial and the gui needing to be running simultansouly, I am using the .after function to call the update serial every 100ms. However, when I run this code, I get no window popping up, and I get an error saying I have exceeded the max recursion depth. Here is my code:

'''
Created on Nov 23, 2014

@author: Charlie
'''

if __name__ == '__main__':
    pass

import serial
from tkinter import *

ser = serial.Serial('COM8')
ser.baudrate = 9600

def update():
    c = StringVar()
    c=ser.readline()
    theta.set(c)
    root.after(100,update())

root=Tk()
theta = StringVar()

w = Label(root, textvariable = theta)
w.pack()

root.after(100,update())    
root.mainloop()
Charlie
  • 11
  • 2

2 Answers2

0

You should use root.after(100, update). Notice the lack of parentheses after update. Using the parentheses sends the result of update to the after call, but to calculate the result, update has to be run which contains another call to after which needs the result of update and so on..

Also see this question.

Also, why do you make a new StringVar every time the update function is called?
c = ser.readline() overwrites c anyway, so you may just as well remove that line.

Community
  • 1
  • 1
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
-1

In func update() remove cycle root.after(100,update()). This:

def update():
    c = StringVar()
    c=ser.readline()
    theta.set(c)
TinnyT
  • 147
  • 1
  • 1
  • 5
  • My intent is to reset the next call for update when I call that. Otherwise, wouldn't my update only get called Once? Once right before the call to main loop. There would be no infinite loop of calling it again and again. – Charlie Nov 24 '14 at 06:52
  • If you're read to data in func `update()` to better update the label. This: 'w.after(100, update())', if you're func return the value. – TinnyT Nov 24 '14 at 08:57