My IDE configuration using Notepad++, ampy and Tera Term

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

My IDE configuration using Notepad++, ampy and Tera Term

Post by pmulvey » Wed Oct 16, 2019 10:26 pm

I use Notepad++ for editing, ampy for downloading and Tera Term for serial terminal. In Notepad++ I use NPPExec to execute the following commands:
NPP_SAVE
ampy --port COM4 put $(FULL_CURRENT_PATH)
reset.bat
exit

The file is first saved, then downloaded via COM4. The reset.bat file sends a Ctrl-D to COM4 and resets the esp8266. I set up a shortcut key Alt-Z to trigger the NPPExec script above. So Alt-Z saves, downloads and resets the device. Without the reset in the script you have to go to Tera Term, connect to the port and do a Ctrl-D, a bit of a pain if you don't need the REPL.

Reset.bat contains:
echo off
mode COM4 BAUD=115200 PARITY=n DATA=8 >nul
echo off
echo ** >COM4

To get the actual Ctrl-D character into a text file type the following in DOS:
echo {Do the key combinations CTRL-P followed by CTRL-D} > temp.bat

Edit this file with Notepad++ and you will see in reverse colours DLE EOT which it displays for CTRL-P and CTRL-D these should be pasted into the reset.bat file in place of the two asterisks.
Paul Mulvey

pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by pmulvey » Sat Oct 26, 2019 7:58 pm

This works great on my home laptop (Win 8.1) but when I tried it on the students' Win 7 machines it doesn't work. Seems to be something different going on at the DOS level. Anyway I made a grand improvement that does work.

My Project has a workspace folder with a globals folder in this so I have a package arrangement with the __init__.py in the globals folder. So the task is to use a shortcut key to execute a NPPExec script:

Code: Select all

NPP_CONSOLE -

set port = COM4

cd  $(CURRENT_DIRECTORY)
npe_console v+					// Send console to the $(OUTPUTL) variable
cmd /c if exist "boot.py" echo Exists	// Am I in the workspace folder?
npe_console v-

if "$(OUTPUTL)" = "Exists"
   NPP_SAVE					// Save the file
   NPP_CONSOLE +
   ampy --port $(port) put $(FULL_CURRENT_PATH)	// Download to ESP
   NPP_CONSOLE -
   cd  $(CURRENT_DIRECTORY)
   cd..						// The SendCTRLD.py script is up one level from here
   NPP_CONSOLE +
   py SendCTRLD.py $(port)
else
   NPP_SAVE
   NPP_CONSOLE +   
   ampy --port $(port) put $(FULL_CURRENT_PATH) \Globals\$(FILE_NAME)
   NPP_CONSOLE -
   cd  $(CURRENT_DIRECTORY)
   cd..						// The SendCTRLD.py script is up two levels from here
   cd..
   NPP_CONSOLE +   
   py SendCTRLD.py $(port)
   NPP_CONSOLE +   
exit
echo No

This is SendCTRLD.py which sends a CTRL-D to the Board. So one shortcut key saves the file, downloads it and does a soft reset on the ESP8266.

Code: Select all

import serial
import sys

