我正在尝试制作 RFID 门锁。目标是扫描 RFID 卡以检查 .txt 文件。如果卡片字符串在文件中,则接受,如果卡片被接受,则播放视频,然后发送解锁信号。
正在发生的事情是,无论是否加载,每张卡都被接受。我不知道为什么会这样......如果有人可以帮忙,我附上我的代码!
这是我的代码:
#!/usr/bin/env python3
from evdev import InputDevice
from select import select
import RPi.GPIO as GPIO
import time
from time import sleep
import subprocess
from subprocess import run
import datetime
import os
import sys
import re
import pygame
from pygame.locals import *
def main():
pygame.init()
windowSurface = pygame.display.set_mode ((1024, 600), pygame.NOFRAME)
img = pygame.image.load("/home/pi/Accepted/New Phone Wallpaper copy.jpg")
pygame.mouse.set_cursor((8,8),(0,0), (0,0,0,0,0,0,0,0), (0,0,0,0,0,0,0,0))
windowSurface.blit(img, (0,0))
pygame.display.flip()
pygame.event.clear()
def check_if_string_in_file(file_name, string_to_search):
"""Check if any line in the file contains given string"""
with open("/home/pi/Accepted/Loaded_Cards.txt",'r') as read_obj:
for line in read_obj:
if string_to_search in line:
return True
return False
rfid_presented = ""
e = rfid_presented;
keys = "X^1234567890asdfghjklXXXXXycbnmXXXXXXXXXXXXXXXXXXXXXXX"
dev = InputDevice('/dev/input/event0')
while True:
r,w,x = select([dev], [],[])
for event in dev.read():
if event.type==1 and event.value==1:
if event.code==28:
if check_if_string_in_file('/home/pi/Accepted/Loaded_Cards.txt', e):
videoPath = "myMovieFile.mp4"
omx = run(["omxplayer", '--win', '1,1,1024,600', videoPath])
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26, GPIO.OUT, initial=GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
time.sleep(3)
GPIO.output(26, GPIO.LOW)
else:
rfid_presented = ""
else:
rfid_presented += keys[event.code]
main()