pythonraspberry-pi2rfidwiegand

Python reading Wiegand dropping Zeros


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()

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 .

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.


Solution

  • 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
    

    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
    

    Your test cases:

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