合并pyserial和pyside2 / pyqt5 [重复]

问题描述 投票:0回答:1

我正在编写代码以具有通过串行连接控制蠕动泵的GUI。

[首先,我开发了一个类“ Pump”,在一本jupyter书中可以很好地工作。其次,我使用PySide2 / PyQt5开发了一个GUI。

““>

通过合并两者,我遇到了一些问题。

这是最后一个有效的代码:

import sys
import math
from serial import Serial
from PySide2.QtWidgets import QApplication, QDialog, QGroupBox, QLineEdit, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox

class Pump:
  def __init__(self, serial=None, motor=None):
    self.on=bool(False)
    self.rpm=float(10)
    self.ml_rpm=float(1)
    self.serial=Serial(str(serial))
    self.motor=str(motor)
    self.trans_volume=float(0)


  def connect(self):
    pass

  def turn_on(self):
    self.on = True
    self.serial.write((self.motor + "H\r").encode())

  def turn_off(self):
    self.on = False
    self.serial.write((self.motor + "I\r").encode())

  def change_rpm(self, new_rpm):
    if 0.1 < new_rpm < 100:
        self.rpm=new_rpm
        self.serial.write((self.motor + "S" + str(round(self.rpm*100)).zfill(6) + "\r").encode())
    else:
        print("outside of rpm range")


transfer_pump = Pump("COM3","1")


class Form(QDialog):

def __init__(self, parent=None):
    super(Form, self).__init__(parent)

    total = QVBoxLayout()
    total.addWidget(self.create_pump())
    self.setLayout(total)

def create_pump(self):
#Widgets
    groupPump = QGroupBox("Transferpumpe")

    lbl_title = QLabel("Serial nnumber")
    txt_serialno = QLineEdit("L19003722")
    btn_connect = QPushButton("Connect")
    btn_cal1 = QPushButton("Calibration RUN")
    btn_cal2 = QPushButton("Calibration CAL")
        # Motor 1
    btn_clw1 = QPushButton(">")
    btn_cclw1 = QPushButton("<")
    btn_stop1 = QPushButton("X")
    txt_tara1 = QLineEdit("tara weight of vessel [g]")
    txt_cal1 = QLineEdit("weight after calibration run [g]")
        # Motor 2
    btn_clw2 = QPushButton(">")
    btn_cclw2 = QPushButton("<")
    btn_stop2 = QPushButton("X")
    txt_tara2 = QLineEdit("tara weight of vessel [g]")
    txt_cal2 = QLineEdit("weight after calibration run [g]")
        # Motor 3
    btn_clw3 = QPushButton(">")
    btn_cclw3 = QPushButton("<")
    btn_stop3 = QPushButton("X")
    txt_tara3 = QLineEdit("tara weight of vessel [g]")
    txt_cal3 = QLineEdit("weight after calibration run [g]")
        # Motor 4
    btn_clw4 = QPushButton(">")
    btn_cclw4 = QPushButton("<")
    btn_stop4 = QPushButton("X")
    txt_tara4 = QLineEdit("tara weight of vessel [g]")
    txt_cal4 = QLineEdit("weight after calibration run [g]")

#Layout      
    title = QHBoxLayout()
    title.addWidget(lbl_title)
    title.addWidget(txt_serialno)        
    motor1 = QHBoxLayout()
    motor1.addWidget(btn_cclw1)
    motor1.addWidget(btn_stop1)
    btn_stop1.clicked.connect(self.stoppump)
    motor1.addWidget(btn_clw1)
    motor1.addWidget(txt_tara1)
    motor1.addWidget(txt_cal1)
    motor2 = QHBoxLayout()
    motor2.addWidget(btn_cclw2)
    motor2.addWidget(btn_stop2) 
    motor2.addWidget(btn_clw2)
    motor2.addWidget(txt_tara2)
    motor2.addWidget(txt_cal2)        
    motor3 = QHBoxLayout()
    motor3.addWidget(btn_cclw3)
    motor3.addWidget(btn_stop3)
    motor3.addWidget(btn_clw3)
    motor3.addWidget(txt_tara3)
    motor3.addWidget(txt_cal3) 
    motor4 = QHBoxLayout()
    motor4.addWidget(btn_cclw4)
    motor4.addWidget(btn_stop4)
    motor4.addWidget(btn_clw4)
    motor4.addWidget(txt_tara4)
    motor4.addWidget(txt_cal4) 
    buttons = QHBoxLayout()
    buttons.addWidget(btn_connect)
    btn_connect.clicked.connect(self.connectpump)
    buttons.addWidget(btn_cal1)   
    buttons.addWidget(btn_cal2)   
    layout = QVBoxLayout()
    layout.addLayout(title)        
    layout.addLayout(motor1)
    layout.addLayout(motor2)  
    layout.addLayout(motor3)  
    layout.addLayout(motor4)        
    layout.addLayout(buttons) 

    groupPump.setLayout(layout)

    return groupPump

def connectpump(self):
#    transfer_pump.serial="COM3"
    transfer_pump.motor="1"
    transfer_pump.turn_on()

def stoppump(self):
   transfer_pump.turn_off()


# Closing of application
def closeEvent(self, event):
    reply = QMessageBox.question(self, "Sure?", "Do you really want to close the application?", 
        QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()


if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Form()
  form.show()
  sys.exit(app.exec_())

[当我按下“连接”时,泵启动,当我按下“ X”时,泵停止。

第二步,我希望允许用户通过按“连接”来连接泵。问题在于,现在已经在初始化期间执行了def startpump()和stoppump():

import sys
import math
from serial import Serial
from PySide2.QtWidgets import QApplication, QWidget, QGroupBox, QLineEdit, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox

class Pump:
  def __init__(self, com=None):
    self.on=bool(False)
    self.rpm=float(10)
    self.ml_rpm=float(1)
    self.com=(str(com))
    self.serial=Serial()
    self.trans_volume=float(0)


  def connect(self):
    self.serial.port = str(self.com)

  def turn_on(self, motor):
    self.on = True
    self.serial.open()
    self.serial.write((motor + "H\r").encode())
    self.serial.close()

  def turn_off(self, motor):
    self.on = False
    self.serial.open()
    self.serial.write((motor + "I\r").encode())
    self.serial.close()

  def change_rpm(self, motor, new_rpm):
    if 0.1 < new_rpm < 100:
        self.rpm=new_rpm
        self.serial.write((motor + "S" + str(round(self.rpm*100)).zfill(6) + "\r").encode())
    else:
        print("Außerhalb der rpm range")

transfer_pump = Pump()

class Form(QWidget):

  def __init__(self, parent=None):
    super(Form, self).__init__(parent)

  #Widgets

    self.lbl_title = QLabel("Transferpumpe")
    self.txt_serialno = QLineEdit("L19003722")
    self.btn_connect = QPushButton("Connect")
    self.btn_cal1 = QPushButton("Calibration RUN")
    self.btn_cal2 = QPushButton("Calibration CAL")
        # Motor 1
    self.btn_clw1 = QPushButton(">")
    self.btn_cclw1 = QPushButton("<")
    self.btn_stop1 = QPushButton("X")
    self.txt_tara1 = QLineEdit("tara weight of vessel [g]")
    self.txt_cal1 = QLineEdit("weight after calibration run [g]")
        # Motor 2
    self.btn_clw2 = QPushButton(">")
    self.btn_cclw2 = QPushButton("<")
    self.btn_stop2 = QPushButton("X")
    self.txt_tara2 = QLineEdit("tara weight of vessel [g]")
    self.txt_cal2 = QLineEdit("weight after calibration run [g]")
        # Motor 3
    self.btn_clw3 = QPushButton(">")
    self.btn_cclw3 = QPushButton("<")
    self.btn_stop3 = QPushButton("X")
    self.txt_tara3 = QLineEdit("tara weight of vessel [g]")
    self.txt_cal3 = QLineEdit("weight after calibration run [g]")
        # Motor 4
    self.btn_clw4 = QPushButton(">")
    self.btn_cclw4 = QPushButton("<")
    self.btn_stop4 = QPushButton("X")
    self.txt_tara4 = QLineEdit("tara weight of vessel [g]")
    self.txt_cal4 = QLineEdit("weight after calibration run [g]")

  #Layout      
    title = QHBoxLayout()
    title.addWidget(self.lbl_title)
    title.addWidget(self.txt_serialno)        
    motor1 = QHBoxLayout()
    motor1.addWidget(self.btn_cclw1)
    motor1.addWidget(self.btn_stop1)
    motor1.addWidget(self.btn_clw1)      
    motor1.addWidget(self.txt_tara1)
    motor1.addWidget(self.txt_cal1)
    motor2 = QHBoxLayout()
    motor2.addWidget(self.btn_cclw2)
    motor2.addWidget(self.btn_stop2) 
    motor2.addWidget(self.btn_clw2)
    motor2.addWidget(self.txt_tara2)
    motor2.addWidget(self.txt_cal2)        
    motor3 = QHBoxLayout()
    motor3.addWidget(self.btn_cclw3)
    motor3.addWidget(self.btn_stop3)
    motor3.addWidget(self.btn_clw3)
    motor3.addWidget(self.txt_tara3)
    motor3.addWidget(self.txt_cal3) 
    motor4 = QHBoxLayout()
    motor4.addWidget(self.btn_cclw4)
    motor4.addWidget(self.btn_stop4)
    motor4.addWidget(self.btn_clw4)
    motor4.addWidget(self.txt_tara4)
    motor4.addWidget(self.txt_cal4) 
    buttons = QHBoxLayout()
    buttons.addWidget(self.btn_connect)
    buttons.addWidget(self.btn_cal1)   
    buttons.addWidget(self.btn_cal2)   
    layout = QVBoxLayout()
    layout.addLayout(title)        
    layout.addLayout(motor1)
    layout.addLayout(motor2)  
    layout.addLayout(motor3)  
    layout.addLayout(motor4)        
    layout.addLayout(buttons) 
    self.setLayout(layout)

  #Connectors    
    self.btn_connect.clicked.connect(self.connectpump)
    self.btn_clw1.clicked.connect(self.startpump("1")) 
    self.btn_stop1.clicked.connect(self.stoppump("1"))

    self.show()


  def connectpump(self):
    transfer_pump.com = "COM3"
    transfer_pump.connect()

  def startpump(self, motor):    
    try:
        transfer_pump.turn_on(motor)
    except:
        print("1")

  def stoppump(self, motor):
    try:
        transfer_pump.turn_off(motor)
    except:
        print("0")

# Closing of application
  def closeEvent(self, event):
    reply = QMessageBox.question(self, "Sure?", "Do you really want to close the application?", 
        QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if reply == QMessageBox.Yes:
        event.accept()
    else:
        event.ignore()


if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Form()
  sys.exit(app.exec_())

为什么程序在初始化期间已经执行了stoppump()和startpump()?当我再单击按钮时,为什么它不起作用?

我正在编写代码以具有一个GUI,该GUI通过串行连接来控制蠕动泵。首先,我开发了一个“泵”类,该类在一本jupyter书中可以很好地工作。其次,我使用...

python pyqt5 pyserial pyside2
1个回答
0
投票

您使用]初始化GUI>

...
self.btn_clw1.clicked.connect(self.startpump("1")) 
self.btn_stop1.clicked.connect(self.stoppump("1"))
...
© www.soinside.com 2019 - 2024. All rights reserved.