0

I'm extremely new to Python, I've been learning a bit over the last couple weeks. This is what I've managed to come up with. I'm pretty over the moon the code works most of the time but it doesnt work all the time.

I'm using 2 GPIO pins to read wiegand format from a reader to my Raspberry Pi 2.
However, it doesn't always scan correctly. Occasionally scanning will hang on storing a bit and I'll have to fob the reader a second time to get it to finish. Sometimes it finishes correctly. Sometimes it finishes in a card number that is completely wrong. Usually fobbing a third time gets a new set of information that is correct. I've messed around with the c versions and python versions of PIGPIO to try understand what I'm doing differently. I can't figure out why it always seems to return a correct set of values from the reader using PIGPIO code. I'm not knowledgeable enough to completely understand whats going on in the PIGPIO code so I'm unsure how to go about creating this consistency in my own code as I capture the information from the reader.

Does anything pop out immediately to anyone that indicates a fault in my logic for reading the information from the reader?

Remember, I'm super new so I need a little bit of hand holding with this. I'm looking to capture only 26 bit card format off an HID 26bit card reader. I have 3 different HID format fobs/cards and I've been using them to test. The issue happens utilizing the same fob over and over and if I cycle through them using one after the other. Sometimes it will scan fine for 10+ times, other times it'll fail immediately and not work for 2 or 3 scans.

from time import sleep
import os
import RPi.GPIO as GPIO
import pifacecad as p
GPIO.setmode(GPIO.BOARD)
#GPIO setup
data0=38
data1=40
GPIO.setup(data0,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(data1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
#Setting up display
cad = p.PiFaceCAD()
cad.lcd.backlight_on()
cad.lcd.clear()
cad.lcd.cursor_off()
cad.lcd.blink_off()
cad.lcd.write("Ready")
#Variables
i=0
dataBits=[]
bitCount=1
fcc="0"
card="0"
Zero="0"
One="1"
hexFilter=[]
hexCard=[]
hexFCC=[]
justRead=0
j=0
k=0
#Main code
while (1):
    #Reading code
    while (bitCount<27):   
        if GPIO.input(data0)==0:
            dataBits.append(Zero)
            print dataBits
            print "Bitcount %d" % bitCount
            bitCount = bitCount+1
            justRead=1
        if GPIO.input(data1)==0:
            dataBits.append(One)
            print dataBits
            print "Bitcount %d" % bitCount
            bitCount = bitCount+1
            justRead=1   
        if justRead ==1:
            if bitCount>26:
    #Transforming our information into usable bits  
    #Rest of the code just handles the information and prints to a screen. 
    #The issue existed before I wrote the rest of the code

I'm using a voltage divide circuit with resistors to drop the 5V to 3V and my data lines and Pi are all grounded together.

Read Hangs
Wiegand Hang Read is incorrect Wiegand Wrong

EDIT for those curious what the addition of the while/pass in the if statements for GPIO looks like. Also added some code with time() for scans that dont complete and hang. It isn't pretty but it works. When I'm less sick I'll bash my head against the keyboard some more and maybe make some progress that people can help with.

from time import time
current_time=time()

#Main code
while (1):
    #Reading code
    while (bitCount<27):   
        if GPIO.input(data0)==0:
            dataBits.append(Zero)
            bitCount = bitCount+1
            justRead=1
            while (GPIO.input(data0)==0):
                pass
        if GPIO.input(data1)==0:
            dataBits.append(One)
            bitCount = bitCount+1
            justRead=1  
            while (GPIO.input(data1)==0):
                pass
        if justRead==1:
            if time()-current_time <4:
                pass
            else:
                bitcount=1
                print("reset")
                justRead=0
                current_time = time()
                dataBits=[]
0

There are several problems with the code as written

  • There is no allowance for missing bits or starting half way through a Wiegand code
  • the same bit may be read multiple times, the software doesn't check for the bit to be finished before continuing the while loop (there is a gap between each bit where neither line is asserted, i.e. both are high).
  • Python, generally, on the Pi is not suitable for reliable timings at typical Wiegand transmission speeds. Even if you use a busy loop the process may be rescheduled at an inconvenient time. C is better but still suffers similar problems. (My) pigpio uses special techniques to get around these problems.

As a matter of interest how much more reliable is the code without the following statements?

        print dataBits
        print "Bitcount %d" % bitCount
  • Originally, I tried to use the test_wiegand c example along with some python code to do most of my scanning and processing of information. This is how I originally was doing things. It worked fine but I couldnt figure out how to change the pins that were being used. I have a PiFace control board thats occupying them. I'll give your suggestion a shot and get back to you, though. – Dregnox Feb 4 '16 at 9:32
  • Removing it caused things to break. I'm going to guess my print commands were causing enough of a delay that it was reading the bits properly most of the time? link – Dregnox Feb 4 '16 at 9:40
  • That could well be the case. try adding a while GPIO.input(data1)==0/pass construct after each if GPIO.input(data1)==0 instead to wait for the bit to finish, perhaps as the last part of the if blocks. – joan Feb 4 '16 at 10:01
  • I'm not quite sure what you mean by the /pass construct. – Dregnox Feb 9 '16 at 0:55
  • 1
    A while loop needs a statement in Python (I think). An indented pass is the required null statement. – joan Feb 9 '16 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.