title: Connect to serial from a PyQt GUI
body: <p>I write a program to that send and recive data from serial, but I have a problem, I want to create a function "connect()" or a class, and when I press a button, the function is executed, but if I create this function in "MainWindow" class, variable "ser" from "TestThread" class become uninitialized, can you help me?</p>
  
  <pre><code>import sys
  import serial
  
  
  from PyQt5.QtWidgets import QMainWindow, QApplication
  from PyQt5.QtCore import QThread, pyqtSignal
  from PyQt5.uic import loadUi
  
  
  ser = serial.Serial('/dev/tty.usbmodem14201', 9600, timeout=1)
  
  class TestThread(QThread):
      serialUpdate = pyqtSignal(str)
      def run(self):
          while ser.is_open:
              QThread.sleep(1)
              value = ser.readline().decode('ascii')
              self.serialUpdate.emit(value)
              ser.flush()
  
  class MainWindow(QMainWindow):
      def __init__(self):
          super(MainWindow, self).__init__()
          loadUi('/Users/bogdanvesa/P2A_GUI/mainwindow.ui', self)
          self.thread = TestThread(self)
          self.thread.serialUpdate.connect(self.handleSerialUpdate)
  
          self.connect_btn.clicked.connect(self.connectSer)
          self.lcd_EBtn.clicked.connect(self.startThread)
  
      def startThread(self):
          self.thread.start()
  
      def handleSerialUpdate(self, value):
          print(value)
          self.lcd_lineEdit.setText(value)
  
  
  def main():
  
      app = QApplication(sys.argv)
      form = MainWindow()
      form.show()
      sys.exit(app.exec_())
  
  if __name__ == "__main__":
      main()
  </code></pre>