Dec-15-2021, 08:51 PM
(This post was last modified: Dec-15-2021, 08:51 PM by deanhystad.)
This code doesn't make any sense.
import serial import os import time mynums = [] if __name__ == '__main__': ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1) ser.flush() while True: if ser.in_waiting > 0: t_end = time.time() + 10 while time.time() < t_end: value = ser.readline().decode('utf-8').rstrip() #reading from serial. It's a float 0.00 (second.milliseconds) runTime = (value) #convert variable to float. print(value) #prints variable result. mynums.append(runTime) print(sum(mynums))Translating to Not Python
Repeat this If there is something in my serial port repeat this for 10 seconds. read the serial port. Even if the read fails append the resuts to a listNobody has the code quite right yet. You should not be using ser.in_waiting() with ser.readline(). You can have bytes in the buffer but still be waiting for the newline character. Instead of peeking in the buffer just do the readline() with a timeout and check the length of the returned value.
import serial import time ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1) ser.flush() end_time = time.time() + 60 total = 0 while time.time() < end_time: value = ser.readline() if len(value) > 0: # len() will be zero if readline() timed out print(value) total += float(value.decode()) print("Total", total)