简体   繁体   中英

How to establish serial connection in PyQT5

I am new to python. I am developing a GUI in Qt using python 3.6 (pyqt5). I have a connect button that establishes serial connection with the device. Now the GUI has other buttons as well. But they should only work if the connect button has been pressed and a serial connection has been established. At all other times it should post a message 'Device not connected' . Here's a part of the code:

import serial, time
import sys
import PyQt5
from PyQt5.QtWidgets import *
import mainwindow_auto
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):
    def __init__(self):
        #define gui actions here
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.constat=0

        self.plus_y_button.clicked.connect(self.moveplusy)   
        self.connect_button.clicked.connect(self.connect_printer)

    def connect_printer(self):

        port=str(self.port_sel_box.currentText())
        baudrate=str(self.baud_sel_box.currentText())

        try:
            ser = serial.Serial(port, baudrate)
            self.log_box.append('Connecting to printer....\nPort selected :'+self.port_sel_box.currentText()+'\nBaud Rate :'+self.baud_sel_box.currentText())

        except serial.serialutil.SerialException:
            self.log_box.append('No device available....Please connect a device')
    def moveplusy(self):
       if(ser.isOpen()==true):

           print('moving Y by +1')
           self.log_box.append('moving Y by +1')
       else:
           print('No device available')

this is only a part of the code,I have not included all the widgets in the code, but only the relevant parts.When I run the code the gui window opens, but when i press the plus_y_button python crashes.One way would be to make the serial connection in the constructor function, but I want the serial connection to take place only when I press the connect_button . This is a screenshot of what happens when I press the plus_y_button :

截图

Any idea as on how to solve it?

In the function "connect_printer" you locally define the variable "ser" which then gets destroyed when the function is finished. In order to use "ser" in the function "moveplusy" you need to define it as a member of the class (eg self.ser). Additionally, "True" should be capitalized in the function.

import serial, time
import sys
import PyQt5
from PyQt5.QtWidgets import *
import mainwindow_auto
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):
    def __init__(self):
        #define gui actions here
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.constat=0

        self.plus_y_button.clicked.connect(self.moveplusy)   
        self.connect_button.clicked.connect(self.connect_printer)

    def connect_printer(self):

        port=str(self.port_sel_box.currentText())
        baudrate=str(self.baud_sel_box.currentText())

        try:
            self.ser = serial.Serial(port, baudrate)
            self.log_box.append('Connecting to printer....\nPort selected :'+self.port_sel_box.currentText()+'\nBaud Rate :'+self.baud_sel_box.currentText())

        except serial.serialutil.SerialException:
            self.log_box.append('No device available....Please connect a device')
    def moveplusy(self):
       if self.ser.isOpen():

           print('moving Y by +1')
           self.log_box.append('moving Y by +1')
       else:
           print('No device available')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM