79577176

Date: 2025-04-16 12:28:17
Score: 2
Natty:
Report link

You're reading data too fast, and the serial input is not guaranteed to end cleanly with \n before your code tries to process it, and that’s why you are getting incomplete or "corrupted" lines.

1. Use a dedicated thread for serial reading

Tkinter is single-threaded. If you read from a serial in the same thread as the GUI, the GUI slows down (and vice versa), especially when you move the window.

Run the serial reading in a separate thread, and put the valid data into a queue.Queue() which the GUI can safely read from.

import threading
import queue

data_queue = queue.Queue()

def serial_read_thread():
    read_Line = ReadLine(ser)
    while True:
        try:
            line = read_Line.readline().decode('utf-8').strip()
            parts = [x for x in line.split(",") if x]
            if len(parts) >= 21:
                data_queue.put(parts)
            else:
                print("⚠️ Invalid line, skipped:", parts)
        except Exception as e:
            print(f"Read error: {e}")

Start this thread once at the beginning:

t = threading.Thread(target=serial_read_thread, daemon=True)

t.start()

2. In your Tkinter loop, check the queue

Use after() in Tkinter to periodically fetch from the queue and update the UI:

def update_gui_from_serial():
    try:
        while not data_queue.empty():
            data = data_queue.get_nowait()
            print(data)
    except queue.Empty:
        pass
    root.after(50, update_gui_from_serial)  

Please let me know if this works and if you need any further help! :)

Reasons:
  • RegEx Blacklisted phrase (2.5): Please let me know
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Najem