Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Thread for serial Reading shows an error
#1
I created a python interface to display some information on the screen while collecting data through the serial port. The communication is made with an ATmega micro-controller. The serial port should send data once I hit the Start button, then it sends a command character to the micro-controller ('g' = go). If I hit the Close button the data should stop being sent (Close button sends another command character, 's' = stop).

I know that it was not possible to create another loop inside the program when using tkinter (can cause crashes and malfunctioning). The solution would be create threads. So I created a Thread to read the data in parallel of tkinter loop. Each block of data sent ends with a '\n', so I had to use the readline() command.

Here is the simplified code


from tkinter import *
from tkinter import ttk
import time
import serial
import threading

ser = serial.Serial(
        port = 'COM3', 
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        timeout=None,
        bytesize= serial.EIGHTBITS)

ser.flushInput()
if ser.isOpen() == False:
    ser.open()

def raise_frame(frame):
    frame.tkraise()

def finish():

    ser.write('s'.encode())
    time.sleep(1)
    ser.close()
    root.destroy()

def start():
    ser.write('g'.encode())
    thread.start()

def readSerial():
    while True:
        DATA = ser.readline()
        print(DATA)

def force_close(event):
    ser.write('s'.encode())
    time.sleep(1)
    ser.close()
    root.destroy()


root = Tk()
root.attributes("-fullscreen", True)
w, h = root.winfo_screenwidth(), root.winfo_screenheight()

f1 = Frame(root)
f1.place(x=0,y=0, relheight=1, relwidth=1)

# =============================================================================
# Start Button
# =============================================================================
btn_start=Button(f1, text="Start",font = ("Arial Bold",30), command = start)
btn_start.pack(padx=10,pady=30, side = BOTTOM)

# =============================================================================
# Close Button
# =============================================================================
close_value=BooleanVar()
btn_close=Button(f1, text="Close",font = ("Arial Bold",30), command=finish)
btn_close.pack(padx=10,pady=30, side = BOTTOM)

thread=threading.Thread(target=readSerial)
thread.daemon=True 
root.bind('<Escape>',force_close)
raise_frame(f1)
root.mainloop()
The problem is: everytime that I close the application this error appears:

Exception in thread Thread-8:
Traceback (most recent call last):
  File "C:\Users\apoco\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\apoco\Anaconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "<ipython-input-2-838b466f09c9>", line 41, in readSerial
    DATA = ser.readline()
  File "C:\Users\apoco\Anaconda3\lib\site-packages\serial\serialwin32.py", line 269, in read
    win32.ResetEvent(self._overlapped_read.hEvent)
AttributeError: 'NoneType' object has no attribute 'hEvent'
Does anyone knows how to handle with this error?

Best Regards,

Emmanuel
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Tkinter GUI is unresponsive in a thread sandarbhasthana 2 252 May-06-2019, 03:20 PM
Last Post: sandarbhasthana
  run executable in separate process thread shift838 1 320 Jan-30-2019, 02:03 PM
Last Post: Alfalfa
  Thread/Signal LittleGrim13 2 674 Nov-30-2018, 02:35 PM
Last Post: LittleGrim13
  [WxPython] Using thread in the wxPython hildogjr 8 1,621 Aug-11-2018, 02:59 AM
Last Post: hildogjr
  Read Pins by UART Serial Communication Tonyita 0 530 May-06-2018, 10:13 PM
Last Post: Tonyita
  [Tkinter] Problem reading entry box in thread LeeMadeux 2 934 Jan-17-2018, 03:15 PM
Last Post: LeeMadeux
  Widget placement issues with tkinter grid thread 1 mgtheboss 2 1,309 Jan-09-2018, 03:59 PM
Last Post: SmokerX

Forum Jump:


Users browsing this thread: 1 Guest(s)