【问题标题】:Exit program for fingerprint after 15 seconds of no input on fingerprintsensor PYTHON [duplicate]在指纹传感器 PYTHON 上没有输入 15 秒后退出指纹程序 [重复]
【发布时间】:2019-01-24 16:22:36
【问题描述】:

:) 我有一个我自己无法解决的问题(我用time.time() 尝试了很多,但我从来没有解决我的问题)。 如果 15 秒过去了,我希望我的程序退出并停止等待手指。如果有人知道如何在 python 中做到这一点,我会非常高兴和感激! 非常感谢你!有什么问题可以问我!代码在下面!

import sys
import os
sys.path.insert(0, '/home/pi/scripts')
import subprocess
import lcddriver
from time import *


lcd = lcddriver.lcd()
lcd.lcd_clear()

import hashlib
from pyfingerprint.pyfingerprint import PyFingerprint


## Tries to initialize the sensor
try:
f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000)

if ( f.verifyPassword() == False ):
    raise ValueError('The given fingerprint sensor password is wrong!')

except Exception as e:
lcd.lcd_display_string('Initialization failed!', 2)
print('Exception message: ' + str(e))
exit(1)

## Gets some sensor information
print('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ 
str(f.getStorageCapacity()))

## Tries to search the finger and calculate hash
try:
 f = PyFingerprint('/dev/ttyUSB0', 57600, 0xFFFFFFFF, 0x00000000)

if ( f.verifyPassword() == False ):
    raise ValueError('The given fingerprint sensor password is wrong!')

except Exception as e:
lcd.lcd_display_string('Initialization failed!', 2)
print('Exception message: ' + str(e))
exit(1)

## Gets some sensor information
print('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ 
str(f.getStorageCapacity()))

## Tries to search the finger and calculate hash
try:
lcd.lcd_display_string(' Waiting for finger', 2)

## Wait that finger is read
while ( f.readImage() == False ):
    pass

## Converts read image to characteristics and stores it in charbuffer 1
f.convertImage(0x01)

## Searchs template
result = f.searchTemplate()

positionNumber = result[0]
accuracyScore = result[1]
 if ( positionNumber == -1 ):

    os.system('python access_denied.py')
    exit(0)
 else:
    lcd.lcd_clear()
    lcd.lcd_display_string("  Finger accepted!", 2)
    sleep(1.5)
    lcd.lcd_clear()
    os.system('python keypad.py')

【问题讨论】:

  • 到目前为止你用time.time()做了什么
  • 我尝试在 while 循环开始时实现 'start = time.time()' 和 'end = time.time() ' - 开始循环询问 end - start

标签: python time fingerprint


【解决方案1】:

你可以试试这个:

timeout = time.time() + 15 # 15s from now
while True:
   # do stuff
    end_time = time.time()
    if end_time > timeout or f.readImage():
        break
    else:
        time.sleep(0.25) # sleep to reduce CPU usage

【讨论】:

  • 感谢您的帮助! “做事”是什么意思?
  • 啊,你可以在检查每个循环中的指纹之前做一些事情。如果你不需要它,请忽略它
  • 完美——有效!你是个了不起的人!
【解决方案2】:

您是否尝试过启动线程以保持时间计数并在设定时间后关闭程序?当读取指纹时,程序会在线程关闭程序之前关闭线程。 我个人在多线程方面没有太多经验,除了一般知识,所以我不完全知道如何应用这个,但这个显示可以帮助你https://www.tutorialspoint.com/python/python_multithreading.htm

【讨论】:

  • 感谢您的帮助!尽管我现在得到了解决方案,但我肯定会通过多线程阅读自己!:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多