PyQt5.QtSerialPort在从其他类调用时不调用连接函数

时间:2017-09-01 10:39:27

标签: python pyqt pyqt5

关于从其他类调用PyQt5.QtSerialPort我有一个奇怪的(对我来说)问题:

这是串行代码类实现我想从其他对话框或类调用:

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QObject, QIODevice, pyqtSignal
from PyQt5.QtSerialPort import QSerialPort

class Com_port(QObject):
    ser = None
    packet_received = pyqtSignal(int)
    packet = [0,1,2,3,4,5,6,7,8,9,10,11,12]

    def __init__(self, *args, **kwds):
        super(Com_port, self).__init__()
        #self.buffer = kwds.pop('buffer')
        self.ser = QSerialPort(kwds.pop('port'))
        #self.ser = QSerialPort("COM5")
        self.ser.open(QIODevice.ReadWrite)
        self.ser.setBaudRate(kwds.pop('baudrate'))
        #self.ser.setBaudRate(115200)
        self.ser.readyRead.connect(self.on_serial_read)
        self.packet_received.connect(self.rcvData_signal)

        self.b1_bmsovi=b'\x0d\x0e\x0f\x10\x11\x12'
        self.brojac=0
        self.bms_number=0
        self.b1_bmsovi=b'\x0d\x0e\x0f\x10\x11\x12'
        self.brojac=0
        self.bms_number=0
        print ("Ipak sam prozvan")

    def rcvData_signal(self,bms):
        print(bms)

    def check_packet(self):
        rezultat = 0
        if self.packet[0]==170 and self.packet[1]==200 and self.packet[3]==1 and self.packet[12]==85 and (self.packet[2] in self.b1_bmsovi):
            self.bms_number = self.packet[2]
            rezultat = 1
        return rezultat

    def process_bytes(self, bs):
        """
        """
        for b in bs:
            if b == 170:
                self.brojac=0
            if self.brojac < 13:
                print (b)
                self.packet[self.brojac] = b
                self.brojac += 1
            if self.brojac==12 and self.check_packet()==1:
                self.packet_received.emit(self.bms_number)

    def on_serial_read(self):
        """
            Called when the application gets data from the connected device.
        """
        self.process_bytes(bytes(self.ser.readAll()))
# end of class Com_port

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ComPortApp = Com_port(port="COM5",baudrate=115200)
    sys.exit(app.exec_())
    """
    Korisni linkovi
    https://programtalk.com/vs2/python/8876/mu/mu/interface.py/
    """

当我单独执行此代码时,我得到了rcvData_signal(self,bms)函数的结果,该函数打印bms编号。

所以我想将此类用作来自com端口的通用串行数据源。 我编写此代码只是为了测试Com_port类:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from bms_single_ui import Ui_bms_single
from com_port_thread import Com_port

class bms_single(QMainWindow):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        ser=Com_port(port="COM5",baudrate=115200) # Com_port instance
        ComPortApp.packet_received.connect(self.rcvData_signal) #event redir

        self.ui = Ui_bms_single()
        self.ui.setupUi(self)
        self.setWindowTitle("BMS SINGLE PREVIEW")
        self.setWindowFlags(Qt.FramelessWindowHint)
        #self.setWindowFlags(Qt.Window | Qt.WindowTitleHint | Qt.CustomizeWindowHint )
        self.setFixedSize(800,480)
        self.move(0, 0)

    def rcvData_signal(self,bms):   # No calling to this :(
        print(bms)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    #ComPortApp = Com_port(port="COM5",baudrate=115200)
    sys.exit(app.exec_())

在这种情况下,我不能&#39;从Com_port实例获取任何数据:(

但是当我在代码底部解开此行#ComPortApp = Com_port(port =&#34; COM5&#34;,baudrate = 115200)时(并在类bms_singe中注释关于com端口的所有行),我得到了来自Com_port实例的所有数据。

从bms_single类调用Com_port类有什么问题?

1 个答案:

答案 0 :(得分:0)

根据documentation

  

'__main__'是顶级代码执行的范围的名称。   从标准输入,脚本或交互式提示中读取时,模块的__name__设置为'__main__'

     

[..]

从上面我们得出结论,只有if __name__ ==" __main__ " 执行:“。如果其他文件中存在相同的表达式,它们将被省略。所以你不应该使用变量ComPortApp,因为它不存在,正确的是使用变量ser ,在你的情况下它改变了:

ComPortApp.packet_received.connect(self.rcvData_signal)

为:

ser.packet_received.connect(self.rcvData_signal)

另一个问题是垃圾收集器正在消除对象ser,有两个选项:

  1. 为其创建变量ser类成员将更改为self.ser类中的bms_single
  2. self.ser=Com_port(port="COM5",baudrate=115200) # Com_port instance
    self.ser.packet_received.connect(self.rcvData_signal) #event redir
    
    1. 将父属性传递给Com_port类,您必须更改以下内容:
    2. ser= Com_port(port="COM5",baudrate=115200, parent=self)
      

      class Com_port(QObject):
          [...]
      
          def __init__(self, *args, **kwds):
              super(Com_port, self).__init__(kwds.pop('parent'))
      

      我还建议将QWidget.__init__(self, parent)更改为QMainWindow.__init__(self, parent)

      最后,不要忘记在main中创建并显示bms_single的实例。

      if __name__ == '__main__':
          app = QApplication(sys.argv)
          w = bms_single()
          w.show()
          sys.exit(app.exec_())