Basic VS Code use and setup

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
andymouse
Posts: 12
Joined: Sat Apr 06, 2019 7:05 pm

Basic VS Code use and setup

Post by andymouse » Wed Apr 10, 2019 11:01 am

Hi,
In order to follow a tutorial I want to use Visual Studio Code, and when I open a Terminal, Windows 10, Power Shell is running and I will need to get used to its commands, but I would like to be able to tell it which serial port I am using just to get me moving with Micropython and have basic operation of my board all under one roof (so to speak!)
So my question is how do I tell it I'm on comm3 ? and get access to the Repl, also, what is the best place to find PS commands and explanations. Every journey and all that!
If you think Power Shell is a bad Idea for someone starting out please say so and put me straight. Please give as much detail as you can (you CAN teach an old dog new tricks, but slowly!)

Thanks.
Andy

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

Re: Basic VS Code use and setup

Post by dhylands » Wed Apr 10, 2019 1:45 pm

I wrote a python program I call find_port.py. You can find a copy of it here: https://github.com/dhylands/dotfiles/bl ... nd_port.py

It should work on Windows, Mac, or Linux. If you run

Code: Select all

find_port.py -l
then it should list all of the USB serial devices. For example, under Windows 10, I tested this in PowerShell with a SparkFun ESP32 Thing plugged in:

Code: Select all

PS C:\Users\dhyla\bin> python .\find_port.py -l
USB Serial Device 1027:24597 with vendor 'FTDI' serial 'DN02Z2XCA' found @COM5
You can also go into Device Manager, click on "Ports (COM & LPT)" and in my case it shows "USB Serial Port (COM5)"

andymouse
Posts: 12
Joined: Sat Apr 06, 2019 7:05 pm

Re: Basic VS Code use and setup

Post by andymouse » Wed Apr 10, 2019 2:19 pm

hi
Thanks, but I no which port I need its much simpler than that, I just dont no what to type in power shell to use the terminal and have the Repl command line, I realy am this new! I am in VScode and I have:-

PS C:\Users\andy>

I dont no where to go from here, and why I am doing it, I believe I have to point the terminal at a path to the com port maybe type "-p" or "com3"
something like that so I need a little jog to get me going! I will invest time in PowerShell, but for the moment I want to communicate with my board in the same manner as "Putty" but in the VisualCode environment using its terminal, I did use device manager to find my CH340 device, but I am not used to "command line" work, and any help you can give would be great as I'm itching to move on.

Andy

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

Re: Basic VS Code use and setup

Post by dhylands » Wed Apr 10, 2019 2:53 pm

Basically, you need to execute a command line terminal program and pass whatever arguments that particular command line terminal program wants. Most terminal programs (like putty) for Windows are GUI based and not command line based.

However, python includes one called miniterm which comes with pyserial.

From powershell, I was able to execute: python -m serial.tools.miniterm COM5 115200 and get a REPL.

Code: Select all

PS C:\Users\dhyla> python -m serial.tools.miniterm COM5 115200
--- Miniterm on COM5  115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

>>>
--- exit ---
You'll probably need to press RETURN to get the >>> prompt or perhaps press Control-C if you have a main.py that runs something on the board. You could also use rshell. You would run rshell -p COM5 This would get you to an rshell prompt where you can enter rshell commands to copy files into and out of the micropython filesystem, or use the repl command to get into a micropyton repl.

Code: Select all

PS C:\Users\dhyla> rshell -p COM5
Using buffer-size of 32
Connecting to COM5 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /flash/
Setting time ... Apr 10, 2019 07:47:50
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 1970
Welcome to rshell. Use the exit command to exit rshell.
C:\Users\dhyla> repl
Entering REPL. Use Control-X to exit.
>
MicroPython ESP32_LoBo_v3.2.12 - 2018-04-18 on ESP32 board with ESP32
Type "help()" for more information.
>>>
>>>
C:\Users\dhyla>
PS C:\Users\dhyla>
If you are going to use rshell to copy files in and out of the micropython board, then I recommend disabling USB mass storage.

andymouse
Posts: 12
Joined: Sat Apr 06, 2019 7:05 pm

