diff -r a09a30251d4e -r 3368ce0e7879 eric6/MicroPython/MicroPythonSerialPort.py --- a/eric6/MicroPython/MicroPythonSerialPort.py Sat Jul 20 14:47:24 2019 +0200 +++ b/eric6/MicroPython/MicroPythonSerialPort.py Sat Jul 20 14:48:09 2019 +0200 @@ -4,29 +4,43 @@ # """ -Module implementing a QSerialPort with additional functionality. +Module implementing a QSerialPort with additional functionality for +MicroPython devices. """ from __future__ import unicode_literals -from PyQt5.QtCore import QIODevice +from PyQt5.QtCore import QIODevice, QTime from PyQt5.QtSerialPort import QSerialPort class MicroPythonSerialPort(QSerialPort): """ - Class implementing a QSerialPort with additional functionality + Class implementing a QSerialPort with additional functionality for + MicroPython devices. """ - def __init__(self, parent=None): + def __init__(self, timeout=10000, parent=None): """ Constructor + @param timeout timout in milliseconds to be set + @type int @param parent reference to the parent object @type QObject """ super(MicroPythonSerialPort, self).__init__(parent) self.__connected = False + self.__timeout = timeout # 10s default timeout + + def setTimeout(self, timeout): + """ + Public method to set the timeout for device operations. + + @param timeout timout in milliseconds to be set + @type int + """ + self.__timeout = timeout def openSerialLink(self, port): """ @@ -70,7 +84,7 @@ return self.__connected def readUntil(self, expected=b"\n", size=None): - """ + r""" Public method to read data until an expected sequence is found (default: \n) or a specific size is exceeded. @@ -81,19 +95,27 @@ @return bytes read from the device including the expected sequence @rtype bytes """ + from PyQt5.QtCore import QCoreApplication, QEventLoop data = bytearray() + + t = QTime() + t.start() while True: - if self.waitForReadyRead(): - c = bytes(self.read(1)) - if c: - data += c - if data.endswith(expected): - break - if size is not None and len(data) >= size: - break - else: + # TODO: check if this is still needed when used with a QThread + QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents) +## if self.waitForReadyRead(self.__timeout): + c = bytes(self.read(1)) + if c: + data += c + if data.endswith(expected): break - else: + if size is not None and len(data) >= size: + break +# else: +# break + if t.elapsed() > self.__timeout: break +## else: +## break return bytes(data)