I’m snagged on a little bug with Py code for RasPi3. Two strings (one from RFID badge and the other read in from a .txt file) should be equal and evaluate to True when compared. I’ve tried ==, is, and trimmed any white space. Below is the code with the troublemaker line marked with a comment above it.
Apologies for the copy and paste job… can’t figure out how to paste it properly here
import serial
serial = serial.Serial("/dev/ttyUSB0", baudrate=9600)
def populateDataBase():
try:
with open(filename,‘r’) as myFile:
for line in myFile:
fileLines.append(line)
except IOError:
print(“Error: File does not appear to exist.”)
return
while True:
while not hasScannedBadgeID:
data = serial.read()
if "x03" in str(data):
hasScannedBadgeID = True
scannedBadgeID = scannedBadgeID.replace("'","").replace("b","").replace("\\x02","").replace("\\r\\n","").strip()
else:
scannedBadgeID = scannedBadgeID + str(data)
populateDataBase()
print(scannedBadgeID)
for line in fileLines:
lineSplit = line.split("|")
userBadgeID = lineSplit[1].strip()
print("1: " + scannedBadgeID)
print("2: " + userBadgeID)
print("3: " + userBadgeID is scannedBadgeID)
print()
##########Line Below is where error is##########
if(scannedBadgeID is userBadgeID)
print(userName)
userName = lineSplit[0]
userStatus = lineSplit[2]
userActiveStatus = lineSplit[3]
scannedBadgeApproved = True
break
else:
userBadgeID = ""
cont = input("Carry On?")
hasScannedBadgeID = False
scannedBadgeID = ""
fileLines = []
Does the scanned badge come in with unprintable chars like carriage return and linefeed? may try debug printing the length of the strings, sorry only thing I can think of.
Does the badge data contain any alpha chars? Or all numeric? If the former, you might consider ensuring all data is same case (lower() or upper()).
Karl’s got the right idea regarding debugging, though. Also, try testing with a test HoodUsers.txt file, maybe a single line file, that you KNOW should give you known results, in this case a match to the swiped badge.
I see from the syntax of your code that you are using Python version 3. If you plan to use a lot of downloadable Python code that is out there, just beware that most of it is written using Python version 2. Just a heads up here.
Presumably that’s taken care of by the database, so you aren’t going to be able to accept an ID that is bad because it doesn’t appear in the database – but it would be nice to do a checksum check with an appropriate warning message when loading the IDs out of the .txt file!
Que…? Uhhh… general plan is to have anybody listed as an admin in the database (a super fancy .txt file i managed to create with a single right mouse button click) swipe their badge activating a one second period where recently certified users can swipe their badge and be added to the database. Are there common issues with bad data transmission from rfid readouts?
Sounds like you have the problem solved, but I would recommend the following to help future debugging. Oftentimes, empty strings, leading/trailing spaces, newlines, etc. can confuse text comparisons. I usually bracket the printed variable with a known character so that one can see the existence of these other things. Example: in your code, you have the following debug print statements: