Sep-20-2018, 01:44 AM
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
Best Regards,
Emmanuel
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