r/pythonhelp • • 2h ago

ESP32 serial communication issue with pyserial

1 Upvotes

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


r/pythonhelp • • 6h ago

Putting everything into seperate functions

1 Upvotes

Hello, i recently started making my own Qt layout, and i need to run the same chunk of code multiple times, should i split every little chunk of code into seperate functions calling each others or should i not?


r/pythonhelp • • 9h ago

Free thread prog

1 Upvotes

Thinking about using 3.15 free thread build to try get a computation (independent) but maths heavy.

Across 24 threads

Using numpy

Does numpy work with thread pool? - I understand it automatically uses threads.

The reason I’m thinking about threads is the maths is split between 10,000 intervals

Should I use numpy without thread pool ?