src/eric7/MicroPython/MicroPythonSerialDeviceInterface.py

Fri, 28 Apr 2023 12:07:41 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 28 Apr 2023 12:07:41 +0200
branch
mpy_network
changeset 9990
54c614d91eff
parent 9989
src/eric7/MicroPython/MicroPythonDeviceInterface.py@286c2a21f36f
child 10008
c5bcafe3485c
permissions
-rw-r--r--

MicroPython
- Refactored the code by introducing a device interface base class and changed the interface instantiation logic to prepare the basis for developing the 'webrepl' interface.

7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
9653
e67609152c5e Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9576
diff changeset
3 # Copyright (c) 2019 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
7 Module implementing an interface to talk to a connected MicroPython device via
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
8 a serial link.
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
11 from PyQt6.QtCore import QCoreApplication, QEventLoop, QThread, QTimer, pyqtSlot
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
13 from eric7 import Preferences
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
15 from .MicroPythonDeviceInterface import MicroPythonDeviceInterface
9473
3f23dbf37dbe Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9413
diff changeset
16 from .MicroPythonSerialPort import MicroPythonSerialPort
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
19 class MicroPythonSerialDeviceInterface(MicroPythonDeviceInterface):
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
21 Class implementing an interface to talk to a connected MicroPython device via
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
22 a serial link.
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
24
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
25 PasteModePrompt = b"=== "
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
26 TracebackMarker = b"Traceback (most recent call last):"
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
27
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 def __init__(self, parent=None):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Constructor
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
31
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @param parent reference to the parent object
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @type QObject
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8143
diff changeset
35 super().__init__(parent)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
36
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 self.__blockReadyRead = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
38
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__serial = MicroPythonSerialPort(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
40 timeout=Preferences.getMicroPython("SerialTimeout"), parent=self
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
41 )
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 self.__serial.readyRead.connect(self.__readSerial)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
43
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 def __readSerial(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 Private slot to read all available serial data and emit it with the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 "dataReceived" signal for further processing.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if not self.__blockReadyRead:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 data = bytes(self.__serial.readAll())
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 self.dataReceived.emit(data)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
53
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 @pyqtSlot()
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
55 def connectToDevice(self, connection):
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 """
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
57 Public slot to connect to the device.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
58
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
59 @param connection name of the connection to be used
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 @type str
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 @return flag indicating success
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 """
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
64 return self.__serial.openSerialLink(connection)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
65
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 def disconnectFromDevice(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
69 Public slot to disconnect from the device.
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.__serial.closeSerialLink()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
72
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 def isConnected(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 Public method to get the connection status.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
76
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 @return flag indicating the connection status
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 @rtype bool
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 return self.__serial.isConnected()
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
81
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @pyqtSlot()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 def handlePreferencesChanged(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 Public slot to handle a change of the preferences.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 self.__serial.setTimeout(Preferences.getMicroPython("SerialTimeout"))
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
88
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 def write(self, data):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 Public method to write data to the connected device.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
92
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 @param data data to be written
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 @type bytes or bytearray
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__serial.isConnected() and self.__serial.write(data)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
97
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
98 def __pasteOn(self):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
99 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
100 Private method to switch the connected device to 'paste' mode.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
101
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
102 Note: switching to paste mode is done with synchronous writes.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
103
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
104 @return flag indicating success
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
105 @rtype bool
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
106 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
107 if not self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
108 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
109
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
110 pasteMessage = b"paste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== "
9810
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
111
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
112 self.__serial.clear() # clear any buffered output before entering paste mode
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
113 self.__serial.write(b"\x02") # end raw mode if required
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
114 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
115 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
116 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
117 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
118 for _i in range(3):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
119 # CTRL-C three times to break out of loops
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
120 self.__serial.write(b"\r\x03")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
121 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
122 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
123 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
124 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
125 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
126 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
127 self.__serial.write(b"\r\x05") # send CTRL-E to enter paste mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
128 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
129
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
130 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
131 # it timed out; try it again and than fail
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
132 self.__serial.write(b"\r\x05") # send CTRL-E again
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
133 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
134 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
135 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
136
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
137 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
138 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
139 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
140 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
141 return True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
142
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
143 def __pasteOff(self):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
144 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
145 Private method to switch 'paste' mode off.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
146 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
147 if self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
148 self.__serial.write(b"\x04") # send CTRL-D to cancel paste mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
149
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 def __rawOn(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 Private method to switch the connected device to 'raw' mode.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
153
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 Note: switching to raw mode is done with synchronous writes.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
155
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 @return flag indicating success
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
157 @rtype bool
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 if not self.__serial:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
161
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
162 rawReplMessage = b"raw REPL; CTRL-B to exit\r\n>"
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
163
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
164 self.__serial.write(b"\x02") # end raw mode if required
8061
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
165 written = self.__serial.waitForBytesWritten(500)
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
166 # time out after 500ms if device is not responding
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
167 if not written:
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
168 return False
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 for _i in range(3):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 # CTRL-C three times to break out of loops
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 self.__serial.write(b"\r\x03")
8061
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
172 written = self.__serial.waitForBytesWritten(500)
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
173 # time out after 500ms if device is not responding
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
174 if not written:
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
175 return False
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 QThread.msleep(10)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
177 self.__serial.readAll() # read all data and discard it
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
178 self.__serial.write(b"\r\x01") # send CTRL-A to enter raw mode
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 self.__serial.readUntil(rawReplMessage)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 if self.__serial.hasTimedOut():
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
181 # it timed out; try it again and than fail
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182 self.__serial.write(b"\r\x01") # send CTRL-A again
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 self.__serial.readUntil(rawReplMessage)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 if self.__serial.hasTimedOut():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
186
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8117
diff changeset
187 QCoreApplication.processEvents(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
188 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
189 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
190 self.__serial.readAll() # read all data and discard it
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 return True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
192
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 def __rawOff(self):
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 Private method to switch 'raw' mode off.
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 if self.__serial:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
198 self.__serial.write(b"\x02") # send CTRL-B to cancel raw mode
7102
5e77aa4671e6 MicroPythonCommandsInterface: fixed an issue causing the prompt being shown in the REPL pane for each file manager command sequence.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7095
diff changeset
199 self.__serial.readUntil(b">>> ") # read until Python prompt
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
200 self.__serial.readAll() # read all data and discard it
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
201
9749
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
202 def probeDevice(self):
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
203 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
204 Public method to check the device is responding.
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
205
9990
54c614d91eff MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9989
diff changeset
206 If the device has not been flashed with a MicroPython firmware, the
9749
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
207 probe will fail.
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
208
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
209 @return flag indicating a communicating MicroPython device
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
210 @rtype bool
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
211 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
212 if not self.__serial:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
213 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
214
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
215 if not self.__serial.isConnected():
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
216 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
217
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
218 # switch on raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
219 self.__blockReadyRead = True
9826
9340ce7fb12f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9821
diff changeset
220 ok = self.__pasteOn()
9749
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
221 if not ok:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
222 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
223 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
224
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
225 # switch off raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
226 QThread.msleep(10)
9826
9340ce7fb12f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9821
diff changeset
227 self.__pasteOff()
9749
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
228 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
229
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
230 return True
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
231
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
232 def execute(self, commands, *, mode="raw", timeout=0):
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 Public method to send commands to the connected device and return the
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 result.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
236
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 If no serial connection is available, empty results will be returned.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
238
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239 @param commands list of commands to be executed
9765
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
240 @type str or list of str
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
241 @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
242 'raw')
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
243 @type str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
244 @keyparam timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
245 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
246 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
247 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
248 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
249 @exception ValueError raised in case of an unsupported submit mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
250 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
251 if mode not in ("paste", "raw"):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
252 raise ValueError("Unsupported submit mode given ('{0}').".format(mode))
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
253
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
254 if mode == "raw":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
255 return self.__execute_raw(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
256 elif mode == "paste":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
257 return self.__execute_paste(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
258 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
259 # just in case
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
260 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
261
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
262 def __execute_raw(self, commands, timeout=0):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
263 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
264 Private method to send commands to the connected device using 'raw REPL' mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
265 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
266
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
267 If no serial connection is available, empty results will be returned.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
268
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
269 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
270 @type str or list of str
9775
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
271 @param timeout per command timeout in milliseconds (0 for configured default)
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
272 (defaults to 0)
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
273 @type int (optional)
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 @return tuple containing stdout and stderr output of the device
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 @rtype tuple of (bytes, bytes)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 if not self.__serial:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278 return b"", b""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
279
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
280 if not self.__serial.isConnected():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 return b"", b"Device not connected or not switched on."
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
282
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 result = bytearray()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 err = b""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
285
9765
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
286 if isinstance(commands, str):
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
287 commands = [commands]
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
288
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
289 # switch on raw mode
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 self.__blockReadyRead = True
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 ok = self.__rawOn()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 if not ok:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 self.__blockReadyRead = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
294 return (b"", b"Could not switch to raw mode. Is the device switched on?")
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
295
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
296 # send commands
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 QThread.msleep(10)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 for command in commands:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299 if command:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 commandBytes = command.encode("utf-8")
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 self.__serial.write(commandBytes + b"\x04")
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
302 QCoreApplication.processEvents(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
303 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
304 )
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
305 ok = self.__serial.readUntil(b"OK")
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
306 if ok != b"OK":
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
307 self.__blockReadyRead = False
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
308 return (
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
309 b"",
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
310 "Expected 'OK', got '{0}', followed by '{1}'".format(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
311 ok, self.__serial.readAll()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
312 ).encode("utf-8"),
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
313 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
314
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 # read until prompt
9775
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
316 response = self.__serial.readUntil(b"\x04>", timeout=timeout)
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 if self.__serial.hasTimedOut():
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 self.__blockReadyRead = False
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 return b"", b"Timeout while processing commands."
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
320 if b"\x04" in response[:-2]:
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
321 # split stdout, stderr
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
322 out, err = response[:-2].split(b"\x04")
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
323 result += out
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
324 else:
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 err = b"invalid response received: " + response
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 if err:
9764
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
327 result = b""
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
328 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
329
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
330 # switch off raw mode
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
331 QThread.msleep(10)
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
332 self.__rawOff()
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
333 self.__blockReadyRead = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
334
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335 return bytes(result), err
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
336
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
337 def __execute_paste(self, commands, timeout=0):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
338 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
339 Private method to send commands to the connected device using 'paste' mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
340 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
341
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
342 If no serial connection is available, empty results will be returned.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
343
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
344 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
345 @type str or list of str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
346 @param timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
347 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
348 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
349 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
350 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
351 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
352 if not self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
353 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
354
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
355 if not self.__serial.isConnected():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
356 return b"", b"Device not connected or not switched on."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
357
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
358 if isinstance(commands, list):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
359 commands = "\n".join(commands)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
360
9810
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
361 # switch on paste mode
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
362 self.__blockReadyRead = True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
363 ok = self.__pasteOn()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
364 if not ok:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
365 self.__blockReadyRead = False
9810
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
366 return (b"", b"Could not switch to paste mode. Is the device switched on?")
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
367
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
368 # send commands
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
369 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
370 for command in commands.splitlines(keepends=True):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
371 # send the data as single lines
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
372 commandBytes = command.encode("utf-8")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
373 self.__serial.write(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
374 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
375 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
376 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
377 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
378 ok = self.__serial.readUntil(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
379 if ok != commandBytes:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
380 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
381 return (
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
382 b"",
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
383 "Expected '{0}', got '{1}', followed by '{2}'".format(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
384 commandBytes, ok, self.__serial.readAll()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
385 ).encode("utf-8"),
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
386 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
387
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
388 # switch off paste mode causing the commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
389 self.__pasteOff()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
390 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
391 # read until Python prompt
9821
6b1b06d74532 Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9810
diff changeset
392 result = (
6b1b06d74532 Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9810
diff changeset
393 self.__serial.readUntil(b">>> ", timeout=timeout)
6b1b06d74532 Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9810
diff changeset
394 .replace(b">>> ", b"")
6b1b06d74532 Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9810
diff changeset
395 .strip()
6b1b06d74532 Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9810
diff changeset
396 )
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
397 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
398 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
399 return b"", b"Timeout while processing commands."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
400
9810
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
401 # get rid of any OSD string
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
402 if result.startswith(b"\x1b]0;"):
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
403 result = result.split(b"\x1b\\")[-1]
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
404
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
405 if self.TracebackMarker in result:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
406 errorIndex = result.find(self.TracebackMarker)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
407 out, err = result[:errorIndex], result[errorIndex:]
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
408 else:
9810
39d3b227358c Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9799
diff changeset
409 out = result
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
410 err = b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
411
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
412 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
413 return out, err
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
414
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
415 def executeAsync(self, commandsList, submitMode):
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
416 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
417 Public method to execute a series of commands over a period of time
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
418 without returning any result (asynchronous execution).
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
419
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
420 @param commandsList list of commands to be execute on the device
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
421 @type list of str
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
422 @param submitMode mode to be used to submit the commands
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
423 @type str (one of 'raw' or 'paste')
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
424 @exception ValueError raised to indicate an unknown submit mode
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
425 """
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
426 if submitMode not in ("raw", "paste"):
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
427 raise ValueError("Illegal submit mode given ({0})".format(submitMode))
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
428
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
429 if submitMode == "raw":
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
430 startSequence = [ # sequence of commands to enter raw mode
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
431 b"\x02", # Ctrl-B: exit raw repl (just in case)
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
432 b"\r\x03\x03\x03", # Ctrl-C three times: interrupt any running program
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
433 b"\r\x01", # Ctrl-A: enter raw REPL
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
434 b'print("\\n")\r',
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
435 ]
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
436 endSequence = [
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
437 b"\r",
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
438 b"\x04",
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
439 ]
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
440 self.__executeAsyncRaw(
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
441 startSequence
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
442 + [c.encode("utf-8") + b"\r" for c in commandsList]
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
443 + endSequence
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
444 )
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
445 elif submitMode == "paste":
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
446 self.__executeAsyncPaste(commandsList)
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
447
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
448 def __executeAsyncRaw(self, commandsList):
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
449 """
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
450 Private method to execute a series of commands over a period of time
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
451 without returning any result (asynchronous execution).
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
452
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
453 @param commandsList list of commands to be execute on the device
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
454 @type list of bytes
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
455 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
456 if commandsList:
9764
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
457 command = commandsList.pop(0)
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
458 self.__serial.write(command)
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
459 QTimer.singleShot(2, lambda: self.__executeAsyncRaw(commandsList))
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
460 else:
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
461 self.__rawOff()
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
462 self.executeAsyncFinished.emit()
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
463
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
464 def __executeAsyncPaste(self, commandsList):
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
465 """
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
466 Private method to execute a series of commands over a period of time
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
467 without returning any result (asynchronous execution).
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
468
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
469 @param commandsList list of commands to be execute on the device
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
470 @type list of str
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
471 """
9989
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
472 self.__blockReadyRead = True
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
473 self.__pasteOn()
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
474 command = b"\n".join(c.encode("utf-8)") for c in commandsList)
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
475 self.__serial.write(command)
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
476 self.__serial.readUntil(command)
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
477 self.__blockReadyRead = False
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
478 self.__pasteOff()
286c2a21f36f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9826
diff changeset
479 self.executeAsyncFinished.emit

eric ide

mercurial