@@ -148,6 +148,8 @@ def __init__(self, device, baudrate=115200, user='micro', password='python', wai
148148 if delayed :
149149 print ('' )
150150
151+ self .uioutput = ""
152+
151153 def close (self ):
152154 self .serial .close ()
153155
@@ -160,8 +162,41 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
160162 if data .endswith (ending ):
161163 break
162164 elif self .serial .inWaiting () > 0 :
163- new_data = self .serial .read (1 )
165+ new_data = self .serial .read (1 )
166+ data = data + new_data
167+ if data_consumer :
168+ data_consumer (new_data )
169+ timeout_count = 0
170+ else :
171+ timeout_count += 1
172+ if timeout is not None and timeout_count >= 100 * timeout :
173+ break
174+ time .sleep (0.01 )
175+ return data
176+
177+ def read_until_output (self , min_num_bytes , ending , timeout = 10 , data_consumer = None ,ter = None ):
178+ data = self .serial .read (min_num_bytes )
179+ if data_consumer :
180+ data_consumer (data )
181+ timeout_count = 0
182+ while True :
183+ if data .endswith (ending ):
184+ break
185+ elif self .serial .inWaiting () > 0 :
186+ new_data = self .serial .read (1 )
164187 data = data + new_data
188+ try :
189+ ded = str (data ).split ("\\ " )
190+ if ded [- 1 ]== "n'" and ded [- 2 ]== "r" :
191+ ded = str (data )[2 :- 5 ]
192+ self .uioutput = self .uioutput + ded + "\n "
193+ print (self .uioutput )
194+ ter .value = self .uioutput
195+ ded = None
196+ data = b''
197+ except Exception as e :
198+ print (e )
199+
165200 if data_consumer :
166201 data_consumer (new_data )
167202 timeout_count = 0
@@ -215,21 +250,23 @@ def exit_raw_repl(self):
215250 self .serial .write (b'\r \x02 ' ) # ctrl-B: enter friendly REPL
216251
217252 def follow (self , timeout , data_consumer = None ):
253+
218254 # wait for normal output
219255 data = self .read_until (1 , b'\x04 ' , timeout = timeout , data_consumer = data_consumer )
220256 if not data .endswith (b'\x04 ' ):
221257 raise PyboardError ('timeout waiting for first EOF reception' )
222258 data = data [:- 1 ]
223-
224259 # wait for error output
225260 data_err = self .read_until (1 , b'\x04 ' , timeout = timeout )
226261 if not data_err .endswith (b'\x04 ' ):
227262 raise PyboardError ('timeout waiting for second EOF reception' )
228263 data_err = data_err [:- 1 ]
229-
230264 # return normal and error output
231265 return data , data_err
232266
267+ def follow_output (self , timeout , data_consumer = None ,term = None ):
268+ data = self .read_until_output (1 , b'\x04 ' , timeout = timeout , data_consumer = data_consumer ,ter = term )
269+
233270 def exec_raw_no_follow (self , command ):
234271 if isinstance (command , bytes ):
235272 command_bytes = command
@@ -280,11 +317,13 @@ def get_time(self):
280317# but for Python3 we want to provide the nicer version "exec"
281318setattr (Pyboard , "exec" , Pyboard .exec_ )
282319
283- def execfile (filename , device = '/dev/ttyACM0' , baudrate = 115200 , user = 'micro' , password = 'python' ):
320+ def execfile (filename , device = '/dev/ttyACM0' , baudrate = 115200 , user = 'micro' , password = 'python' , terminal = None ):
284321 pyb = Pyboard (device , baudrate , user , password )
285322 pyb .enter_raw_repl ()
286- output = pyb .execfile (filename )
287- stdout_write_bytes (output )
323+ with open (filename , 'rb' ) as f :
324+ pyfile = f .read ()
325+ pyb .exec_raw_no_follow (pyfile )
326+ pyb .follow_output (10 , None , term = terminal )
288327 pyb .exit_raw_repl ()
289328 pyb .close ()
290329
0 commit comments