变量达到len(ADCDATA)==100000后运行def main函数

2024-10-05 13:20:14 发布

您现在位置:Python中文网/ 问答频道 /正文

只有在使用键盘中断时,代码才会停止

虽然我将其取出并读取了adc数据,但使用了中断,然后spi.close与更新表一起被跳过(“PCEM SHT.1”,ADCDATA)

我尝试过使用不同的异常,根据ADCDATA中变量的数量引发异常,但没有效果

# import many libraries
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function  
from googleapiclient.discovery import build  
from httplib2 import Http  
from oauth2client import file, client, tools  
from oauth2client.service_account import ServiceAccountCredentials  
import datetime
import spidev # import the SPI driver


def read_mcp3002(channel):

### Working ADC Code

    return adc_data


def update_sheet(sheetname, my_list):  

###Working Update google sheet code
try:
    while True:
        Result=1##3.23#for 3302 [mV]..... Voltage=(High Voltage-Low Voltage)/(2^(# of conversion bits))
        voltage1=(read_mcp3002(0))
        voltage1k=voltage1*Result
        voltage2=(read_mcp3002(1))
        voltage2k=voltage2*Result  # This one #


        ADCDATA += [[voltage1k, voltage2k]]

        if len(ADCDATA) == 100000:
            #print(ADCDATA)
            ADCDATA = []
            print("+10000")


except KeyboardInterrupt: # Ctrl-C
    spi.close()


def main():  


    update_sheet("PCEM SHT.1", ADCDATA)


if __name__ == '__main__':  
    main()

所需的结果是在len(ADCDATA)=100000时自动停止:如果len(ADCDATA)==100000,它将运行if语句中的代码,并执行spi.close(),最后通过def main()的更新表(“PCEM SHT1”,ADCDATA)运行


Tags: fromimportspiclosereadifmaindef

热门问题