I would like to run a file on the esp with mpfshell.
There is a command exec in mpfshell but that does not work and is not documented.
Ampy has a run - function, but I like to use it with a websocket.
mpfshell: how to execute a file
Re: mpfshell: how to execute a file
Not the answer, but basically the same question.
rshell's remote copy command runs the following code on the remote:
with the effect of saving everything received from a matching method running on the host. Could this function be modified: instead of saving the received content to dst_filename it instead passes the received data to exec and returns the result of the execution?
Looks like a great potential extension of rshell - unfortunately I don't know enough Python to make the modifications without some hints.
rshell's remote copy command runs the following code on the remote:
Code: Select all
def recv_file_from_host(src_file, dst_filename, filesize, dst_mode='wb'):
"""Function which runs on the pyboard. Matches up with send_file_to_remote."""
import sys
import ubinascii
if HAS_BUFFER:
try:
import pyb
usb = pyb.USB_VCP()
except:
try:
import machine
usb = machine.USB_VCP()
except:
usb = None
if usb and usb.isconnected():
# We don't want 0x03 bytes in the data to be interpreted as a Control-C
# This gets reset each time the REPL runs a line, so we don't need to
# worry about resetting it ourselves
usb.setinterrupt(-1)
try:
with open(dst_filename, dst_mode) as dst_file:
bytes_remaining = filesize
if not HAS_BUFFER:
bytes_remaining *= 2 # hexlify makes each byte into 2
buf_size = BUFFER_SIZE
write_buf = bytearray(buf_size)
read_buf = bytearray(buf_size)
while bytes_remaining > 0:
read_size = min(bytes_remaining, buf_size)
buf_remaining = read_size
buf_index = 0
while buf_remaining > 0:
if HAS_BUFFER:
bytes_read = sys.stdin.buffer.readinto(read_buf, bytes_remaining)
else:
bytes_read = sys.stdin.readinto(read_buf, bytes_remaining)
if bytes_read > 0:
write_buf[buf_index:bytes_read] = read_buf[0:bytes_read]
buf_index += bytes_read
buf_remaining -= bytes_read
if HAS_BUFFER:
dst_file.write(write_buf[0:read_size])
else:
dst_file.write(ubinascii.unhexlify(write_buf[0:read_size]))
# Send back an ack as a form of flow control
sys.stdout.write('\x06')
bytes_remaining -= read_size
return True
except:
return False
Code: Select all
send_file_to_remote
Looks like a great potential extension of rshell - unfortunately I don't know enough Python to make the modifications without some hints.
Bernhard Boser
Re: mpfshell: how to execute a file
rshell could definitely be modified to exec a file after copying.
I normally just use the repl from within rshell.
You can also use pyboard.py to execute a file from the host on the pyboard. This method doesn't copy it to the filesystem, just loads it into RAM and executes it.
I normally just use the repl from within rshell.
You can also use pyboard.py to execute a file from the host on the pyboard. This method doesn't copy it to the filesystem, just loads it into RAM and executes it.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: mpfshell: how to execute a file
The tests directory in the source tree illustrates remote execution and capturing results. The subdirectory pyb contains tests intended to run on an attached Pyboard.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: mpfshell: how to execute a file
I added a run command to rshell at https://github.com/bboser/rshell.
Bernhard Boser