我正在测量一个系统的一个相位的功率值,通过串行rtu modbus通信从一个电表。我正在尝试编写一个代码,允许我监视某个值内的功率值,例如+- 5%。如果瞬时读取的功率值保持在这个值的范围内() 15分钟,那么我就把它作为有效的数据,否则如果在15分钟结束之前,这个值离开这个范围,我必须将计时器重置为15。如果您知道任何监视变量值的库,它将对我非常有用。谢谢
发布于 2020-02-08 09:33:55
你不需要任何外部库。
取决于您是否只想在脚本中运行它,或者如果您有更多需要运行的东西,您可以选择将其设置为async。试着做这样的事情:
import datetime
import asyncio
def resetTimer(duration: float):
now = datetime.datetime.now()
expire = now + datetime.timedelta(0,0,0,0, duration)
return expire
async def monitor(durationMinutes: float, variableToCheck: float) -> None:
lowerThreshhold = variableToCheck * 0.95
upperThreshhold = variableToCheck * 1.05
expire = resetTimer(durationMinutes)
while datetime.datetime.now() >= expire:
if variableToCheck >= upperThreshhold or variableToCheck <= lowerThreshhold:
expire = resetTimer(durationMinutes) # resets timer
else:
# do something else if value stays within range the entire duration用法:
task1 = asyncio.create_task(
monitor(15, myVariable)
)
await task1https://stackoverflow.com/questions/60125428
复制相似问题