Flashing Wemos D1 Mini with ESPHome: Unexpected error: could not open port 'COM4': PermissionError(13, 'Access is denied.', None, 5)

I’m trying to flash my Wemos D1 mini with ESPHome to control my Garage door and I’m getting this error:

Unexpected error: could not open port ‘COM4’: PermissionError(13, ‘Access is denied.’, None, 5)

then followed by this error:

Unexpected error: ESP Chip Auto-Detection failed: Failed to connect to Espressif device: Timed out waiting for packet header

I’ve spent the last couple of hours on Google trying to figure out the fix by installing drivers etc, but nothing’s working. I’ve also tried flashing another Sonoff Basic that I had lying around and the same problem is happening, so I don’t think it’s the board.

I’m attempting this on a windows machine, using the flash tool and my code is the following

esphome:
  name: esphome_garage_door
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: 'xxx'
  password: 'xxx'
  fast_connect: on
  
api:

ota:

web_server:
  port: 80

logger:

binary_sensor:
- platform: gpio
  pin:
    number: GPIO13
    mode: INPUT_PULLUP
    inverted: False
  name: "Garage Door Sensor"
  device_class: garage_door
  filters:
    - delayed_on: 20ms

switch:
- platform: gpio
  id: relay
  pin:
    number: GPIO5
    inverted: False
  restore_mode: ALWAYS_OFF
- platform: template
  name: "Garage Door Switch"
  icon: "mdi:garage"
  turn_on_action:
  - switch.turn_on: relay
  - delay: 1s
  - switch.turn_off: relay
- platform: restart
  name: 'Garage Door REBOOT'


status_led:
  id: status_light
  pin:
    number: GPIO2

Has anyone overcome this?

Change usb cable.
Unplug, and plug back your Wemos D1 mini. Then, double-check that you’ve selected the right serial port in the Tools > Serial menu. Then, click the “ Connect/disconnect ” button to establish a serial communication.
This error might also mean that you have your serial port being used in another program (like a serial terminal or in the Arduino IDE).

Hi @Vlad, a lot of these things don’t apply to my view of the ESPHome flash tool (this is off Google images, I’m not at my machine right now):

If I take those bits away from your suggestion, it sounds as if you’re saying my USB UART connector could be defective? I’ve got this:
https://www.amazon.com.au/gp/aw/d/B072K3Z3TL?psc=1&ref=ppx_pop_mob_b_asin_title

I know this is an old topic, but I was able to get around this error on windows 10 (amd ryzen 5 chipset with asus mb) by simply running the x86 package instead of the x64.

Sorry if not an apporiate bump, but there really wasn’t an answer here.

Can confirm this, worked for me too.