eric6/MicroPython/MicroPythonCommandsInterface.py

branch
micropython
changeset 7108
4f6133a01c6a
parent 7102
5e77aa4671e6
child 7112
701cdc76f887
equal deleted inserted replaced
7103:aea236dc8002 7108:4f6133a01c6a
11 11
12 import ast 12 import ast
13 import time 13 import time
14 import os 14 import os
15 15
16 from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QThread, QTimer 16 from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject, QThread, QTimer, QCoreApplication, QEventLoop
17 17
18 from .MicroPythonSerialPort import MicroPythonSerialPort 18 from .MicroPythonSerialPort import MicroPythonSerialPort
19 19
20 import Preferences 20 import Preferences
21 21
135 @@rtype bool 135 @@rtype bool
136 """ 136 """
137 if not self.__serial: 137 if not self.__serial:
138 return False 138 return False
139 139
140 rawReplMessage = b"raw REPL; CTRL-B to exit\r\n" 140 rawReplMessage = b"raw REPL; CTRL-B to exit\r\n>"
141 softRebootMessage = b"soft reboot\r\n" 141 softRebootMessage = b"soft reboot\r\n"
142 142
143 self.__serial.write(b"\x02") # end raw mode if required 143 self.__serial.write(b"\x02") # end raw mode if required
144 self.__serial.waitForBytesWritten() 144 self.__serial.waitForBytesWritten()
145 for _i in range(3): 145 for _i in range(3):
158 return False 158 return False
159 159
160 # some MicroPython devices seem to need to be convinced in some 160 # some MicroPython devices seem to need to be convinced in some
161 # special way 161 # special way
162 data = self.__serial.readUntil(rawReplMessage) 162 data = self.__serial.readUntil(rawReplMessage)
163 if self.__serial.hasTimedOut():
164 return False
165 if not data.endswith(rawReplMessage): 163 if not data.endswith(rawReplMessage):
166 self.__serial.write(b"\r\x01") # send CTRL-A again 164 self.__serial.write(b"\r\x01") # send CTRL-A again
167 self.__serial.readUntil(rawReplMessage) 165 self.__serial.readUntil(rawReplMessage)
168 if self.__serial.hasTimedOut(): 166 if self.__serial.hasTimedOut():
169 return False 167 return False
168 QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)
170 self.__serial.readAll() # read all data and discard it 169 self.__serial.readAll() # read all data and discard it
171 return True 170 return True
172 171
173 def __rawOff(self): 172 def __rawOff(self):
174 """ 173 """
715 ] 714 ]
716 out, err = self.execute(commands) 715 out, err = self.execute(commands)
717 if err: 716 if err:
718 raise IOError(self.__shortError(err)) 717 raise IOError(self.__shortError(err))
719 718
720 def showTime(self): 719 def getTime(self):
721 """ 720 """
722 Public method to get the current time of the device. 721 Public method to get the current time of the device.
723 722
724 @return time of the device 723 @return time of the device
725 @rtype str 724 @rtype str
726 @exception IOError raised to indicate an issue with the device 725 @exception IOError raised to indicate an issue with the device
727 """ 726 """
728 commands = [ 727 commands = [
729 "import time", 728 "import time",
730 "print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))", 729 "\n".join([
731 # __IGNORE_WARNING_M601__ 730 "try:",
731 " print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()"
732 "))",
733 # __IGNORE_WARNING_M601__
734 "except AttributeError:",
735 " tm = time.localtime()",
736 " print('{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'"
737 ".format(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour,"
738 " tm.tm_min, tm.tm_sec))",
739 ]),
732 ] 740 ]
733 out, err = self.execute(commands) 741 out, err = self.execute(commands)
734 if err: 742 if err:
735 raise IOError(self.__shortError(err)) 743 raise IOError(self.__shortError(err))
736 return out.decode("utf-8").strip() 744 return out.decode("utf-8").strip()

eric ide

mercurial