【发布时间】:2021-05-06 02:47:02
【问题描述】:
我有一个在无限循环上运行的脚本,从 Arduino 收集数据并将这些信息添加到 json 文件中。当我用 cnt c 停止程序时,它会切断 while 循环,因此没有正确完成将数据发送到 json 文件。
我查看了其他堆栈溢出问题。就像我曾经在我的代码中实现的 "[How to stop an infinite loop safely in Python?"][1] 一样,但它有时会正确终止 json 数据。
我的 while 循环看起来像这样:
interrupted = False
while True:
serialDataDic= ArduinoSerial(arduinoSerial,68,startTimeofData)# gather x amount of samples of data at 8.9kHz
# print(serialDataDic)
decompositionCoeffiecients=DWT(serialDataDic['voltage'],'haar',2)
# print(decompositionCoeffiecients)
#looping through each of our functions and placing that info in our dictionary to go to our json file
for key in serialDataDic:
plottingDicData[key].append(serialDataDic[key])
for key in decompositionCoeffiecients:
plottingDicData[key].append(decompositionCoeffiecients[key])
# print(plottingDicData)
#sending out sensor data into a json file
jsonfileData=JsonData(fileName,plottingDicData)
if interrupted:
print("Gotta go")
break
如何正确终止此 while 循环,以便在终止前完成 json 数据收集?
【问题讨论】: