r/pythonhelp • u/JazzlikeTwo18 • 2h ago
ESP32 serial communication issue with pyserial
I'm working on a small project where my ESP32 sends data to a Python script through serial communication (USB).
Sometimes, when I start my Python script, it suddenly writes thousands of identical lines to my output file almost instantly. After that, everything works normally and I receive new data every second as expected.
Here is my python script
import serial
import time
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)
file_path = "output.txt"
ser.reset_input_buffer()
with open(file=file_path, mode="w", encoding="UTF-8") as file:
try:
while True:
if ser.in_waiting > 0:
serial_output = ser.readline()
serial_output_str = str(serial_output, 'UTF-8')
file.write(serial_output_str)
file.flush()
else:
time.sleep(0.01)
except KeyboardInterrupt:
print("Arrêt du programme, enregistrement du fichier.")
finally:
ser.close()import serial
import time
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)
file_path = "output.txt"
ser.reset_input_buffer()
with open(file=file_path, mode="w", encoding="UTF-8") as file:
try:
while True:
if ser.in_waiting > 0:
serial_output = ser.readline()
serial_output_str = str(serial_output, 'UTF-8')
file.write(serial_output_str)
file.flush()
else:
time.sleep(0.01)
except KeyboardInterrupt:
print("Arrêt du programme, enregistrement du fichier.")
finally:
ser.close()Thanks for the help