我有一个我自己无法解决的问题(我尝试了很多,time.time()但我从来没有解决我的问题)。如果 15 秒过去了,我希望我的程序退出并停止等待手指。如果有人知道如何在 python 中做到这一点,我会非常高兴和感激!非常感谢!问我是否有任何问题!代码在下面!import sysimport ossys.path.insert(0, '/home/pi/scripts')import subprocessimport lcddriverfrom time import *lcd = lcddriver.lcd()lcd.lcd_clear()import hashlibfrom pyfingerprint.pyfingerprint import PyFingerprint## Tries to initialize the sensortry: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 informationprint('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ str(f.getStorageCapacity()))## Tries to search the finger and calculate hashtry: 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 informationprint('Currently used templates: ' + str(f.getTemplateCount()) +'/'+ str(f.getStorageCapacity()))## Tries to search the finger and calculate hashtry:lcd.lcd_display_string(' Waiting for finger', 2)## Wait that finger is readwhile ( f.readImage() == False ): pass## Converts read image to characteristics and stores it in charbuffer 1f.convertImage(0x01)## Searchs templateresult = 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')
2 回答

GCT1015
TA贡献1827条经验 获得超4个赞
你可以试试这个:
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
添加回答
举报
0/150
提交
取消