serial port - Python Program keeps crashing -


i'm trying create simple ui control whether can print data serial port , using pyqt, created simple ui 2 buttons- start recording button , stop recording button.

when press start recording button, data gets printed out, moment press stop recording button, system hangs , while loop keeps running. think might because program trying out of while loop, isn't able to. i'd appreciate help.

main code:

import gui_main import sys  import numpy  pyqt4 import qtcore, qtgui import pyqt4.qwt5 qwt import matplotlib import serial  import threading   global readervar  def reader():      readervar= true      ser = serial.serial(7) #insert com port number data being collected     ser.baudrate = 9600 #insert baud rate      while true:          if readervar:              line= ser.readline()             print (line)             print(readervar)         else if uiplot.btn2.clicked():              break     readervar=false   def shutter():      readervar=false  def main():      global uiplot     app = qtgui.qapplication(sys.argv)     win_plot = gui_main.qtgui.qmainwindow()     uiplot = gui_main.ui_form()     uiplot.setupui(win_plot)      uiplot.btn1.clicked.connect(reader)     uiplot.btn2.clicked.connect(shutter)     # display windows     win_plot.show()     code = app.exec_()      sys.exit(code)    if __name__ == "__main__":     main() 

gui code:

# -*- coding: utf-8 -*-  # form implementation generated reading ui file '.\form.ui' # # created: fri mar 20 15:17:05 2015 #      by: pyqt4 ui code generator 4.11.3 # # warning! changes made in file lost!  pyqt4 import qtcore, qtgui  try:     _fromutf8 = qtcore.qstring.fromutf8 except attributeerror:     def _fromutf8(s):         return s  try:     _encoding = qtgui.qapplication.unicodeutf8     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror:     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig)  class ui_form(object):     def setupui(self, form):         form.setobjectname(_fromutf8("form"))         form.resize(282, 192)         self.centralwidget = qtgui.qwidget(form)         self.centralwidget.setobjectname(_fromutf8("centralwidget"))         self.btn1 = qtgui.qpushbutton(self.centralwidget)         self.btn1.setgeometry(qtcore.qrect(20, 50, 91, 23))         self.btn1.setobjectname(_fromutf8("btn1"))         self.btn2 = qtgui.qpushbutton(self.centralwidget)         self.btn2.setgeometry(qtcore.qrect(160, 50, 91, 23))         self.btn2.setobjectname(_fromutf8("btn2"))         self.btn3 = qtgui.qpushbutton(self.centralwidget)         self.btn3.setgeometry(qtcore.qrect(110, 90, 75, 23))         self.btn3.setobjectname(_fromutf8("btn3"))         self.label = qtgui.qlabel(self.centralwidget)         self.label.setgeometry(qtcore.qrect(110, 20, 47, 13))         self.label.setobjectname(_fromutf8("label"))         form.setcentralwidget(self.centralwidget)         self.menubar = qtgui.qmenubar(form)         self.menubar.setgeometry(qtcore.qrect(0, 0, 282, 21))         self.menubar.setobjectname(_fromutf8("menubar"))         form.setmenubar(self.menubar)         self.maintoolbar = qtgui.qtoolbar(form)         self.maintoolbar.setobjectname(_fromutf8("maintoolbar"))         form.addtoolbar(qtcore.qt.toptoolbararea, self.maintoolbar)         self.statusbar = qtgui.qstatusbar(form)         self.statusbar.setobjectname(_fromutf8("statusbar"))         form.setstatusbar(self.statusbar)          self.retranslateui(form)         qtcore.qobject.connect(self.btn1, qtcore.signal(_fromutf8("clicked()")), self.label.update)         qtcore.qobject.connect(self.btn2, qtcore.signal(_fromutf8("clicked()")), self.label.update)         qtcore.qobject.connect(self.btn3, qtcore.signal(_fromutf8("clicked()")), form.close)         qtcore.qmetaobject.connectslotsbyname(form)      def retranslateui(self, form):         form.setwindowtitle(_translate("form", "form", none))         self.btn1.settext(_translate("form", "start recording", none))         self.btn2.settext(_translate("form", "stop recording", none))         self.btn3.settext(_translate("form", "exit", none))         self.label.settext(_translate("form", "textlabel", none))   if __name__ == "__main__":     import sys     app = qtgui.qapplication(sys.argv)     form = qtgui.qmainwindow()     ui = ui_form()     ui.setupui(form)     form.show()     sys.exit(app.exec_()) 

i think may misunderstand happening execution of program.

pyqt (and gui) applications have event loop running. in case of pyqt, started loop app.exec_(). code below line never runs until gui application stopped. because python (deliberately) stuck in loop within exec_() method.

so, might ask, how of code run if program stuck in event loop? event loop (as name suggests) there listen events button clicks, mouse/keyboard interaction, etc. event loop responsible creating hover effect when move mouse on button.

when click first button, event loop goes , runs function reader(). function has infinite loop in it. such, control of application never returned pyqt event loop. means can't click on other button. hover effect won't show if move mouse on buttons in application. gui applications rely on control being returned event loop in order stay responsive.

so can never click second button stop loop in reader() because loop preventing other code running.

the solution put reader() loop in thread. setting threads beyond scope of answer though. there plenty of resources on using both python threads , (py)qt qthreads on internet (including on stackoverflow). note threads come risks when used gui applications. instance, must make sure never interact gui directly secondary thread.

if run problems using threads encourage post question showing how far have got, , stating problem having. i'll happy help!


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -