在指纹传感器上没有输入15秒后退出指纹程序

2024-06-03 09:28:52 发布

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

:)我有一个我自己解决不了的问题(我用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')

Tags: toimportstringlcdtimeosdisplayexception
2条回答

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

你可以试试这个:

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

相关问题 更多 >