Python Forum
Synchronizing tkinter with serial baud rate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Synchronizing tkinter with serial baud rate
#1
I am creating an appplication, for displaying my arduino serial data that i receive.

However, my application has bugs.

Sometimes, after some time, the program gets characters mixed.

Here, i am uploading a picture:

[Image: 4IUulnu]
https://imgur.com/4IUulnu

The white part is my tkinter frame, the black is the terminal. As you can see, in the beginning everything looks good, but then, characters that should not be there start to appear.

This is the code for this:

def readSerial():
    global after_id
    try:
       ser_bytes = ser.readline()
       print(ser_bytes)
       ser_bytes = ser_bytes.decode("utf-8")
       print(ser_bytes)
    except UnicodeExceptionError:
       print("Oops")
    text.insert("end", ser_bytes)
    after_id=root.after(50,readSerial)
This behavior does not always happen (which is weird), but when it does, i have noticed that the text response is slow. So when i change the value of the potentiometer, it does not instatnly appear in the next measurement, but appears two-three measurements later.

So i suspect that after a while, all this buffered data gets accumulated and starts to mess with my program. But even if this was the case, i do not understand why it is happenning sometimes, and not always.

One of my assumptioms, is an asynchronicity between the baud rate, and the repeatability of the function that performs all the serial read - and tkinter writing.

What i mean isthat i have set up my serial object like this:
ser = serial.Serial(port = COMPort, baudrate=9600, timeout=0.1)
The baud rate is 9600

While the tkinter function repeats every 50 ms:
 after_id=root.after(50,readSerial)
The function keeps repeating after 50 ms.
Maybe 50 is not a correct value, when having 9600 baud rate.

Does anyone know the root of this problem? And also, why this problem happens, but not all the time?
Is my assumption correct?
If i am using 9600 baud rate, then what the correct value for the perdio of the readSerial() function?
Reply


Messages In This Thread
Synchronizing tkinter with serial baud rate - by xbit - Apr-25-2021, 01:21 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020