【问题标题】:How to establish serial connection in PyQT5如何在 PyQT5 中建立串行连接
【发布时间】:2017-11-29 04:15:28
【问题描述】:

我是 python 新手。我正在使用 python 3.6 (pyqt5) 在 Qt 中开发一个 GUI。我有一个连接按钮,可以与设备建立串行连接。现在 GUI 也有其他按钮了。但它们只有在按下连接按钮并建立串行连接时才能工作。在所有其他时间,它应该发布一条消息'Device not connected'。以下是部分代码:

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')

这只是代码的一部分,我没有在代码中包含所有小部件,而只包含相关部分。当我运行代码时,gui窗口打开,但是当我按下plus_y_button python 崩溃。一种方法是在构造函数中进行串行连接,但我希望仅在按下connect_button 时进行串行连接。这是我按下plus_y_button时发生的截图:

你知道如何解决吗?

【问题讨论】:

  • 您能具体说明您想要解决的问题吗?
  • 我只想在按下connect_button 时建立串行连接。当我按下plus_y_button 时,它应该执行特定操作,具体取决于是否按下了“connect_button”并建立了串行连接,否则应该只需打印“无设备”

标签: python qt user-interface pyqt pyserial


【解决方案1】:

在函数“connect_printer”中,您可以在本地定义变量“ser”,然后在函数完成时将其销毁。为了在函数“moveplusy”中使用“ser”,您需要将其定义为类的成员(例如 self.ser)。此外,函数中的“True”应大写。

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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多