lol.py 881 B

12345678910111213141516171819202122232425262728293031
  1. def get_data():
  2. import serial
  3. import time
  4. # Configure serial port (adjust port name as needed)
  5. ser = serial.Serial('COM10', 9600)
  6. # Wait for the serial connection to establish
  7. ser.timeout = 2
  8. #ser.write(b'testcom.py\n') # Send command to start the script
  9. while True:
  10. # Read data from serial port
  11. data = ser.readline().decode().strip()
  12. # Check if data is not empty
  13. #print(data)
  14. if data:
  15. try:
  16. # Attempt to convert data to float
  17. value = float(data)
  18. timestamp = time.time()
  19. return timestamp, value
  20. except ValueError:
  21. print("Invalid data format:", data)
  22. else:
  23. print("Empty data received")
  24. # Add a small delay to prevent rapid looping
  25. time.sleep(0.001)