eric6/MicroPython/MicroPythonSerialPort.py

Sun, 08 Dec 2019 12:38:34 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 08 Dec 2019 12:38:34 +0100
changeset 7353
caa2ccd5677c
parent 7229
53054eb5b15a
child 7360
9190402e4505
permissions
-rw-r--r--

EspDevices: fixed an issue resetting the attached device when not connected.

7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
7 Module implementing a QSerialPort with additional functionality for
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
8 MicroPython devices.
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
7083
217862c28319 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7082
diff changeset
12 from PyQt5.QtCore import QIODevice, QTime, QCoreApplication, QEventLoop
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt5.QtSerialPort import QSerialPort
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 class MicroPythonSerialPort(QSerialPort):
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
18 Class implementing a QSerialPort with additional functionality for
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
19 MicroPython devices.
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
21 def __init__(self, timeout=10000, parent=None):
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Constructor
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
25 @param timeout timout in milliseconds to be set
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
26 @type int
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @param parent reference to the parent object
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @type QObject
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 super(MicroPythonSerialPort, self).__init__(parent)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.__connected = False
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
33 self.__timeout = timeout # 10s default timeout
7082
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
34 self.__timedOut = False
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
35
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
36 def setTimeout(self, timeout):
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
37 """
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
38 Public method to set the timeout for device operations.
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
39
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
40 @param timeout timout in milliseconds to be set
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
41 @type int
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
42 """
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
43 self.__timeout = timeout
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 def openSerialLink(self, port):
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 Public method to open a serial link to a given serial port.
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @param port port name to connect to
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 @type str
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 @return flag indicating success
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @rtype bool
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.setPortName(port)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 if self.open(QIODevice.ReadWrite):
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.setDataTerminalReady(True)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 # 115.200 baud, 8N1
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.setBaudRate(115200)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 self.setDataBits(QSerialPort.Data8)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.setParity(QSerialPort.NoParity)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.setStopBits(QSerialPort.OneStop)
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.__connected = True
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 return True
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 else:
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 return False
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 def closeSerialLink(self):
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 Public method to close the open serial connection.
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
7077
3b7475b7a1ef MicroPython: started to implement the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7070
diff changeset
72 if self.__connected:
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 self.close()
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 self.__connected = False
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 def isConnected(self):
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 Public method to get the connection state.
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @return flag indicating the connection state
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @rtype bool
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 return self.__connected
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85
7082
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
86 def hasTimedOut(self):
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
87 """
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
88 Public method to check, if the last 'readUntil' has timed out.
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
89
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
90 @return flag indicating a timeout
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
91 @@rtype bool
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
92 """
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
93 return self.__timedOut
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
94
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 def readUntil(self, expected=b"\n", size=None):
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
96 r"""
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 Public method to read data until an expected sequence is found
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 (default: \n) or a specific size is exceeded.
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @param expected expected bytes sequence
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @type bytes
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 @param size maximum data to be read
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 @type int
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 @return bytes read from the device including the expected sequence
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 @rtype bytes
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 data = bytearray()
7082
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
108 self.__timedOut = False
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
109
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
110 t = QTime()
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
111 t.start()
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 while True:
7083
217862c28319 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7082
diff changeset
113 QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
114 c = bytes(self.read(1))
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
115 if c:
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
116 data += c
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
117 if data.endswith(expected):
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 break
7070
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
119 if size is not None and len(data) >= size:
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
120 break
3368ce0e7879 Started to implement the device file system interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7067
diff changeset
121 if t.elapsed() > self.__timeout:
7082
ec199ef0cfc6 MicroPython: continued implementing the file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7077
diff changeset
122 self.__timedOut = True
7067
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 break
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
3fc4082fc6ba Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 return bytes(data)

eric ide

mercurial