0

I have a programming tool for an ESP32 platform and this programming tool should use the esptool and a task to program the ESP32. This is done by a subprocess:

while(True):
    Command = "{}".format(os.getcwd()) + os.path.sep + "esptool.py --port {} --after hard_reset flash_id".format(Port)
    print("[DEBUG {}] Run command {}".format(Index, Command))

    Proc = subprocess.Popen(Command, shell = True, stdout = subprocess.PIPE)

    Output = str()
    try:
        Output = Proc.communicate(timeout = 10)[0].decode("ascii")
    except subprocess.TimeoutExpired:
        if(args.debug):
        print("[DEBUG {}] Process timeout!".format(Index))

        Proc.kill()

        # Sometimes the application will stuck here. Use a timeout again to prevent it.
        try:
            Proc.communicate(timeout = 1)
        except subprocess.TimeoutExpired:
            pass

        # Timeout - jump back and try it again
        continue

    ...

The process is started and a timeout occurs:

[DEBUG 0] Run command ...\app\esptool.py --port COM14 --after hard_reset flash_id
[DEBUG 0] Process timeout!

The application should try the programming process again, but I get SerialException from the esptool.py.

serial.serialutil.SerialException: could not open port 'COM14': PermissionError(13, 'Zugriff verweigert', None, 5)

It looks like the process is still alive. But why? How can I fix it? I´m testing the application with Windows 10, but it also should run under Linux. I´m using Python 3.9.5 64-Bit.

Kampi
  • 1,798
  • 18
  • 26
  • 2
    `shell = True` is often an issue because kill doesn't kill the children. Try killing the children. Check this: https://stackoverflow.com/a/47549778/6451573 – Jean-François Fabre Dec 09 '21 at 14:43
  • You wrote: "*It looks like the process is still alive.*" Did you check with task manager if the process is still alive? Unrelated: `os.getcwd()` returns the current working directory. Do you really expect to always have `esptool.py` in the *current working directory*? Or is your intention that `esptool.py` resides in the same directory as your other script? – Bodo Dec 09 '21 at 14:46

0 Answers0