在rfid扫描中出错标签

时间:2014-10-15 14:35:58

标签: python raspberry-pi nfc rfid

我在使用Python的Raspberry Pi上用RDM630扫描rfid标签时得到了奇怪的结果。 我不是专业人士,所以也许你可以轻易搞清楚我的错误。

标签被识别出来很好,但似乎我在那里有某种循环。 例如,如果我使用tag1和tag2交替,我会得到像tag1两次然后tag2两次。 中断触发次数是正确的,但它会随机显示tag1或tag2。似乎有某种缓冲区在新的中断触发器上给出了旧的id(但并非总是如此)。

谢谢&抱歉我的英文不好

import RPi.GPIO as GPIO
import time
import serial
import MySQLdb
import sys
import os
from thread import start_new_thread

GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
UART = serial.Serial("/dev/ttyAMA0", 9600)
UART.open()
success = 0


def cls():
    os.system(['clear', 'cls'][os.name == 'nt'])

cls()


def mysqlex(sqlquery):
    global success
    db = MySQLdb.connect(host="localhost", user="xxx", passwd="xxx", db="xxx")
    cur = db.cursor()
    try:
        cur.execute(sqlquery)
        db.commit()
        success = cur.fetchone()
        print success
    except:
        db.rollback()
        success = None
        print "no success"
    cur.close()
    db.close()


def led():
    GPIO.setup(18, GPIO.OUT)
    GPIO.setwarnings(False)
    GPIO.output(18, False)
    time.sleep(3)
    GPIO.output(18, True)


def interrupt(channel):
    start = "\x02"
    ende = "\x03"
    while True:
        ID = ""
        state = UART.read()
        if state == start:
            for Counter in range(13):
                zeichen = UART.read()
                ID += str(zeichen)
#            UART.flushInput()
            ID = ID.replace(start, "")
            ID = ID.replace(ende, "")
            print "ID -->", ID
            rfid = int(ID, 16)
            print "+********************************+"
            print rfid
            sql = "SELECT * FROM Rfid WHERE rfid_key = ('%s')" % rfid
            mysqlex(sql)
            if success is not None:
                start_new_thread(led, ())
                print "Zugang erlaubt                   +"
                print "+********************************+"
                print " "
                return
            else:
                print "Zugang verweigert                +"
                print "+********************************+"
                print " "
                return


GPIO.add_event_detect(24, GPIO.RISING, callback=interrupt, bouncetime=3000)


def main():
    while True:
        time.sleep(1)


if __name__ == '__main__':
    try:
        main()

    except KeyboardInterrupt:
        sys.exit()

    finally:
       GPIO.cleanup()

1 个答案:

答案 0 :(得分:0)

找到答案。

UART.flushInput()

if state == start:

它就像一个魅力。