2

我正在尝试构建一个 PyQt 应用程序,该应用程序(除其他外)能够通过 QTextEdit 框像串行终端程序(HyperTerminal、TeraTerm 等)一样运行。我已经阅读了 PySerial 页面中的一些示例和我想我已经设法让接收数据线程正常工作,但可能效率不高。

我的问题是如何获取 QTextEdit 框中最后输入的字符并将其发送到串行连接?我尝试使用 QTextEdit 发出的 textChanged 信号,但它会发送我输入的所有内容以及它接收到的所有内容。我已经尝试在我的主 GUI 类中设置一个 eventFilter,但我无法弄清楚如何将它传递给另一个文件中的串行函数。我是否想要一个单独的线程来监听从 eventFilter 发出的信号?我怎么做?有没有更优雅的方法来做到这一点?

我确定我只是想多了,解决方案很简单,但我有点挣扎。我将附上相关的代码片段(不是完整的代码集),也许有人可以指出我正确的方向。如果有人还认为我正在做的线程可以以更有效的方式完成,那么也请转发给我!

感谢任何人都可以提供的任何帮助!

主文件:

import sys
from PyQt4 import QtGui
from MainGUI import TestGUI
from SerialClasses import *
from SerialMiniterm import *

class StartMainWindow(QtGui.QMainWindow):      
    def __init__(self, parent=None):
        super(StartMainWindow, self).__init__(parent)
        self.ui = TestGUI()
        self.ui.setupUi(self)    
        self.ui.serialTextEditBox.installEventFilter(self)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.KeyPress and source is self.ui.serialTextEditBox):
            # print some debug statements to console
            if (event.key() == QtCore.Qt.Key_Tab):
                print ('Tab pressed')
            print ('key pressed: %s' % event.text())
            print ('code pressed: %d' % event.key())
            # do i emit a signal here?  how do i catch it in thread?
            self.emit(QtCore.SIGNAL('transmitSerialData(QString)'), event.key())
            return True
        return QtGui.QTextEdit.eventFilter(self, source, event)   

    def serialConnectCallback(self):
        self.miniterm = SerialMiniterm(self.ui, self.SerialSettings)
        self.miniterm.start()
        temp = self.SerialSettings.Port + 1
        self.ui.serialLabel.setText("<font color = green>Serial Terminal Connected on COM%d" % temp) 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle("Cleanlooks")
    myapp = StartMainWindow()
    myapp.show()
    sys.exit(app.exec_())

SerialMiniterm.py:

import serial
from PyQt4 import QtGui, QtCore

def character(b):
    return b

class SerialMiniterm(object):
    def __init__(self, ui, SerialSettings):
        self.SerialSettings = SerialSettings
        self.ui = ui
        self.serial = serial.Serial(self.SerialSettings.Port, self.SerialSettings.BaudRate, parity=self.SerialSettings.Parity, rtscts=self.SerialSettings.RTS_CTS, xonxoff=self.SerialSettings.Xon_Xoff, timeout=1)
        self.repr_mode = self.SerialSettings.RxMode
        self.convert_outgoing = self.SerialSettings.NewlineMode
        self.newline = NEWLINE_CONVERISON_MAP[self.convert_outgoing]
        self.dtr_state = True
        self.rts_state = True
        self.break_state = False

    def _start_reader(self):
        """Start reader thread"""
        self._reader_alive = True
        self.receiver_thread = ReaderThread(self.alive, self._reader_alive, self.repr_mode, self.convert_outgoing, self.serial)
        self.receiver_thread.connect(self.receiver_thread, QtCore.SIGNAL("updateSerialTextBox(QString)"), self.updateTextBox)
        self.receiver_thread.start()

    def _stop_reader(self):
        """Stop reader thread only, wait for clean exit of thread"""
        self._reader_alive = False
        self.receiver_thread.join()

    def updateTextBox(self, q):
        self.ui.serialTextEditBox.insertPlainText(q)
        self.ui.serialTextEditBox.moveCursor(QtGui.QTextCursor.End)
        #print "got here with value %s..." % q

    def start(self):
        self.alive = True
        self._start_reader()
        # how do i handle transmitter thread?

    def stop(self):
        self.alive = False

    def join(self, transmit_only=False):
        self.transmitter_thread.join()
        if not transmit_only:
            self.receiver_thread.join()

class ReaderThread(QtCore.QThread):       
    def __init__(self, alive, _reader_alive, repr_mode, convert_outgoing, serial, parent=None):
        QtCore.QThread.__init__(self, parent)
        self.alive = alive
        self._reader_alive = _reader_alive
        self.repr_mode = repr_mode
        self.convert_outgoing = convert_outgoing
        self.serial = serial

    def __del__(self):
        self.wait()

    def run(self):
        """loop and copy serial->console"""
        while self.alive and self._reader_alive:
            data = self.serial.read(self.serial.inWaiting())
            if data:                            #check if not timeout
                q = data
                self.emit(QtCore.SIGNAL('updateSerialTextBox(QString)'), q)
4

1 回答 1

2

像这样的东西?

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Terminal(QtGui.QPlainTextEdit):
    def keyPressEvent(self, event):
        print event.text()
        return QtGui.QPlainTextEdit.keyPressEvent(self, event)

term = Terminal()
term.show()
于 2012-06-19T15:42:13.717 回答