esptool.py resets ESP32 automatically by asserting DTR and RTS control lines of the USB to serial converter chip, i.e., FTDI, CP210x, or CH340x. The DTR and RTS control lines are in turn connected to GPIO0 and EN (CHIP_PU) pins of ESP32, thus changes in the voltage levels of DTR and RTS will boot the ESP32 into Firmware Download mode.
Note:When developing esptool.py, keep in mind DTR and RTS are active low signals, i.e., True = pin @ 0V, False = pin @ VCC.
# 第一步:确定串口名,分为两种情况 # 1)对于给定目标串口名(例如 /dev/ttyUSB0 or /dev/ttyACM0) # 2)没有指定目标串口名,需要在计算机上编译所有的的串口,然后指定一个可用串口名 # 第二步:打开串口 # 第三步:使用串口Read or Write 数据 # 第四步:关闭串口
import serial #导入模块 import time import threading
defopen_serial_port(port, baudrate=9600): """ 打开指定的串口。 :param port: 串口号,例如 'COM1' 或 '/dev/ttyUSB0' :param baudrate: 波特率,默认为 9600 :return: Serial 对象 """ try: ser = serial.Serial(port, baudrate, timeout=1) print(f"Open port {port} success") return ser except serial.SerialException as e: print(f"Open port {port} fail: {e}") returnNone
defread_data(ser): """ 从串口读取数据。 :param ser: Serial 对象 :return: 读取的数据 """ if ser isnotNoneand ser.is_open: # 读取一行数据 line = ser.readline() if line: print(f"Recv Data: {line.decode('utf-8').strip()}") else: print("Not Recv Data") else: print("serial not open")
defread_data_cb(ser, stop_event): """ 从串口读取数据的函数,运行在一个单独的线程中。 :param ser: Serial 对象 """ try: received_data = [] whilenot stop_event.is_set(): if ser.in_waiting > 0: line = ser.readline() if line: data = line.decode('utf-8').strip() received_data.append(data) print(f"Recv Data: {data}") time.sleep(0.01) # 避免CPU占用过高 except KeyboardInterrupt: print("Data reading stopped.")
defwrite_data(ser, data): """ 向串口发送数据。 :param ser: Serial 对象 :param data: 要发送的数据 """ if ser isnotNoneand ser.is_open: # 发送数据 ser.write(data.encode('utf-8')) print(f"Send Data: {data.strip()}") else: print("Serial not open")
defClassicReset(ser): """ Classic reset sequence, sets DTR and RTS lines sequentially. """ ser.dtr = False# IO0=HIGH ser.rts = True# EN=LOW, chip in reset time.sleep(0.1) ser.dtr = True# IO0=LOW ser.rts = False# EN=HIGH, chip out of reset # default time (0.05) to wait before releasing boot pin after reset time.sleep(0.05) ser.dtr = False# IO0=HIGH, done
defHardReset(ser): """ Reset sequence for hard resetting the chip. Can be used to reset out of the bootloader or to restart a running app. """ ser.dtr = False ser.rts = True# EN->LOW # Give the chip some time to come out of reset, # to be able to handle further DTR/RTS transitions time.sleep(0.2) ser.rts = False time.sleep(0.2)
defmain(): # 设置串口参数 port = '/dev/ttyUSB0'# 根据你的设备修改此值 baudrate = 115200
# 打开串口 ser = open_serial_port(port, baudrate)
if ser isnotNone: try: # 创建一个线程来读取串口数据 stop_event = threading.Event() read_thread = threading.Thread(target=read_data_cb, args=(ser, stop_event)) read_thread.start()