2016-08-05 74 views
0

我有一个项目为我校工作,读取身份证RFID RDM6300 这是我的代码连续型读取ID的RFID

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

+0

请修复您的代码的缩进。 –

+0

是的,谢谢修复它 –

+0

只有第一类是固定的。 –

回答

0

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