1212 pyb.exec('pyb.Led(1).on()')
1313 pyb.exit_raw_repl()
1414
15+ To run a script from the local machine on the board and print out the results:
16+
17+ import pyboard
18+ pyboard.execfile('test.py', device='/dev/ttyACM0')
19+
1520"""
1621
1722import time
@@ -45,8 +50,8 @@ def eval(self, expression):
4550
4651 def exec (self , command ):
4752 command_bytes = bytes (command , encoding = 'ascii' )
48- for i in range (0 , len (command_bytes ), 10 ):
49- self .serial .write (command_bytes [i :min (i + 10 , len (command_bytes ))])
53+ for i in range (0 , len (command_bytes ), 32 ):
54+ self .serial .write (command_bytes [i :min (i + 32 , len (command_bytes ))])
5055 time .sleep (0.01 )
5156 self .serial .write (b'\x04 ' )
5257 data = self .serial .read (2 )
@@ -55,19 +60,32 @@ def exec(self, command):
5560 data = self .serial .read (2 )
5661 while self .serial .inWaiting () > 0 :
5762 data = data + self .serial .read (self .serial .inWaiting ())
58- time .sleep (0.1 )
63+ time .sleep (0.01 )
5964 if not data .endswith (b'\x04 >' ):
6065 print (data )
6166 raise Exception ('could not exec command' )
6267 if data .startswith (b'Traceback' ) or data .startswith (b' File ' ):
6368 print (data )
6469 raise Exception ('command failed' )
65- return data [:- 2 ]
70+ return str (data [:- 2 ], encoding = 'ascii' )
71+
72+ def execfile (self , filename ):
73+ with open (filename ) as f :
74+ pyfile = f .read ()
75+ return self .exec (pyfile )
6676
6777 def get_time (self ):
6878 t = str (self .exec ('pyb.time()' ), encoding = 'ascii' ).strip ().split ()[1 ].split (':' )
6979 return int (t [0 ]) * 3600 + int (t [1 ]) * 60 + int (t [2 ])
7080
81+ def execfile (filename , device = '/dev/ttyACM0' ):
82+ pyb = Pyboard (device )
83+ pyb .enter_raw_repl ()
84+ output = pyb .execfile (filename )
85+ print (output , end = '' )
86+ pyb .exit_raw_repl ()
87+ pyb .close ()
88+
7189def run_test ():
7290 device = '/dev/ttyACM0'
7391 pyb = Pyboard (device )
0 commit comments