Re: Basic VS Code use and setup

Post by andymouse » Wed Apr 10, 2019 6:36 pm

Hi
Your a rock star! that does indeed get me to the Repl and I have switched an LED off, so very close, however (you where waiting for that!) I have some problems.
the "back space" key doesn't work, I just get a strange graphic, for instance if I type "import" wrong, and press the backspace key I get:-

>>> impor␛[␛[␛[␛[K

also the "delete" key gives me a capital "S"

When I go back to Putty all is ok. And as long as I dont make any mistakes all appears well from VScode as well:-

>>> import os
>>> os.listdir()
['boot.py', 'main.py']
>>> import machine
>>> from machine import Pin
>>> led = Pin(2,Pin.OUT)
>>> led.on()
>>> led.off()
>>> led.value(True)
>>> led.value(False)
>>> led.value(1)
>>> led.value(0)
>>>

I realy thought I was there, please can you help me a bit further and give me your thoughts on the problems. I am from "Arduino land" and I guess I'm used to having this stuff done for me! buy the IDE, but I am a willing student.
Thanks
Andy

User avatar
apollodriver
Posts: 31
Joined: Tue Jan 29, 2019 8:54 am

Re: Basic VS Code use and setup

Post by apollodriver » Sat Oct 19, 2019 1:39 pm

hello dear dhylands hello dear andymouse, ;)


many thanks for stepping up the plate and bringing up ths important topic.

as i am heading these things: see the basic-setup - with some important essentials
well - question: is this a possible solution to the com-port issue!?
note: i also have had such issues recently - with the setup of upyCraft.

i have found some ideas and hints here: https://python-forum.io/Thread-VS-Code- ... ght=VSCode

see:
With code. from command line in any folder will open files in that folder in VS Code.
Example with virtual environment that build into Python venv
with the following example:

Code: Select all

# Make
E:\div_code
λ python -m venv my_env
 
# cd in
E:\div_code
λ cd my_env
 
# Activate
E:\div_code\my_env
λ E:\div_code\my_env\Scripts\Activate
 
# Test pip
(my_env) E:\div_code\my_env
λ pip -V
pip 10.0.1 from e:\div_code\my_env\lib\site-packages\pip (python 3.7
 
# Install required package 
(my_env) E:\div_code\my_env
λ pip install requests
Collecting requests
 
# Start VS Code
(my_env) E:\div_code\my_env
λ code .
See now that it automatically find Python interpreter in virtual environment.
So push run button it will use Python version in virtual environment.

see also https://docs.python-guide.org/dev/virtualenvs/

This tutorial walks you through installing and using Python packages. It will show you how to install and use the necessary tools and make strong recommendations on best practices. Keep in mind that Python is used for a great many different purposes, and precisely how you want to manage your dependencies may change based on how you decide to publish your software. The guidance presented here is most directly applicable to the development and deployment of network services (including web applications), but is also very well suited to managing development and testing environments for any kind of project.

Installing Pipenv
Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases.

Use pip to install Pipenv:

Code: Select all

$ pip install --user pipenv

Note see this ....
This does a user installation to prevent breaking any system-wide packages. If pipenv isn’t available in your shell after installation, you’ll need to add the user base’s binary directory to your PATH. On Linux and macOS you can find the user base binary directory by running python -m site --user-base and adding bin to the end. For example, this will typically print ~/.local (with ~ expanded to the absolute path to your home directory) so you’ll need to add ~/.local/bin to your PATH. You can set your PATH permanently by modifying ~/.profile.
On Windows you can find the user base binary directory by running py -m site --user-site and replacing site-packages with Scripts. For example, this could return C:\Users\Username\AppData\Roaming\Python36\site-packages so you would need to set your PATH to include C:\Users\Username\AppData\Roaming\Python36\Scripts. You can set your user PATH permanently in the Control Panel. You may need to log out for the PATH changes to take effect.

see also https://docs.python-guide.org/dev/virtualenvs/

well - question: is this a possible solution to the com-port issue!?

i also have had such issues recently - with the setup of upyCraft.

best regards apollo
WPGear.org is a compendium of useful developer tools for working with WordPress.

Post Reply