0

I have followed a simple tutorial on creating a basic gui using qt designer and have incorporated that into python. I then made a simple python script that just continuously reads newlines from the serial port and prints them to the terminal. I want to combine them but I'm afraid my understanding of how pyqt4 works or perhaps classes themselves is just too lacking to accomplish such a simple task. For starters all I want to accomplish is to continuously print the incoming serial data. I've tried to move the serial print loop into different areas of the python script but it causes the window to hang. What is the proper way to do this? pyqt code:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(400, 300)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.centralWidget)
        self.verticalLayout.setMargin(11)
        self.verticalLayout.setSpacing(6)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.textBrowser = QtGui.QTextBrowser(self.centralWidget)
        self.textBrowser.setObjectName(_fromUtf8("textBrowser"))
        self.verticalLayout.addWidget(self.textBrowser)
        self.pushButton = QtGui.QPushButton(self.centralWidget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
        self.menuBar.setObjectName(_fromUtf8("menuBar"))
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(MainWindow)
        self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(MainWindow)
        self.statusBar.setObjectName(_fromUtf8("statusBar"))
        MainWindow.setStatusBar(self.statusBar)
        self.textBrowser.append("blabla")
        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "Close", None))
        self.textBrowser.append("blabla")


if __name__ == "__main__":
    import sys
    import serial
    ser = serial.Serial()
    ser.baudrate = 115200
    ser.port = 'COM6'
    ser.open()
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    ui.textBrowser.append(str(ser.readline()))
    sys.exit(app.exec_())

serial code:

import serial
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM6'
ser.open()
while True:
    print(ser.readline())   # read a '\n' terminated line

1 Answer 1

0

For anyone that has a similar issue. I found a solution by implementing a timer and creating a new function in the class:

self.my_timer = QtCore.QTimer()
        self.my_timer.timeout.connect(self.print_serial)
        self.my_timer.start(10) # milliseconds

    def print_serial(self):
        if ser.in_waiting:
            self.textBrowser.append(str(ser.readline()))

this worked for my application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.