src/eric7/MicroPython/MicroPythonSerialPort.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
15 class MicroPythonSerialPort(QSerialPort): 15 class MicroPythonSerialPort(QSerialPort):
16 """ 16 """
17 Class implementing a QSerialPort with additional functionality for 17 Class implementing a QSerialPort with additional functionality for
18 MicroPython devices. 18 MicroPython devices.
19 """ 19 """
20
20 def __init__(self, timeout=10000, parent=None): 21 def __init__(self, timeout=10000, parent=None):
21 """ 22 """
22 Constructor 23 Constructor
23 24
24 @param timeout timout in milliseconds to be set 25 @param timeout timout in milliseconds to be set
25 @type int 26 @type int
26 @param parent reference to the parent object 27 @param parent reference to the parent object
27 @type QObject 28 @type QObject
28 """ 29 """
29 super().__init__(parent) 30 super().__init__(parent)
30 31
31 self.__connected = False 32 self.__connected = False
32 self.__timeout = timeout # 10s default timeout 33 self.__timeout = timeout # 10s default timeout
33 self.__timedOut = False 34 self.__timedOut = False
34 35
35 def setTimeout(self, timeout): 36 def setTimeout(self, timeout):
36 """ 37 """
37 Public method to set the timeout for device operations. 38 Public method to set the timeout for device operations.
38 39
39 @param timeout timout in milliseconds to be set 40 @param timeout timout in milliseconds to be set
40 @type int 41 @type int
41 """ 42 """
42 self.__timeout = timeout 43 self.__timeout = timeout
43 44
44 def openSerialLink(self, port): 45 def openSerialLink(self, port):
45 """ 46 """
46 Public method to open a serial link to a given serial port. 47 Public method to open a serial link to a given serial port.
47 48
48 @param port port name to connect to 49 @param port port name to connect to
49 @type str 50 @type str
50 @return flag indicating success 51 @return flag indicating success
51 @rtype bool 52 @rtype bool
52 """ 53 """
56 # 115.200 baud, 8N1 57 # 115.200 baud, 8N1
57 self.setBaudRate(115200) 58 self.setBaudRate(115200)
58 self.setDataBits(QSerialPort.DataBits.Data8) 59 self.setDataBits(QSerialPort.DataBits.Data8)
59 self.setParity(QSerialPort.Parity.NoParity) 60 self.setParity(QSerialPort.Parity.NoParity)
60 self.setStopBits(QSerialPort.StopBits.OneStop) 61 self.setStopBits(QSerialPort.StopBits.OneStop)
61 62
62 self.__connected = True 63 self.__connected = True
63 return True 64 return True
64 else: 65 else:
65 return False 66 return False
66 67
67 def closeSerialLink(self): 68 def closeSerialLink(self):
68 """ 69 """
69 Public method to close the open serial connection. 70 Public method to close the open serial connection.
70 """ 71 """
71 if self.__connected: 72 if self.__connected:
72 self.close() 73 self.close()
73 74
74 self.__connected = False 75 self.__connected = False
75 76
76 def isConnected(self): 77 def isConnected(self):
77 """ 78 """
78 Public method to get the connection state. 79 Public method to get the connection state.
79 80
80 @return flag indicating the connection state 81 @return flag indicating the connection state
81 @rtype bool 82 @rtype bool
82 """ 83 """
83 return self.__connected 84 return self.__connected
84 85
85 def hasTimedOut(self): 86 def hasTimedOut(self):
86 """ 87 """
87 Public method to check, if the last 'readUntil' has timed out. 88 Public method to check, if the last 'readUntil' has timed out.
88 89
89 @return flag indicating a timeout 90 @return flag indicating a timeout
90 @@rtype bool 91 @@rtype bool
91 """ 92 """
92 return self.__timedOut 93 return self.__timedOut
93 94
94 def readUntil(self, expected=b"\n", size=None): 95 def readUntil(self, expected=b"\n", size=None):
95 r""" 96 r"""
96 Public method to read data until an expected sequence is found 97 Public method to read data until an expected sequence is found
97 (default: \n) or a specific size is exceeded. 98 (default: \n) or a specific size is exceeded.
98 99
99 @param expected expected bytes sequence 100 @param expected expected bytes sequence
100 @type bytes 101 @type bytes
101 @param size maximum data to be read 102 @param size maximum data to be read
102 @type int 103 @type int
103 @return bytes read from the device including the expected sequence 104 @return bytes read from the device including the expected sequence
104 @rtype bytes 105 @rtype bytes
105 """ 106 """
106 data = bytearray() 107 data = bytearray()
107 self.__timedOut = False 108 self.__timedOut = False
108 109
109 t = QTime.currentTime() 110 t = QTime.currentTime()
110 while True: 111 while True:
111 QCoreApplication.processEvents( 112 QCoreApplication.processEvents(
112 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents) 113 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
114 )
113 c = bytes(self.read(1)) 115 c = bytes(self.read(1))
114 if c: 116 if c:
115 data += c 117 data += c
116 if data.endswith(expected): 118 if data.endswith(expected):
117 break 119 break
118 if size is not None and len(data) >= size: 120 if size is not None and len(data) >= size:
119 break 121 break
120 if t.msecsTo(QTime.currentTime()) > self.__timeout: 122 if t.msecsTo(QTime.currentTime()) > self.__timeout:
121 self.__timedOut = True 123 self.__timedOut = True
122 break 124 break
123 125
124 return bytes(data) 126 return bytes(data)

eric ide

mercurial