中断在python中读取的输入

时间:2019-03-02 17:27:59

标签: python input interrupt

我对python非常陌生。我正在做一个从加速度计读取数据并将其写入文件的项目。我这样做非常成功。我现在尝试使其每隔两分钟中断一次读取数据的input()命令,并写入一个新文件,并重复该过程。这将在车辆的研究设备中使用,因此脚本会在汽车启动时启动。我现在在下面发布一个示例脚本,它永远不会进入主循环(在输入之后)。我需要在一个时间窗口后中断此输入,并且想学习如何。我的代码如下。谢谢!

import sys
import time
import traceback
import serial
import datetime
import os
import datetime
import os.path

from Phidget22.Devices.Accelerometer import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
from pathlib import Path
from PhidgetHelperFunctions import *


os.chdir("C:/Users/Mohsine/OneDrive - UAB - The University of Alabama at 
Birmingham/Car project/accelometer/")

now = datetime.datetime.now()
print(now)

m = int(now.strftime("%M"))      

print(m)

def fileNamer():
looper = 1 
counter = 1 

while looper > 0:        
    fname =  'P1' + "S" + str(counter) + now.strftime("Acc Y%Y-M%m-D%d H%H- 
    M%M") +  '.txt'
    my_file = Path("C:/Users/Mohsine/OneDrive - UAB - The University of 
    Alabama at Birmingham/Car project/accelometer/" + fname)
    if my_file.is_file():
        counter = counter + 1            
    else:
        looper = 0
    return fname







def onAccelerationChangeHandler(self, acceleration, timestamp):



        D=acceleration[0], acceleration[1], acceleration[2]

        #print(acceleration[0])

        #print("                      -> Timestamp   : %f\n" % timestamp)


        x = str(datetime.datetime.now())
        #fname = now.strftime("ACCELOMETER test  ")
        f = open(fname,"a")
        f.write(x + " ")
        f.write("%s " % str(acceleration[0]))
        f.write("%s " % str(acceleration[1]))
        f.write("%s\n" % str(acceleration[2]))
        print(D)
        f.close()


fname = fileNamer()

ch = Accelerometer()

print(fname)
ch.setDeviceSerialNumber(415163)

ch.setOnAccelerationChangeHandler(onAccelerationChangeHandler)

ch.openWaitForAttachment(5000)

accelerometer = input('accelerometer : \n ')        
def main():
    now = datetime.datetime.now()
    k = m
    print(k)  
    if(int(now.strftime("%M")) == k+2 or int(now.strftime("%M")) == k-58):


        fname = fileNamer()

        ch = Accelerometer()

        print(fname)
        ch.setDeviceSerialNumber(415163)

        ch.setOnAccelerationChangeHandler(onAccelerationChangeHandler)

        ch.openWaitForAttachment(5000)


        accelerometer = input('accelerometer : \n ')
        k = k+2    

main()

2 个答案:

答案 0 :(得分:0)

我不太确定,因为我无法完全理解您的脚本,但也许是因为您在filenamer()之前调用main()并且有循环?

答案 1 :(得分:0)

强制性的“未经测试”,因为我当然没有实际的设备。

如果我正确理解,只要读取新值,设备就会触发onAccelerationChangeHandler()调用,并且如果自从第一次调用处理程序以来已经过了两分钟,您希望此处理程序将其写入新文件。

您可以尝试以下操作:

def onAccelerationChangeHandler(self, acceleration, timestamp):
    if 'file_creation_time' not in onAccelerationChangeHandler.__dict__:
        # save the time in seconds since epoch for first file creation
        onAccelerationChangeHandler.file_creation_time = time.time()
    if 'file_name' not in onAccelerationChangeHandler.__dict__:
        # get the first file name
        onAccelerationChangeHandler.file_name = fileNamer()
    now = time.time()
    if (now - onAccelerationChangeHandler.file_creation_time) > 120:
        # The file was created more than two minutes ago!
        onAccelerationChangeHandler.file_creation_time = now  # new creation time!
        onAccelerationChangeHandler.file_name = fileNamer()  # new file name!
    save_stuff_into_this_file(onAccelerationChangeHandler.file_name)

现在,我不太确定Python社区关于函数中的静态变量的位置。通常的方法是创建一个类,并将静态数据保留为实例的属性,但是由于此方法某种程度上与库对象实例挂钩,因此我不确定是否可以做到这一点。