Skip to content

Instantly share code, notes, and snippets.

@jurriaan
Created March 17, 2024 09:35
Show Gist options
  • Save jurriaan/fec7d176cd4b27c15a534ac7014d337e to your computer and use it in GitHub Desktop.
Save jurriaan/fec7d176cd4b27c15a534ac7014d337e to your computer and use it in GitHub Desktop.
Reset serial port of ESP32 / Arduino devices. Can be used to reset a serial port before running esptool.
import serial
import time
import sys
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
print("Current serial ports:\n")
for port, desc, hwid in sorted(ports):
print("{}: {} [{}]".format(port, desc, hwid))
if len(sys.argv) > 1:
print("\nResetting...\n")
com = serial.Serial(sys.argv[1], 1200, dsrdtr=True)
com.dtr = True
com.write(b"\x00")
time.sleep(2)
# This part fails due to the device resetting, but the device is actually reset
try:
com.dtr = False
time.sleep(1)
com.close()
except OSError:
pass
ports_after = serial.tools.list_ports.comports()
print("Device's serial port should have been reset:\n")
for port, desc, hwid in set(ports_after) - set(ports):
print("{}: {} [{}]".format(port, desc, hwid))
print(
"\nUsually the reset device will get a new serial port, make sure to upload using that port."
)
else:
print("\nPlease pass the serial port to this script")
@jurriaan
Copy link
Author

Example usage to reset the serial port of a TLora-T3S3 board's serial port:

$ python reset_serial.py /dev/ttyACM0
Current serial ports:

/dev/ttyACM0: LilyGo TLora-T3S3-V1 - TinyUSB CDC [USB VID:PID=303A:1001 SER=AC5495DFA5A3 LOCATION=1-2:1.0]

Resetting...

Device's serial port should have been reset:

/dev/ttyACM1: USB JTAG/serial debug unit [USB VID:PID=303A:1001 SER=AC:54:95:DF:A5:A3 LOCATION=1-2:1.0]

Usually the reset device will get a new serial port, make sure to upload using that port.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment