Python 3.5 随着新数据的出现而变化的最大值

Max value that changes as new data comes in with Python 3.5

我是编码新手,这可能有一个简单的解决方案,但基本上我是在尝试记录来自连接到 Arduino 的传感器的传入数据。出于我的目的,我不仅要记录输入的数据,还要记录试验的最高值。

我的代码只生成 Max_Force = 0。我的最终目标是让我记录的所有值也保存最大值。然后最终将所有这些值链接到 GUI 以方便操作员使用。

import serial
import csv
import time
import numpy as np
import warnings
import serial
import serial.tools.list_ports

arduino_ports = [
    p.device
    for p in serial.tools.list_ports.comports()
    if 'Arduino' in p.description
]
if not arduino_ports:
    raise IOError("No Arduino found")
if len(arduino_ports) > 1:
    warnings.warn('Multiple Arduinos found - using the first')

Arduino = serial.Serial(arduino_ports[0])
Arduino.flush()
Arduino.reset_input_buffer()

start_time=time.time()
Distance = 0.5 # This is how long the lever arm is in feet


with open('DynoData.csv', 'w') as outfile:
    outfileWrite = csv.writer(outfile)
    while True:
        while (Arduino.inWaiting()==0):
            pass
        try:
            data = Arduino.readline()
            dataarray = data.decode().rstrip().split(',')
            Arduino.reset_input_buffer()
            Force = round(float(dataarray[0]),3)
            Max_Force = 0
            if Max_Force < Force:
                Max_Force == Force
            else:
                Max_Force == Max_Force

            RPM = round(float (dataarray[1]),3)
            Torque = round(Force * Distance,3)
            HorsePower = round(Torque * RPM / 5252,3)
            Run_Time = round(time.time()-start_time,3)
            print (Force ,",",Max_Force )
        except (KeyboardInterrupt, SystemExit,IndexError,ValueError):
            pass

        outfileWrite.writerow([Force,",",Max_Force,"lbs", RPM, "RPMs", Torque,"ft-lbs" ,HorsePower ,"HP" ,Run_Time ,"sec" ])

由于您正在分配一个变量,所以您只使用了 1 个等号。所以 Max_Force == Force 需要是 Max_Force = Force 并且这些行:

else:
    Max_Force == Max_Force #there should only be 1 equal sign btw

没有必要,因为没有理由将变量设置为自身。

您还需要将 Max_Force = 0 移动到 while True 循环上方,因为您经常将 Max_Value 设置回 0。