Damien George | 3eece29 | 2015-06-04 23:53:26 +0100 | [diff] [blame] | 1 | Getting a MicroPython REPL prompt |
| 2 | ================================= |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 3 | |
| 4 | REPL stands for Read Evaluate Print Loop, and is the name given to the |
Damien George | 3eece29 | 2015-06-04 23:53:26 +0100 | [diff] [blame] | 5 | interactive MicroPython prompt that you can access on the pyboard. Using |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 6 | the REPL is by far the easiest way to test out your code and run commands. |
| 7 | You can use the REPL in addition to writing scripts in ``main.py``. |
| 8 | |
| 9 | To use the REPL, you must connect to the serial USB device on the pyboard. |
| 10 | How you do this depends on your operating system. |
| 11 | |
| 12 | Windows |
| 13 | ------- |
| 14 | |
| 15 | You need to install the pyboard driver to use the serial USB device. |
| 16 | The driver is on the pyboard's USB flash drive, and is called ``pybcdc.inf``. |
| 17 | |
| 18 | To install this driver you need to go to Device Manager |
| 19 | for your computer, find the pyboard in the list of devices (it should have |
| 20 | a warning sign next to it because it's not working yet), right click on |
| 21 | the pyboard device, select Properties, then Install Driver. You need to |
| 22 | then select the option to find the driver manually (don't use Windows auto update), |
| 23 | navigate to the pyboard's USB drive, and select that. It should then install. |
| 24 | After installing, go back to the Device Manager to find the installed pyboard, |
| 25 | and see which COM port it is (eg COM4). |
Damien George | b27c987 | 2015-01-06 16:09:49 +0000 | [diff] [blame] | 26 | More comprehensive instructions can be found in the |
| 27 | `Guide for pyboard on Windows (PDF) <http://micropython.org/resources/Micro-Python-Windows-setup.pdf>`_. |
| 28 | Please consult this guide if you are having problems installing the driver. |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 29 | |
| 30 | You now need to run your terminal program. You can use HyperTerminal if you |
| 31 | have it installed, or download the free program PuTTY: |
Damien George | 6e6dfdc | 2014-11-02 23:37:02 +0000 | [diff] [blame] | 32 | `putty.exe <http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html>`_. |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 33 | Using your serial program you must connect to the COM port that you found in the |
| 34 | previous step. With PuTTY, click on "Session" in the left-hand panel, then click |
| 35 | the "Serial" radio button on the right, then enter you COM port (eg COM4) in the |
| 36 | "Serial Line" box. Finally, click the "Open" button. |
| 37 | |
| 38 | Mac OS X |
| 39 | -------- |
| 40 | |
| 41 | Open a terminal and run:: |
| 42 | |
| 43 | screen /dev/tty.usbmodem* |
| 44 | |
Damien George | 88d3054 | 2014-10-31 01:37:19 +0000 | [diff] [blame] | 45 | When you are finished and want to exit screen, type CTRL-A CTRL-\\. |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 46 | |
| 47 | Linux |
| 48 | ----- |
| 49 | |
| 50 | Open a terminal and run:: |
| 51 | |
| 52 | screen /dev/ttyACM0 |
| 53 | |
| 54 | You can also try ``picocom`` or ``minicom`` instead of screen. You may have to |
| 55 | use ``/dev/ttyACM1`` or a higher number for ``ttyACM``. And, you may need to give |
| 56 | yourself the correct permissions to access this devices (eg group ``uucp`` or ``dialout``, |
| 57 | or use sudo). |
| 58 | |
| 59 | Using the REPL prompt |
| 60 | --------------------- |
| 61 | |
Damien George | 3eece29 | 2015-06-04 23:53:26 +0100 | [diff] [blame] | 62 | Now let's try running some MicroPython code directly on the pyboard. |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 63 | |
| 64 | With your serial program open (PuTTY, screen, picocom, etc) you may see a blank |
| 65 | screen with a flashing cursor. Press Enter and you should be presented with a |
Damien George | 3eece29 | 2015-06-04 23:53:26 +0100 | [diff] [blame] | 66 | MicroPython prompt, i.e. ``>>>``. Let's make sure it is working with the obligatory test:: |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 67 | |
| 68 | >>> print("hello pyboard!") |
| 69 | hello pyboard! |
| 70 | |
| 71 | In the above, you should not type in the ``>>>`` characters. They are there to |
| 72 | indicate that you should type the text after it at the prompt. In the end, once |
| 73 | you have entered the text ``print("hello pyboard!")`` and pressed Enter, the output |
| 74 | on your screen should look like it does above. |
| 75 | |
| 76 | If you already know some python you can now try some basic commands here. |
| 77 | |
| 78 | If any of this is not working you can try either a hard reset or a soft reset; |
| 79 | see below. |
| 80 | |
| 81 | Go ahead and try typing in some other commands. For example:: |
| 82 | |
| 83 | >>> pyb.LED(1).on() |
| 84 | >>> pyb.LED(2).on() |
| 85 | >>> 1 + 2 |
| 86 | 3 |
| 87 | >>> 1 / 2 |
| 88 | 0.5 |
| 89 | >>> 20 * 'py' |
| 90 | 'pypypypypypypypypypypypypypypypypypypypy' |
| 91 | |
| 92 | Resetting the board |
| 93 | ------------------- |
| 94 | |
| 95 | If something goes wrong, you can reset the board in two ways. The first is to press CTRL-D |
Damien George | 3eece29 | 2015-06-04 23:53:26 +0100 | [diff] [blame] | 96 | at the MicroPython prompt, which performs a soft reset. You will see a message something like :: |
Damien George | d19c256 | 2014-09-25 17:21:59 +0100 | [diff] [blame] | 97 | |
| 98 | >>> |
| 99 | PYB: sync filesystems |
| 100 | PYB: soft reboot |
| 101 | Micro Python v1.0 on 2014-05-03; PYBv1.0 with STM32F405RG |
| 102 | Type "help()" for more information. |
| 103 | >>> |
| 104 | |
| 105 | If that isn't working you can perform a hard reset (turn-it-off-and-on-again) by pressing the RST |
| 106 | switch (the small black button closest to the micro-USB socket on the board). This will end your |
| 107 | session, disconnecting whatever program (PuTTY, screen, etc) that you used to connect to the pyboard. |
| 108 | |
| 109 | If you are going to do a hard-reset, it's recommended to first close your serial program and eject/unmount |
| 110 | the pyboard drive. |