Python Forum
[Tkinter] Thread for serial Reading shows an error
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
Reply


Messages In This Thread
Thread for serial Reading shows an error - by eabs86 - Sep-20-2018, 01:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Kivy] Kivy pop up shows duplicate buttons from main screen CEKinch 0 1,921 Feb-05-2021, 05:40 PM
Last Post: CEKinch
  [Kivy] Kivy text label won't shows up! AVD_01 1 2,894 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [PyQt] Ubuntu Taskbar icon shows title as "Unknown" while hover davidmuni 3 3,686 Jan-28-2020, 01:13 PM
Last Post: davidmuni
  [Tkinter] Problem reading entry box in thread LeeMadeux 2 3,301 Jan-17-2018, 03:15 PM
Last Post: LeeMadeux

Forum Jump:

User Panel Messages

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