Created
March 17, 2024 09:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage to reset the serial port of a TLora-T3S3 board's serial port: