Ich verzweifel gerade an einer Schleife in Python, leider kriege ich da ein total unlogisches Verhalten.
Der Code ist:
magicBoxActive = True
idFile = '/ram/cardID'
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
try:
global cardID
cardID = ''
while magicBoxActive :
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If no card is found
print 'Wanted: ', MIFAREReader.MI_OK, ' Found: ', status
if status != MIFAREReader.MI_OK:
print 'No Card found!'
cardID = ''
if os.path.isfile(idFile):
os.remove(idFile)
continue
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status != MIFAREReader.MI_OK:
print 'Card not readable!'
continue
foundID = str(uid[0])+'-'+str(uid[1])+'-'+str(uid[2])+'-'+str(uid[3])
if cardID == foundID:
continue
print 'Card found: ', foundID, ' old Card: ', cardID
cardID = foundID
f = open(idFile, 'w')
f.write(cardID)
f.close()
Alles anzeigen
Dabei funktioniert erstens diese Stelle nicht:
Der Enumerator ist genau das, was auf diese Variable drauf geschrieben werden soll, und die Ausgabe an der Stelle lautet auch ganz klar:
TROTZDEM geht der Code in die If Bedingung rein! Versteh ich nicht.
Der zweite Punkt ist, das in dieser If Bedingung am Ende ein continue steht, das wird aber anscheinend total ignoriert, denn der Code läuft hier einfach weiter, was im Folgenden dann zu Fehlern führt.
Hat jemand eine Idee wie das zu erklären ist?
Bin da mit meinem Latein am Ende, wenn einfachste Vergleiche einfach nicht funktionieren....
Was mich jetzt total verwirrt ist das es "von Hand" klappt:
sudo python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> import MFRC522
>>> MIFAREReader = MFRC522.MFRC522()
>>> (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
>>> status != MIFAREReader.MI_OK
False
>>> status == MIFAREReader.MI_OK
True
>>>
Alles anzeigen