Linking Live data from an Arduino to a LCDNumber from pyqt5 with python3.5

前端 未结 1 873
不思量自难忘°
不思量自难忘° 2021-01-17 02:35

I am fairly new to making a GUI. I designed one for a project that I would like to have LCDs update as the data comes in from the Arduino. I am using pyqt5 to convert my Qtd

1条回答
  •  粉色の甜心
    2021-01-17 03:03

    For the correct operation I have modified the name of some variables because they do not match their use. In order to send information between elements of PyQt it is advisable to use signals and slots, for this you must create a signal in QThread that sends the information that is required to be displayed in the GUI for this we use pyqtSignal() where we indicate the types of parameters that go to send.

    class GetData(QThread):
        dataChanged = pyqtSignal(float, float, float, float)
        Distance = 0.5
    
        def __init__(self, parent=None):
            QThread.__init__(self, parent)
    
            arduino_ports = [  # automatically searches for an Arduino and selects the port it's on
                p.device
                for p in serial.tools.list_ports.comports()
                if 'Arduino' in p.description
            ]
    
            if not arduino_ports:
                raise IOError("No Arduino found - is it plugged in? If so, restart computer.")
            if len(arduino_ports) > 1:
                warnings.warn('Multiple Arduinos found - using the first')
            self.Arduino = serial.Serial(arduino_ports[0], 9600, timeout=1)
    
        def __del__(self):  # part of the standard format of a QThread
            self.wait()
    
        def run(self):  # also a required QThread function, the working part
            self.Arduino.close()
            self.Arduino.open()
    
            self.Arduino.flush()
            self.Arduino.reset_input_buffer()
            start_time = time.time()
    
            while True:
                while self.Arduino.inWaiting() == 0:
                    pass
                try:
                    data = self.Arduino.readline()
                    dataarray = data.decode().rstrip().split(',')
                    self.Arduino.reset_input_buffer()
                    Force = round(float(dataarray[0]), 3)
                    RPM = round(float(dataarray[1]), 3)
                    Torque = round(Force * GetData.Distance, 3)
                    HorsePower = round(Torque * RPM / 5252, 3)
                    Run_Time = round(time.time() - start_time, 3)
                    print(Force, 'Grams', ",", RPM, 'RPMs', ",", Torque, "ft-lbs", ",", HorsePower, "hp", Run_Time,
                          "Time Elasped")
                    self.dataChanged.emit(RPM, Torque, HorsePower, Run_Time)
                except (KeyboardInterrupt, SystemExit, IndexError, ValueError):
                    pass
    

    Qt Designer provides a design, does not provide a widget, for that it is best to create a class inherit from a widget and use the design, create an instance of QThread and connect the dataChanged signal to some slot, and in that slot is where we place the values in the QLCDNumber.

    class GUI(QWidget, Ui_DynoTest1):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.setupUi(self)
            self.thread = GetData(self)
            self.thread.dataChanged.connect(self.onDataChanged)
            self.thread.start()
    
        def onDataChanged(self, RPM, Torque, HorsePower, Run_Time):
            self.lcdNumber_4.display(RPM)
            self.lcdNumber_5.display(Torque)
            self.lcdNumber_6.display(HorsePower)
            self.lcdNumber_7.display(Run_Time)
    
    
    if __name__ == '__main__':
        import sys
        app = QApplication(sys.argv)
        Dyno = GUI()
        Dyno.show()
        sys.exit(app.exec_())
    

    0 讨论(0)
提交回复
热议问题