Help with Python Code for Fume Hood

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)

scannedBadgeID = ‘’
hasScannedBadgeID = False
scannedBadgeApproved = False

fileLines = []
filename = “/home/pi/HoodUsers.txt”

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.

1 Like

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.

1 Like

You never said the precise nature of the trouble.

First of all, the if line needs to end with a colon like this:

        if(scannedBadgeID is userBadgeID):

Second, the is comparison tests whether you have the same instance of an object, not equality of value. I would try this:

        if(scannedBadgeID == userBadgeID):
1 Like

Nice catch Bill, my python-fu is rusty.

Both userScannedBadgeID and userBadgeID are variables of type ‘str’ with lengths of 12.

When printed out they both look identical:
userScannedBadgeID: 345247589DA6
userBadgeID: 345247589DA6

‘==’ yields False

I got it. When testing my code and printing out variable values I concatenated the "3: " to my userBadgeID… embarrassing

1 Like

I don’t see anything that is checking the checksum for data validation. Do you have an intent to validate the checksum?

1 Like

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.

2 Likes

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?

It happens for sure but if the application just dumps the data it should not be a problem.

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:

print("1: " + scannedBadgeID)
print("2: " + userBadgeID)
print("3: " + userBadgeID is scannedBadgeID)
print()

Instead, I would do something like this to bracket each variable in a bracket:

print("1: [" + scannedBadgeID + "]")
print("2: [" + userBadgeID + "]")
print("3: [" + userBadgeID is scannedBadgeID + "]")
print()

Thus, if the userBadgeID (for example) had inadvertently included a trailing newline, that debug print would generate:

2: [123456789
]

and one could easily spot the trailing newline.

My two cents: don’t spend them all in one place.

2 Likes