Python阅读Wiegand掉零 [英] Python reading Wiegand dropping Zeros

查看:195
本文介绍了Python阅读Wiegand掉零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我已经使用过的Raspberry Pi上的RFID wiegand阅读器的代码段.

Here's the code snippet from my RFID wiegand reader on my Raspberry Pi I use already.

def main():
    set_procname("Wiegand Reader")
    global bits
    global timeout
    GPIO.add_event_detect(D0, GPIO.FALLING, callback=one)
    GPIO.add_event_detect(D1, GPIO.FALLING, callback=zero)
    GPIO.add_event_detect(S1, GPIO.FALLING, callback=unlockDoor)
    while 1:
        if bits:
            timeout = timeout -1
            time.sleep(0.001)
            if len(bits) > 1 and timeout == 0:
                #print "Binary:", int(str(bits),2)
                c1 = int(str(bits),2)
                result = ((~c1) >> 1) & 0x0FFFFFF;
                checkAccess(result, doorID)
        else:
            time.sleep(0.001)



    if __name__ == '__main__':
        main()

在普通的USB RFID阅读器上,我得到0000119994,这就是卡上打印的内容.但是使用此代码,其读为119994.我尝试了多张卡.它总是将零放在前面.

On a normal USB RFID reader, I get 0000119994 and that's what's printed on the card. But with this code it reads 119994. I've tried multiple cards. It always drops the zeros at the front .

我什至尝试了一张零卡. 0000120368它显示120368 我以为它要取走前4个字符,但后来我尝试了一个在前面只有3个零的密钥卡. 0004876298,其读数为4876298.只删除前零.

I even tried a card with a zero in it. 0000120368 and it shows 120368 I thought it was taking off the first 4 characters but then I tried a key fob that only had 3 zeros in front. 0004876298 and it reads 4876298. Only dropping the front zeros.

推荐答案

如果前零位为零,Python将删除前几位,这也适用于整数.例如

Python will remove the front few bits if they are zero, this also applies to integers. For example

>>> a = 0003
>>> a
3
>>> b = 0b0011
>>> bin(b)
0b11

据我所见,所有RFID都有10个数字.您可以制作一个简单的程序,在其中添加这些数字并将其存储为字符串:

From what I see, all RFID's will have 10 numbers. You can make a simple program to add those numbers in and store the value as a string:

def rfid_formatter(value):
    str_value = str(value)
    for s in range(10 - len(str_value)):
        str_value = "0" + str_value
    return str_value

您的测试用例:

print rfid_formatter(120368)
print "0000120368"
print rfid_formatter(4876298)
print "0004876298"

这篇关于Python阅读Wiegand掉零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