repl.rst 4.3 KB

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