PyQT4, Python27 - Serial connection and global variables
Question: I think my issue is simple, but I have hit a brick wall. I am not a programmer
but needed a program to control a laser engraver via Arduino. My Original code
was mostly working, but I wanted the ability to select a serial port with a
QComboBox so I can make it software available for everyone.

I figured out how to do that with the code below:

    
    
    import sys
    import serial
    import time
    import serial.tools.list_ports
    from PyQt4 import QtGui
    from window_test import Ui_MainWindow
    
    
    class Main(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.btn_laser_poweron.clicked.connect(self.btnFIRE)
            self.ui.btn_laser_poweroff.clicked.connect(self.btnOFF)
            self.ui.btn_lig_power.clicked.connect(self.btnLIG)
            self.ui.btn_cutting_power.clicked.connect(self.btnCUT)
            self.ui.btn_power_meter.clicked.connect(self.btnTEST)
            self.ui.spinBox.valueChanged.connect(self.PwrLevel)
            self.ui.comboBox.activated.connect(self.srlprt)
    
    
        def srlprt(self):
            serial.Serial(str(self.ui.comboBox.currentText()))
        def btnFIRE(self):
            ser.write("a" + chr(255))
        def btnOFF(self):
            ser.write("b" + chr(0))
        def btnTEST(self):
            ser.write("c" + chr(0))
            time.sleep(59.5)
            ser.write("d" + chr(255))
        def btnLIG(self):
            ser.write("e" + chr(29))
        def btnCUT(self):
            ser.write("f" + chr(160))
        def PwrLevel(self):
            val = self.ui.spinBox.value()
            ser.write("g" + chr(val))
    
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = Main()
        window.show()
        sys.exit(app.exec_())
    

Now my problem is that none of my buttons work because "ser" is not globally
defined. I understand that I broke that when I removed "ser =
serial.Serial(port=COM3)" when it was above the class definition, but I don't
know how to fix it. Any help would be greatly appreciated.

Cheers!


Answer: A simple solution would be to just set `ser` as attribute of your `Main`
instance. Also it couldn't hurt to close the serial connection if it is open
before opening a new one, e.g:

    
    
    import sys
    import serial
    import time
    import serial.tools.list_ports
    from PyQt4 import QtGui
    from window_test import Ui_MainWindow
    
    
    class Main(QtGui.QMainWindow):
        def __init__(self):
            QtGui.QMainWindow.__init__(self)
            self.ser = None
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)
            self.ui.btn_laser_poweron.clicked.connect(self.btnFIRE)
            self.ui.btn_laser_poweroff.clicked.connect(self.btnOFF)
            self.ui.btn_lig_power.clicked.connect(self.btnLIG)
            self.ui.btn_cutting_power.clicked.connect(self.btnCUT)
            self.ui.btn_power_meter.clicked.connect(self.btnTEST)
            self.ui.spinBox.valueChanged.connect(self.PwrLevel)
            self.ui.comboBox.activated.connect(self.srlprt)
    
    
        def srlprt(self):
            if self.ser:
                self.ser.close()
            self.ser = serial.Serial(str(self.ui.comboBox.currentText()))
        def btnFIRE(self):
            self.ser.write("a" + chr(255))
        def btnOFF(self):
            self.ser.write("b" + chr(0))
        def btnTEST(self):
            self.ser.write("c" + chr(0))
            time.sleep(59.5)
            self.ser.write("d" + chr(255))
        def btnLIG(self):
            self.ser.write("e" + chr(29))
        def btnCUT(self):
            self.ser.write("f" + chr(160))
        def PwrLevel(self):
            val = self.ui.spinBox.value()
            self.ser.write("g" + chr(val))
    
    
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        window = Main()
        window.show()
        sys.exit(app.exec_())
    

