Close serial terminal when uploading in VS Code

It would be awesome if the serial terminal were automatically closed when uploading a build. Currently my process looks something like:

  • watch some output on the serial monitor
  • make code changes
  • build
  • upload
  • upload fails, so I remember I left the serial monitor open
  • hunt for the serial monitor window to close it
  • re-upload

:slight_smile: It would be awesome if the middle steps could be skipped.

4 Likes

If you open serial monitor via Serial Monitor button on the bottom toolbar, PIO IDE should close it automatically.
Nevertheless, we depend on VSCode API from Microsoft

This doesn’t seem to work for me – I still have to manually close the serial monitor in VS Code when I upload new code.

2 Likes

If you use buttons on PIO Toolbar, all should work. I’ve just pressed “monitor” button, then “upload” and VSCode asked me to terminate existing task. This is a temporary workaround while VSCode does not have proper API to terminate particular task.

Hi all,
New to this tool. I too am struggling with what I surmise to be a monitor serial connection blocking uploads to the device.

error: Failed to open COM7
error: espcomm_open failed
error: espcomm_upload_mem failed
*** [upload] Error 2

The behaviour is very unpredictable and slowing me down quite a bit! My process is write a bit of code. Build (tick icon). Upload (arrow icon). Monitor (plug icon). But if I repeat this, I get the error above… Sometimes… 1 out of 4 times it succeeds. Success through repeated clicking; not changing anything. Is there a secret process running inside VS Code that rolls a dice and perhaps another few processes laughing at me?

I waited a couple of hours before posting this. But it still persists. And I now also have the following error, which fails every time:

espcomm_sync failed

Any chance this is a known error, and a solution is possible. Would love to continue working in this environment all the way to production, but I’m currently feeling very anxious!

Notes:

  • Made sure no tasks running by checking the little spanner&screwdriver icon.
  • Slowed the baud down to 9600.
  • Checked with a different cable.
  • Checked with a different USB port (windows actually still set it as COM7)
  • Opened port OK in teraTerm just to check it wasn’t in use. It wasn’t!
  • My design puts the ESP8266 into Deep Sleep. Interestingly, I was getting intermittent failures when I was sleeping for 5 or 10 seconds. Now I’ve upped that to 60 seconds, I can’t seem to get it to work hardly at all. Can anyone confirm whether the device being in deep sleep has anything to do with this?

Anyone, anyone??? …

We can’t closed an opened port from another application. Could you close all tools that use serial ports and start uploading? Does it work now?

I was careful to close the Teraterm port after using it. I only opened it to test whether there was another unknown application holding it, which there was not. I could connect over teraterm fine. Then closed it. And back to not connecting.

I know this is an old topic, and perharps someone already has another solution for it, but i believe this might help someone in the future.

I managed to workaround the issue by installing multi-command VS Code extension and creating a Keyboard Shortcut to a command sequence.

Here is the step by step.

  1. Install multi-command VS Code extension
  2. Go to the extension settings and click on:

Multi Command: Commands
command sequence list.
Edit in settings.json

  1. Paste the following code in your settings.json file:

“multiCommand.commands”: [
{
“command”: “multiCommand.closeTerminalAndUpload”,
“interval”: 50,
“sequence”: [
“workbench.action.terminal.kill”,
“platformio-ide.upload”
]
}
],

  1. Open the Keyboard Shortcuts editor by goung to File > Preferences > Keyboard Shortcuts
  2. Search for multiCommand.closeTerminalAndUpload
  3. Assign a Keybinding to it
    • Make sure the chosen Keybinding is not already being used by something else
    • OR, remove that Keybinding from another command before assigning it
  4. OPTIONAL: You can enable Platformio-ide: Force Upload And Monitor in VS Code settings, this will “Force the platformio-ide-upload command to use the ‘Upload and Monitor’ task”. This will open the Monitor everytime you Upload your code (Both from GUI and Keys Shortcut)
  5. DONE!

Now, everytime you press the chosen Keys Shortcut, the current terminal will close, wait 50 milliseconds and upload the new code.

NOTE: This only works for the chosen Keys Shortcut, this won’t work if you try to upload the code by clicking the upload buttons on the GUI

2 Likes

I have the opposite problem…

I’m using ArduinoOTA to upload firmware, and I’m annoyed that pressing the upload button automatically closes my serial monitor. (Which of course would make sense if I where uploading the firmware via serial connection, but it should check that’s the case before closing the serial monitor)

More concrete feature request: Please only close the serial monitor before uploading when platformio.ini doesn’t contain the line:

upload_protocol = espota

Blockquote “workbench.action.terminal.kill”,

Thanks for this, this helped me. It is all I needed to extend the command needed for VSCode PlatformIO Core v6.1.9 without installing any other extensions.

Base on my version I do not see any options for “Force Upload And Monitor” option, but only for the “Auto Close Serial Monitor” which is ticked by default. Unfortunately, it wouldn’t close the Serial Monitor when I use the “Upload and Monitor” action.

To extend the VSCode PlatformIO Toolbar, do the following:

  1. Go to your VSCode settings.
  2. Find the “Platformio-id: toolbar”.
  3. Click the “Edit in settings.json”. This will generate a setting for your vscode named “platformio-ide.toolbar”, this settings are also shown in your VSCode bottom bar (left side).
  4. Inside of the array, add this object data for the additional setting in your toolbar.
...
    {
      "text": "$(arrow-up)",
      "tooltip": "PlatformIO: Upload and Monitor",
      "commands": [
        "workbench.action.terminal.kill",
        "platformio-ide.uploadAndMonitor"
      ]
    }
...
  1. You can change the name and the text any way you want, then save.