ser = serial.Serial(
    port=sys.argv[1],
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.write(serial.to_bytes([0x04]))
ser.close()
print("Board Reset")
Paul Mulvey

pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by pmulvey » Thu Dec 12, 2019 10:45 pm

Ignore my previous posts... this is a work in progress!

When I am using Tera Term to view serial output I had to close the connection before downloading and then open it again because ampy needs the port. In order to improve the automation around programming the device I have added two python programs, DisTeraTerm to close it and ConTeraTerm to reconnect. These essentially find the Tera Term application and send it the appropriate keystrokes. I have just finished it so I'm not 100% sure how bullet proof it is. The updated NPPExec script now looks like this:

Code: Select all

NPP_CONSOLE -
set port = COM4
cd  $(CURRENT_DIRECTORY)
npe_console v+
cmd /c if exist "boot.py" echo Exists
npe_console v-
if "$(OUTPUTL)" = "Exists"
   NPP_SAVE
   cd..
   NPP_CONSOLE +
   py DisTeraTerm.py $(port)
   ampy --port $(port) put $(FULL_CURRENT_PATH)
   NPP_CONSOLE -
   cd  $(CURRENT_DIRECTORY)
   cd..
   NPP_CONSOLE +
   py SendCTRLD.py $(port)
   py ConTeraTerm.py
else
   NPP_SAVE
   cd..
   cd..
   NPP_CONSOLE + 
   py DisTeraTerm.py $(port) 
   ampy --port $(port) put $(FULL_CURRENT_PATH) \Globals\$(FILE_NAME)
   NPP_CONSOLE -
   cd  $(CURRENT_DIRECTORY)
   cd..
   cd..
   NPP_CONSOLE +   
   py SendCTRLD.py $(port)
   py ConTeraTerm.py
   NPP_CONSOLE +   
exit

echo No
This is DisTeraTerm.py:

Code: Select all

import win32api
import win32ui
from time import sleep
import sys
teraterm = sys.argv[1] + " - Tera Term VT"
try:
    PyCWnd1 = win32ui.FindWindow( None, teraterm )
    print ("Stopping " + teraterm)
    PyCWnd1.SetForegroundWindow()
    PyCWnd1.SetFocus()
    win32api.keybd_event(0x12, 0, ) # Alt
    win32api.keybd_event(0x12, 0, 2 ) # Alt release
    win32api.keybd_event(0x46, 0, ) # F
    win32api.keybd_event(0x44, 0, ) # D
except:
    print ("No Tera Term")
and ConTeraTerm.py is:

Code: Select all

import win32api
import win32ui
from time import sleep
import sys
teraterm = "Tera Term - [disconnected] VT"
try:
    PyCWnd1 = win32ui.FindWindow( None, teraterm )
    print ("Starting " + teraterm)
    PyCWnd1.SetForegroundWindow()
    PyCWnd1.SetFocus()
    win32api.keybd_event(0x12, 0, ) # Alt
    win32api.keybd_event(0x12, 0, 2 ) # Alt release
    win32api.keybd_event(0x46, 0, ) # F
    win32api.keybd_event(0x4E, 0, ) # N
    win32api.keybd_event(0x2B, 0, ) # Enter Key
except:
    print ("No Tera Term")

This folder structure is

Code: Select all

ProjectX
     DisTeraTerm.py
     ConTeraTerm.py
     SendCTRLD.py
     workspace
        boot.py
        main.py
        MyProg.py
        Globals
           MyLibrary.py
           LCDLib.py
   
I would welcome feedback on this.
Paul Mulvey

pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by pmulvey » Wed Jan 29, 2020 10:19 am

Just discovered, Thonny 3.2.7 does all of the above!
Paul Mulvey

adomanim
Posts: 3
Joined: Wed Dec 25, 2019 5:55 pm

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by adomanim » Tue Feb 04, 2020 4:55 pm

Does anyone use Notepad++ for coding? How can I use its features effectively for C++?





Lucky Patcher Kodi nox

tll
Posts: 13
Joined: Fri Feb 07, 2020 10:16 am

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by tll » Sat Feb 08, 2020 6:54 pm

Paul, great pieces of Python!

I use EditPadPro, and since I started with MC, I defined "tools" on it called "μP Put" and "μP PutAsMain", which appear as icons on the tool bar when a python file is selected. Each tool invocation causes the file to be saved and an specific CMD file to be called. Very similar to what you are doing.

I also have a "μP Run" which upon ampy run completion, returned an open editor file with the run output. It was very handy for syntax debugging. It somehow got broken recently, not sure why, now I do not get the redirected ampy output... I have not had the time to look into it.

This tools had the annoying requirement of having to stop Teraterm. Now I have added your Python pieces, which seem to be working fine. Upon completion of the tool runs, I end up on the Teraterm window, which I am not sure yet, is what I want or not. But in any case a great improvement.

Thanks!

pmulvey
Posts: 45
Joined: Sun Jul 29, 2018 8:12 am
Location: Athlone, Ireland

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by pmulvey » Thu Feb 27, 2020 11:39 pm

I have since found that on some PCs (old ones) that my python script for sending keystrokes to TeraTerm was unreliable. After some experimentation I added a sleep(.2) just before the line that sends the keystrokes and this seems to resolve the issue.
Having thought that Thonny was going do all of this for me I found that it was very unreliable for a variety of reasons, crashing and raising errors on the device. I cannot understand why there is not an IDE specifically for the very popular world of MicroPython. Still it's great fun cobbling together my own home brew IDE from bits and pieces!
Paul Mulvey

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by aivarannamaa » Sun Mar 01, 2020 11:15 am

pmulvey wrote:
Thu Feb 27, 2020 11:39 pm
Having thought that Thonny was going do all of this for me I found that it was very unreliable for a variety of reasons, crashing and raising errors on the device.
I'm Thonny's author. The main problem is that I haven't been able to reproduce most of the reported crashes. The serial communication just seems to be too fragile with some combinations of specific device, OS and USB driver.

In the next Thonny version I plan to reduce the block size when writing to ESP-32 and ESP-8266 devices, add configuration parameters for tweaking block size and delays and increase some read timeouts. I'm already using separate thread to read device's output as fast as possible (buffer overruns seemed to be my main problem in early versions).

I'm very interested in more ideas about how to make the communication more reliable (without hurting performance too much). Can I somehow detect the receiving capability of the device so that I can choose optimal writing block size dynamically? Currently I'm using raw-mode for executing commands. I have considered using paste-mode instead and treating echo as feedback mechanism. Any thoughts about this?
Aivar Annamaa
https://thonny.org

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by stanely » Sun Mar 01, 2020 1:58 pm

I started with Notepad++ but couldn't get it to colorize Python correctly so I switched to Atom. That was a good decision. Atom has github built-in so that speeds things up too. So for my desktop development I use Atom and cmd.

For ESP32 I use Mu. The editor isn't the greatest, but it's workable. What makes Mu really nice and fast is that it has a local/device folder view that mostly eliminates the need for Ampy. It also has a REPL window, so there's no need for a terminal emulator like TeraTerm, etc. Basically it's a one-stop-shop for ESP32 Python development. I think the idea is similar to Thonny, which I haven't tried, and uPyCraft which I tried, but it wasn't very far along.

stanely
Posts: 55
Joined: Fri Jan 17, 2020 5:19 am
Location: Ohio, USA

Re: My IDE configuration using Notepad++, ampy and Tera Term

Post by stanely » Sun Mar 01, 2020 2:18 pm

aivarannamaa wrote:
Sun Mar 01, 2020 11:15 am
... The serial communication just seems to be too fragile with some combinations of specific device, OS and USB driver.
This is probably off topic from this thread, but are you using flow control when communicating to MicroPython targets? I'm using Ampy and esptool at crazy fast baud rates without any issues, ever. I can't imagine all of that would work reliably without use of CTS/RTS flow control.

Post Reply