I'm writing a code to have a GUI which controls a peristaltic pump via a serial connection.
First I developed a Class "Pump", which works fine in a jupyter book. Second I developed a GUI using PySide2/PyQt5.
By merging both, I get some issues.
This is the last code, which worked fine:
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_())
When I push "connect" the pump starts, when I push "X" the pump stops.
In a second step I wanted to allow the user to connect with the pump by pushing "connect". The issue is that now already during initialization the def startpump() and stoppump() are performed:
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_())
Why does the program execute stoppump() and startpump() already during initialization? And why does it not work, when I click on the buttons anymore?