eric6/MicroPython/MicroPythonSerialPort.py

branch
micropython
changeset 7082
ec199ef0cfc6
parent 7077
3b7475b7a1ef
child 7083
217862c28319
equal deleted inserted replaced
7081:ed510767c096 7082:ec199ef0cfc6
30 """ 30 """
31 super(MicroPythonSerialPort, self).__init__(parent) 31 super(MicroPythonSerialPort, self).__init__(parent)
32 32
33 self.__connected = False 33 self.__connected = False
34 self.__timeout = timeout # 10s default timeout 34 self.__timeout = timeout # 10s default timeout
35 self.__timedOut = False
35 36
36 def setTimeout(self, timeout): 37 def setTimeout(self, timeout):
37 """ 38 """
38 Public method to set the timeout for device operations. 39 Public method to set the timeout for device operations.
39 40
81 @return flag indicating the connection state 82 @return flag indicating the connection state
82 @rtype bool 83 @rtype bool
83 """ 84 """
84 return self.__connected 85 return self.__connected
85 86
87 def hasTimedOut(self):
88 """
89 Public method to check, if the last 'readUntil' has timed out.
90
91 @return flag indicating a timeout
92 @@rtype bool
93 """
94 return self.__timedOut
95
86 def readUntil(self, expected=b"\n", size=None): 96 def readUntil(self, expected=b"\n", size=None):
87 r""" 97 r"""
88 Public method to read data until an expected sequence is found 98 Public method to read data until an expected sequence is found
89 (default: \n) or a specific size is exceeded. 99 (default: \n) or a specific size is exceeded.
90 100
94 @type int 104 @type int
95 @return bytes read from the device including the expected sequence 105 @return bytes read from the device including the expected sequence
96 @rtype bytes 106 @rtype bytes
97 """ 107 """
98 data = bytearray() 108 data = bytearray()
109 self.__timedOut = False
99 110
100 t = QTime() 111 t = QTime()
101 t.start() 112 t.start()
102 while True: 113 while True:
103 QCoreApplication.processEvents() 114 QCoreApplication.processEvents()
109 if size is not None and len(data) >= size: 120 if size is not None and len(data) >= size:
110 break 121 break
111 # else: 122 # else:
112 # break 123 # break
113 if t.elapsed() > self.__timeout: 124 if t.elapsed() > self.__timeout:
125 self.__timedOut = True
114 break 126 break
115 127
116 return bytes(data) 128 return bytes(data)

eric ide

mercurial