Python Forum
Store variable data and display sum after 60 seconds - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Store variable data and display sum after 60 seconds (/thread-35787.html)

Pages: 1 2


Store variable data and display sum after 60 seconds - the_dude - Dec-14-2021

Hello, I'm currently reading and printing data from an arduino via serial USB in a loop and would like to store the data and add it up after 60 seconds has passed to display the result. The data being sent is in a float format (0.00) that represent seconds and milliseconds. I was looking at lists and different ways of storing the data but I'm not sure what the most efficient way of doing or where to start. Any and all help would be greatly appreciated!

import serial
import os

if __name__ == '__main__':
    ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
    ser.flush()

    while True:
        if ser.in_waiting > 0:
            value = ser.readline().decode('utf-8').rstrip() #reading from serial. It's a float 0.00 (second.milliseconds)
            runTime = float(value) #convert variable to float.
            print(runTime) #prints variable result.



RE: Store variable data and display sum after 60 seconds - menator01 - Dec-14-2021

I'm not familiar with using serial but, you may be able to do something like

mynums = []

while True:
    # Code .........
    mynums.append(runTime)

print(sum(mynums))



RE: Store variable data and display sum after 60 seconds - BashBedlam - Dec-14-2021

This will keep a running total for sixty seconds, print the total and then start the next sixty seconds with a zero total. Is that what you were after?
import serial, os, time
 
if __name__ == '__main__':
	ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
	ser.flush()

	while True :
		start_time = time () 
		total = 0
		while time () < start_time + 60 :
			if ser.in_waiting > 0:
				value = ser.readline().decode('utf-8').rstrip() #reading from serial. It's a float 0.00 (second.milliseconds)
				runTime = float(value) #convert variable to float.
				total += runTime
		print(runTime) #prints variable result.



RE: Store variable data and display sum after 60 seconds - the_dude - Dec-14-2021

Hi BashBedlam, I tried your code but it said

start_time = time ()
TypeError: 'module' object is not callable

I played around with it for a bit but couldn't figure out what was missing or wrong.

(Dec-14-2021, 10:15 PM)BashBedlam Wrote: This will keep a running total for sixty seconds, print the total and then start the next sixty seconds with a zero total. Is that what you were after?
import serial, os, time
 
if __name__ == '__main__':
	ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
	ser.flush()

	while True :
		start_time = time () 
		total = 0
		while time () < start_time + 60 :
			if ser.in_waiting > 0:
				value = ser.readline().decode('utf-8').rstrip() #reading from serial. It's a float 0.00 (second.milliseconds)
				runTime = float(value) #convert variable to float.
				total += runTime
		print(runTime) #prints variable result.



RE: Store variable data and display sum after 60 seconds - the_dude - Dec-14-2021

Hi @menator01 Your code has really helped me out. I can take readings until the 60 second mark but then it throws me this error-

Traceback (most recent call last):
File "****.py", line 19, in <module>
print(sum(mynums))
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Any idea what might be causing that? I've tried converting runtime to a float with all types of different formatting but that hasn't worked.

Updated code below:

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))
(Dec-14-2021, 08:43 PM)menator01 Wrote: I'm not familiar with using serial but, you may be able to do something like

mynums = []

while True:
    # Code .........
    mynums.append(runTime)

print(sum(mynums))



RE: Store variable data and display sum after 60 seconds - BashBedlam - Dec-15-2021

Sorry, it'sfrom time import time and thenstart_time = time () Blush


RE: Store variable data and display sum after 60 seconds - menator01 - Dec-15-2021

Have a look at line 16

runTime = (value) #convert variable to float.

you're missing the float(value)


RE: Store variable data and display sum after 60 seconds - the_dude - Dec-15-2021

@menator01 I've tried that multiple times in different ways. I've also done .strip and value.replace(" ","") and nothing is working. Here's the error I received from the float conversion you suggested-

0.38
Traceback (most recent call last):
File "***.py", line 16, in <module>
runTime = float(value) #convert variable to float.
ValueError: could not convert string to float:
>>>


This is what my list looks like if I were to just print it without trying to convert it to a float ['0.38', '0.42', '0.52', ", ", '', '', '', '', '']

(Dec-15-2021, 07:40 AM)menator01 Wrote: Have a look at line 16

runTime = (value) #convert variable to float.

you're missing the float(value)



RE: Store variable data and display sum after 60 seconds - bowlofred - Dec-15-2021

(Dec-15-2021, 03:37 PM)the_dude Wrote: This is what my list looks like if I were to just print it without trying to convert it to a float ['0.38', '0.42', '0.52', ", ", '', '', '', '', '']

Python's float function doesn't accept the empty string for conversion, and that's what you have for the later values in your list.

You should detect if you don't get any value from your serial and continue the loop or return a 0 since it won't affect your sum.


RE: Store variable data and display sum after 60 seconds - deanhystad - Dec-15-2021

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 list
Nobody 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)