连续读取ID rfid

时间:2016-08-05 03:35:37

标签: python raspberry-pi rfid

我有一个项目供我的学校工作,读取身份证到RFID RDM6300 这是我的代码

import serial

class Reader(object):
"""The RFID reader class. Reads cards and returns their id"""

def __init__(self, port_name, baudrate, string_length, timeout=1):
    """Constructor

    parameters:
    port_name : the device name of the serial port
    baudrate: baudrate to read at from the serial port
    string_length: the length of the string to read
    timeout: how to long to wait for data on the port
    """
    self.port = serial.Serial(port_name, baudrate=baudrate, timeout=timeout)
    self.string_length = string_length

def read(self):
    """Read from self.port"""
    rcv = self.port.read(self.string_length)

    if not rcv:
        return None

    try:
        # note : data from the RFID reader is in HEX. We'll return
        # as int.
        tag = { "raw" : rcv,
                "mfr" : int(rcv[1:5], 16),
                "id" : int(rcv[5:11], 16),
                "chk" : int(rcv[11:13], 16)}

        print "READ CARD : %s" % tag['id']

        return Card(tag)
    except:
        return None

class Card(object):

def __init__(self, tag):
    self.tag = tag

def get_id(self):
    """Return the id of the tag"""
    return self.tag['id']

def get_mfr(self):
    """Return the mfr of the tag"""
    return self.tag['mfr']

def get_chk(self):
    """Return the checksum of the tag"""
    return self.tag['chk']


def __repr__(self):
    return str(self.get_id())

def is_valid(self):
    """Uses the checksum to validate the RFID tag"""
    i2 = 0
    checksum = 0

    for i in range(0, 5):
        i2 = 2 * i
        checksum ^= int(self.tag.raw[i2 + 1:i2 + 3], 16)

    return checksum == tag['chk']

但结果是它一次又一次地继续读取完全相同的id Screenshot of the same id repeated

1 个答案:

答案 0 :(得分:1)

这个帖子真的很老了......我不知道你是否想过这个问题,但也许下一个偶然发现这个问题的人会觉得这很有用。您一遍又一遍地读取卡片的原因是只要卡片位于天线附近,RDM6300就会不断发送数据。没有延误。因此,您的串行缓冲区填满,您的程序一次从缓冲区读取一个条目...理想情况下,您应该从串行缓冲区读取一个条目,然后通过清除缓冲区丢弃其余条目...或者可能更好地读取一个条目,根据卡ID,为你想要采取的任何行动留出时间,然后在你的代码准备好进行另一次录入时清除缓冲区......或者在读卡之间的实际时间之后清除缓冲区。