Python Forum
Store variable data and display sum after 60 seconds
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store variable data and display sum after 60 seconds
#11
Hi @deanhystad, I was able to tweak some code and get the below to work, in a manual button press environment that is. It runs for 10 seconds and reads the serial data sent within each second, adds it to a list, and then adds it all up when 10 seconds has passed.

Once I uploaded it to the machine it's monitoring, in my case a laser engraving machine, I quickly discovered the laser fires so rapidly that the serial can send multiple messages (or sets of data) within that second which ultimately tells me this script won't work, like you said.

I would rather have the serial data be the trigger to save it to a list and at the end of 10 seconds add them all up. Can you help me with that? I know it comes across as asking for you to write it for me but I just need some help. Thanks in advance!

0.02799999950.04899999610.03500000000.0250000000
0.0000000000
0.0000000000
0.0000000000
0.90399999610.02700000040.05000000000.03400000330.0250000000
0.0000000000
starting to calculate...
['0.02799999950.04899999610.03500000000.0250000000', '0.0000000000', '0.0000000000', '0.0000000000', '0.90399999610.02700000040.05000000000.03400000330.0250000000', '0.0000000000']
[color=#E74C3C]Traceback (most recent call last):
  File "****.py", line 28, in <module>
    mynums_floated = [float(v) for v in mynums]
  File "****.py", line 28, in <listcomp>
    mynums_floated = [float(v) for v in mynums]
ValueError: could not convert string to float: '0.02799999950.04899999610.03500000000.0250000000'[/color]
>>> 


Working code (sort of):

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:
            mynums = []
            t_end = time.time() + 10
            while time.time() < t_end:
                value = ser.readline().decode('utf-8').rstrip() 
                runTime = (value) 
                runTime2 = ("0.0000000000")

                if runTime == "":
                    mynums.append(runTime2)
                    print(runTime2)
                if runTime != "":
                    mynums.append(runTime)
                    print(runTime)
            print("starting to calculate...")
            print(mynums)
            mynums_floated = [float(v) for v in mynums]
            print(sum(mynums_floated))
            print("Calculation Done!")
(Dec-15-2021, 05:51 PM)deanhystad Wrote: 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 wait 10 seconds before I eventually read it and append 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)
Reply
#12
This is not a speed thing. Your laser engraving machine is not sending individual values separated by newlines. It appears to send 4 values at a time, and it doesn't look like there are any delimiters between the values. That is rather rude.

I would start by reading 1 value and seeing what is sent. Always start solving a problem by understanding what the problem really is. Does the engraver send 4 numbers at a time or is there something happening in the program that concatenates the strings (I don't see how).
import serial
 
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.flush()

while True:
    value = ser.readline()
    if len(value) > 0:
        print('Bytes', value)
        print('String', value.decode())
        break
Try running this and posting what is printed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 305 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  How to display <IPython.core.display.HTML object>? pythopen 3 46,005 May-06-2023, 08:14 AM
Last Post: pramod08728
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,913 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Need help on how to include single quotes on data of variable string hani_hms 5 2,054 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  Problem with module time and leap seconds Pedroski55 3 1,258 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  USE string data as a variable NAME rokorps 1 969 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  store all variable values into list and insert to sql_summary table mg24 3 1,164 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Regex text file to store data in list TheSithSiggi 1 1,538 Dec-03-2020, 04:46 PM
Last Post: bowlofred
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,248 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  How to calculate time difference between each row of dataframe in seconds Mekala 1 2,582 Jul-16-2020, 12:57 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020