[Tutor] python gtk serial loop thread readind data close

Alexandru Achim achim_alexandru at yahoo.com
Thu Mar 30 08:40:41 EDT 2017


Dear users,
I had a problem regarding Threads in python and Gtk3. I want to stop a while loop in Gtk , a loop starded with a thread.
I want to control a delay timer laser board with give me ,when I send a command by serial connection, give back continuous status values ; but I want to stop this loop , to modify parameters and start the loop again. 

A part of my code is here :
import serial
import threading
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject,Pango, GLib
from threading import Thread
GObject.threads_init().............
#START BUTTON

    def start(self,button_start):
       Thread(target=self.start_on,args=(self.button_start))
     
           
    def start_on(self,widget,data=None):
    if ser.isOpen():
        cmd = 's'    
        ser.write(cmd.encode('ascii')+'\r\n')
        while True:
            try:
                 values = ser.readline()
                 self.label_result.set_text(values)
            except:
                      pass
        
#STOP BUTTON         
    def stop(self,button_stop):
     Thread(target=self.stops,args=(button_stop)).start()
        
    def stops(self,widget,data=None):
    if ser.isOpen():
        try:   
            cmd = 'f'    
            ser.write(cmd.encode('ascii')+'\r\n')
            status = ser.readline()
            self.label_result.set_text(status)
        except:
            pass
...........win=sincrolaser()
win.show_all()
Gtk.main()

But the problem is when I send STOP button program freeze , probably the loop from the start is looping forever. How do I stop the start thread? 
Is there a posibility to give a thread flag or identity to find him and close?
 Thank you,Alex  

---------
Eng.ALEXANDRU ACHIM
National Institute for Lasers, Plasma and Radiation Physics  
Center for Advance Laser Technologies - PW Laser Facility  


More information about the Tutor mailing list