抓取类中的设备连接异常并将其传递给pyQT main wind

2024-09-24 06:30:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类可以访问外部LabJack设备。Labjack设备通过USB连接,其主要功能是通过python命令打开或关闭某些东西。我想给我的pyqt主窗口应用程序传递一个异常,以防labjack没有连接。我不太清楚我的LabJack课怎么样了:

import u3

class LabJack:
    def __init__(self):

        try:
            self.Switch = u3.U3()
        except:
            print "Labjack Error"

        #Define State Registers for RB12 Relay Card

        self.Chan0 = 6008
        Chan1 = 6009
        Chan2 = 6010
        Chan3 = 6011
        Chan4 = 6012
        Chan5 = 6013

#Turn the channel on
    def IO_On(self,Channel):
        self.Switch.writeRegister(Channel,0)


    #Turn the channel off
    def IO_Off(self,Channel):   
        self.Switch.writeRegister(Channel,1)

    #The State of the Channel
    def StateSetting(self,Channel):
        self.Switch.readRegister(Channel)
        if Switch.readRegister(Channel) == 0:
            print ('Channel is On')
        else:
            print('Channel is Off')

    #Direction of Current Flow
    def CurrentDirection(self,Channel):
        self.Switch.readRegister(6108)
        print self.Switch.readRegister(6108)

这是我的pyqt代码:

import re
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from LabJackIO import *
from Piezo902 import *
import functools

import ui_aldmainwindow

class ALDMainWindow(QMainWindow,ui_aldmainwindow.Ui_ALDMainWindow):

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

        self.ValveControl = LabJack()



        self.Valve_ON.clicked.connect(functools.partial(self.ValveControl.IO_On,self.ValveControl.Chan0))
        self.Valve_OFF.clicked.connect(functools.partial(self.ValveControl.IO_Off,self.ValveControl.Chan0))
        self.statusBar().showMessage('Valve Off')

app = QApplication(sys.argv)
app.setStyle('motif')
form = ALDMainWindow()
form.show()
app.exec_()

有什么建议吗?你知道吗

谢谢。你知道吗


Tags: fromioimportselfinitdefchannelprint
1条回答
网友
1楼 · 发布于 2024-09-24 06:30:26

如果您的LabJack可以从QObject继承,那么您可以在捕获异常时发出一个信号:

class LabJack(QtCore.QObject):
    switchFailed = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(LabJack, self).__init__(parent)
        try:
            self.Switch = u3.U3()

        except:
            self.switchFailed.emit()

然后在ALDMainWindow中设置一个槽来处理异常:

        self.ValveControl = LabJack()
        self.ValveControl.switchFailed.connect(self.on_ValveControl_switchFailed)

    @QtCore.pyqtSlot()
    def on_ValveControl_switchFailed(self):
        print "Handle exception here."

相关问题 更多 >