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
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=