Controlling the micropython repl from Python with PySerial

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
salimfadhley
Posts: 22
Joined: Thu Jun 19, 2014 10:18 pm

Controlling the micropython repl from Python with PySerial

Post by salimfadhley » Sun Oct 12, 2014 11:53 pm

I'd like to be able to control my Micropython's REPL from my PC.

Initially this is just going to do some trivial stuff (e.g. execute some python), but once I get the basics sorted I'd like to make a tool that automates deploying and testing of code to the pyboard.

I was able to write a few bytes to the board (visible when I manually connect to the repl), but I was not able to actually execute a statement.

Can anybody see an error in my code?

Code: Select all

import logging
import serial
from contextlib import contextmanager
from fdpexpect import fdspawn

@contextmanager
def connect(port=r'/dev/ttyACM0'):
    ser = serial.Serial(port, timeout=1)
    yield fdspawn(ser)
    ser.close()
    
log = logging.getLogger(__name__)

def main():
    text = ["import pyb", "pyb.LED(1).on()"]
    with connect() as ser:
        for line in text:
            ser.expect_exact('>>>', timeout=1)
            ser.send("%s\n" % line)
        
if __name__ == '__main__':
    logging.basicConfig()
    main()

mosi
Posts: 28
Joined: Tue Oct 07, 2014 12:07 am

Re: Controlling the micropython repl from Python with PySeri

Post by mosi » Mon Oct 13, 2014 12:12 am

I am trying to imlpement serial protocol form PC to pyboard as well.

Here are my intermediate findings:

1) RAW REPL console is started with Ctrl+d (translate that into bytes) and finished with Ctrl+b (works like Enter in normal REPL)

2) on my mac in other embedded projects, usually \n does not cut it, I needed '\r\n' to print a newline

3) my REPL on pyboardV3 "freezes" randomly

4) Processing 1.5 (and 2.0) is a universal tool with the ONLY working serial comm. implementation on all three operating systems

Will try your code when I get the chance, which might take days.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Controlling the micropython repl from Python with PySeri

Post by dhylands » Mon Oct 13, 2014 12:24 am

mosi wrote:I am trying to imlpement serial protocol form PC to pyboard as well.

Here are my intermediate findings:

1) RAW REPL console is started with Ctrl+d (translate that into bytes) and finished with Ctrl+b (works like Enter in normal REPL)
RAW REPL is started with Control-A. There is a script in the tools directory called pyboard.py which sends a python script to the pyboad using th raw repl.
mosi wrote:2) on my mac in other embedded projects, usually \n does not cut it, I needed '\r\n' to print a newline
That's normal for serial. \r returns the cursor to column 1 of the current line. \n advances the cursor to the same column of the next line.
mosi wrote:3) my REPL on pyboardV3 "freezes" randomly
Are you running the latest firmware? I remember some problems like this from the early days, but I haven't seen anything like this recently.
mosi wrote:4) Processing 1.5 (and 2.0) is a universal tool with the ONLY working serial comm. implementation on all three operating systems

Will try your code when I get the chance, which might take days.

mosi
Posts: 28
Joined: Tue Oct 07, 2014 12:07 am

Re: Controlling the micropython repl from Python with PySeri

Post by mosi » Mon Oct 13, 2014 6:50 pm

dhylands wrote: RAW REPL is started with Control-A. There is a script in the tools directory called pyboard.py which sends a python script to the pyboad using th raw repl.
Correct.

Thank you for the pyboard.py example, looks like Ctrl+a, Ctrl+b Ctrl+c Ctrl+d all are implemented, perfect!


zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Controlling the micropython repl from Python with PySeri

Post by zaord » Fri Dec 11, 2020 11:57 am

mosi wrote:2) on my mac in other embedded projects, usually \n does not cut it, I needed '\r\n' to print a newline
That's normal for serial. \r returns the cursor to column 1 of the current line. \n advances the cursor to the same column of the next line.


I solve this like that :

Code: Select all

import sys
from rshell import main
from rshell import pyboard

pyb = pyboard.Pyboard('COM4')

pyb.enter_raw_repl()
sys.stdout.write(pyb.exec("import pyb"))

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Controlling the micropython repl from Python with PySeri

Post by zaord » Fri Dec 11, 2020 11:59 am

Thank you for the pyboard.py example, looks like Ctrl+a, Ctrl+b Ctrl+c Ctrl+d all are implemented, perfect!
How can you send a ctrl+a in raw repl ???

Something like pyb.exec("ctrl+a") ?

Post Reply