eric6/MicroPython/MicroPythonSerialPort.py

Sat, 27 Jul 2019 15:43:21 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Jul 2019 15:43:21 +0200
branch
micropython
changeset 7087
2ca7fb61a82f
parent 7084
3eddfc540614
child 7229
53054eb5b15a
permissions
-rw-r--r--

MicroPythonReplWidget: added special handling for the Return and Enter keys to move the cursor to the end of line before passing it on to the REPL.

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

eric ide

mercurial