0

my project is to build a scanner that send the sensor value on Serial port. I want to control the scanner using a GUI Tkinter, so I want to check if something is coming on the serial continuously. However, I used the .after() function that works when something is sent but when the scanner is "waiting" and nothing is sent, the GUI freezes and I can't to do anything until something is sent. Thanks,

Here's the code triggered by the main button:

def Acqard():
   global flag
   flag = 1
   arduinoData.write(b'1')
   log.insert(END, "Début du scan\n")
   root.after(10, write_data)

And here are the functions that save the DATA in a txt file:

def write_data():
    global file_stream
    file_stream = open("data.txt", "a") # mode write ou append ?
    write_lines()


def write_lines():
    global after_id
    data = arduinoData.read()
    try:
        data2 = data.decode("utf-8")
        file_stream.write(data2)
        print(data2)
        if data2 == "F":
            root.after_cancel(after_id)
            print("Ca a marché")
            stopacq()
        after_id = root.after(10, write_data)

    except:
       return

And here's the function that stops the scanner:

def stopacq():
   global flag, file_stream
   flag = 0
   root.after_cancel(after_id)# Annulation des rappels automatiques de write_lines
   file_stream.close()
   file_stream = None
   arduinoData.write(b'3')
   log.insert(END, "Scan interrompu\n")
martineau
  • 119,623
  • 25
  • 170
  • 301
Armczbt
  • 21
  • 7
  • Which call to `after()` is causing the freeze? – martineau Mar 11 '20 at 08:32
  • when the function `Acqard()` is called. – Armczbt Mar 11 '20 at 08:36
  • @Armczbt This: `arduinoData.read()` is blocking. Read [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Mar 11 '20 at 08:43
  • I thought about using Threads, but I can't stop a thread, so it won't stop reading Serial... – Armczbt Mar 11 '20 at 08:56
  • 1
    If you are using `pyserial`, you can set `timeout` option when creating the `Serial` instance. It may reduce the freezing period. – acw1668 Mar 11 '20 at 09:20
  • You can't stop them, but you can [pause them](https://stackoverflow.com/a/15734837/355230). – martineau Mar 11 '20 at 09:21
  • Also see [Trying to fix tkinter GUI freeze-ups (using threads)](https://stackoverflow.com/questions/53525746/trying-to-fix-tkinter-gui-freeze-ups-using-threads). – martineau Mar 11 '20 at 09:25
  • I really don't understand how to fix this... I'm kinda stuck at this moment... Thanks for trying to help me – Armczbt Mar 18 '20 at 19:29

0 Answers0