这个剧本怎么了?

时间:2017-08-12 19:14:08

标签: python rfid

我制作的RFID扫描仪如果扫描仪上有卡就会打开,如果没有卡则会关闭。

以下是代表问题的简化代码:

import RPi.GPIO as GPIO
import MFRC522
import signal
import os

MIFAREReader = MFRC522.MFRC522()

def on():
print 'Lights on'
os.system("php /home/pi/www/RLY4.php")

def off():
    print 'Lights off'
    os.system("php /home/pi/www/RLY4-.php")

while True:
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
    if(status == MIFAREReader.MI_OK):
        on()
    else:
        off()
只要存在卡,

status == MIFAREReader.MI_OK为True,因此应调用on()函数。

每当卡片没有出现时,灯光就会关闭,但是当卡片 出现时,灯光会在开启和关闭之间交替显示,而if和else语句都会被调用。

这个脚本发生了什么,为什么if和else语句都被调用?

1 个答案:

答案 0 :(得分:0)

没关系我现在使用两个while循环来计算它,每次扫描卡时都会在每个循环之间交替:

import RPi.GPIO as GPIO
import MFRC522
import signal
import datetime
import os
import time
import math
import colorama
from colorama import Fore, Style
colorama.init()

scan1 = True
scan2 = False
continue_reading = True

MIFAREReader = MFRC522.MFRC522()
GPIO.setwarnings(False)
print 'press CTRL+C to stop'

def on():
    print 'Lights on'
    os.system("php /home/pi/www/RLY4.php")

def off():
    print 'Lights off'
    os.system("php /home/pi/www/RLY4-.php")

def sayHi():
    print 'hi'

while True:
    while scan1:
        (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
        if status == MIFAREReader.MI_OK:
            print 'card detected at first scan, switching systems on.'
        (status,uid) = MIFAREReader.MFRC522_Anticoll()
        if status == MIFAREReader.MI_OK:
            UID = repr(str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
            UID0 = str(UID)
            print UID0
            on()
            scan1 = False
            scan2 = True

    while scan2:
        (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
        if status == MIFAREReader.MI_OK:
            print 'card detected at second scan, switching systems off.'
        (status,uid) = MIFAREReader.MFRC522_Anticoll()
        if status == MIFAREReader.MI_OK:
            UID = repr(str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))
            UID0 = str(UID)
            print UID0
            off()
            scan1 = True
            scan2 = False
        sayHi()

这可能是一种效率低下的方式,如果有人知道更好的方式,那么随意纠正我......