One of the most interesting features of MicroPython boards is the ability to connect to a REPL, or read-evaluate-print loop, and interactively run Python code.  Think of this like a Python 'command line' where you type in Python code and instantly see the results of running it.  This is perfect for exploring hardware and learning about Python code.  With the sino:bit MicroPython port you can connect to its REPL just like any other MicroPython board, including the micro:bit.

To demonstrate using the REPL you can install a simple Python serial terminal program.  This program is cross-platform and works on Windows, Mac OSX, and Linux so it's good for demonstrating here.  However check out this MicroPython guide for more information about other REPL tools.

First follow the steps from the ampy page to locate your board's serial port, and to install the PySerial tool (and again note on Windows you need to install a driver to access the serial port).  Inside PySerial it includes both the list_port tool and a miniterm tool that's a handy cross-platform serial terminal.

Once PySerial is installed and you've located the board's serial port, run the following command to open the REPL through the serial port:

python -m --raw <serial port name> 115200

(remember to run with python3 if you pip3 installed PySerial into your Python 3.x installation)

Be sure to specify your board's serial port name in < serial port name >, like COM4, /dev/cu.usbmodem1411, etc.

You should see miniterm start and might not see anything else after that (particularly if your sino:bit is already running code.  Press Ctrl-C to tell the sino:bit to stop running code and get ready for your commands:

Notice a KeyboardInterrupt error occurs (this is how the running program is stopped) and then a Python prompt >>> appears!

Try typing in Python code and pressing enter to see it immediately run, like to print hello world:

print('Hello world!')

You can run any Python code from the REPL!  Try importing the microbit module and using its display scroll function to scroll a message:

import microbit
microbit.display.scroll('Hello world!')

You should see the message scroll across the sino:bit's LED display.  Notice that as the message scrolls you can't type anything else in the REPL.  This is because the sino:bit can only do one thing at a time, and running code like scrolling a message takes over anything else, like running code from the REPL.  Once the message finishes scrolling you'll see the REPL prompt >>> return and you can enter more code.  Remember using the REPL is just like running a Python script line by line--if you run a loop or other complex processing you might have to wait for it to finish!

The REPL is a great way to test out and explore hardware or Python code.  You can do anything from a Python script in the REPL--don't be afraid to experiment and explore!

To quit the miniterm program press Ctrl-] like it mentions at the start.  That's all there is to the basics of using the REPL with the sino:bit!

This guide was first published on Feb 23, 2018. It was last updated on Feb 23, 2018.

This page (Interactive REPL) was last updated on Feb 22, 2018.

Text editor powered by tinymce.