Occasional write fail
Stephen,
The following code snippet works well generally. However once in a while it fails with the error thrown as shown in the second section below. A timing issue?
Code (part)
# This the tag reader scan loop.
while True:
# Scan the tag reader
with nfc.Contactless
def connected(tag): return False
tag = clf.connect(
if tag.ndef:
if tagcheck == 'XXX':
if tagcheck == 'OUT':
# Write the tag
tag = clf.connect(
# Now save the new tag data to file
# print (file_time + ' ' + file_name + ' ' + file_mob + ' ' + file_status+'\n')
# Light the correct LED
if tagstatus == ' IN':
Error thrown:
Enter your Selection [1-5]: 4
Now in Attendance Mode
USE THE ADMIN TAG TO STOP THE ATTENDANCE MODE
Traceback (most recent call last):
File "./tagapp3.py", line 162, in <module>
tag.
File "/usr/local/
self.
File "/usr/local/
self.
File "/usr/local/
super(
File "/usr/local/
self.
File "/usr/local/
self.
File "/usr/local/
self.
File "/usr/local/
raise Type3TagCommand
nfc.tag.
pi@raspberrypi:~ $
Question information
- Language:
- English Edit question
- Status:
- Answered
- For:
- nfcpy Edit question
- Assignee:
- No assignee Edit question
- Last query:
- 2017-01-11
- Last reply:
- 2017-01-12
ian@iwjohnston.com (ian-5) said : | #1 |
Some more info using a MiFare Desfire card on the same application as above. The occasional error thrown is somewhat different...
*** NEW INFO ***
The name of the new month?: Desfire
Month Desfire was created
-------
1. Check a Tag
2. Initialise a Tag
3. Set a new Month
4. Run the Attendance System
5. Exit
-------
Enter your Selection [1-5]: 4
Now in Attendance Mode
USE THE ADMIN TAG TO STOP THE ATTENDANCE MODE
Traceback (most recent call last):
File "./tagapp3.py", line 162, in <module>
tag.
AttributeError: 'NoneType' object has no attribute 'message'
pi@raspberrypi:~ $ ./tagapp3.py
*** ATTENDANCE SYSTEM ***
Password:
*** CURRENT MONTH IS SET TO default.csv UNLESS SET BY Menu No 3 ***
-------
1. Check a Tag
2. Initialise a Tag
3. Set a new Month
4. Run the Attendance System
5. Exit
-------
Enter your Selection [1-5]: 3
The name of the new month?: Desfire
Month Desfire was created
-------
1. Check a Tag
2. Initialise a Tag
3. Set a new Month
4. Run the Attendance System
5. Exit
-------
Enter your Selection [1-5]: 4
Now in Attendance Mode
USE THE ADMIN TAG TO STOP THE ATTENDANCE MODE
Traceback (most recent call last):
File "./tagapp3.py", line 162, in <module>
tag.
AttributeError: 'NoneType' object has no attribute 'message'
pi@raspberrypi:~ $
ian@iwjohnston.com (ian-5) said : | #2 |
Stephen,
I have provided my revamped program with added "clf.close()" commands as well as using nfcpy R 0.12. As you can see I am no professional Python person looking at the code structure. However using R 0.12 and the using the clf.close() statements, it has seemed to stabilise a fair bit. I noted your comments re the use of Raspberry Pi3 USB ports and how they were not always stable re write times. Hope this helps
Ian
Program Code
#!/usr/bin/env python
## This is a Python2 program to read NFC tags (cards) that are NDEF complient.
## It is an attendance system that simply tracks tag touches as either "IN' or 'OUT'.
## The actual reading and writing of/to the tags uses the nfcpy module
## from Stephen Tieldemann. It will check (read) tags, write the tags with a predetemined
## set of records which will then be stored to a text file in .csv format such that the
## info can be read into a spreadsheet. It is set so that the admin user can set a
## particular month to be stored. (If no month is selected, a file with the name
## 'default.csv' is used.
## The reading loop (Menu 4) will continue forever until broken out by an Admin
## tag with the status record set to 'XXX' (Record3)
## It has been tested using the SONY USB tag reader (RC-S380) with a Raspberry Pi3
## as the computer on Felica Lite-S tags (Tag Type3) as well as Mifare Desfire tags
## (Tag Type 4A)
## The logic is that each card is programmed with four text records:
##
## 1. The card user name
## 2. The card user mobile phone number
## 3 The card user status as either IN or OUT
## 4. A time stamp giving year-month-day as well as time in hours-min-seconds
##
## It will reject both blank NDEF cards as well as non NDEF cards (i.e. transport cards or similar)
## It also has a very simple password system that is hard coded into the program (needs to be
## much improved as a new feature)
import nfc
import nfc.ndef
import time
import sys, select, os
import getpass
import RPi.GPIO as GPIO
from datetime import datetime
GPIO.setmode(
GPIO.setwarning
GPIO.setup(5, GPIO.OUT) # Set up the GREEN led
GPIO.setup(6, GPIO.OUT) # Set up the RED led
GPIO.setup(13, GPIO.OUT) # Set up the In Operation led
GPIO.setup(17, GPIO.OUT) # Set up buzzer pin
## Ask for the Password
password = 'admin'
p = getpass.getpass()
if p.lower() == password:
_=os.
print " ATTENDANCE SYSTEM"
print
print
else:
print 'Sorry wrong password'
sys.exit()
## Get the menu posted
print '*** CURRENT MONTH IS SET TO default.csv UNLESS SET BY Menu No 3 ***\n'
print
print
print
global current_month
current_month = 'default'
global working_month
def print_menu():
print 30 * "-" , "MAIN MENU" , 30 * "-"
print "1. Check a Tag"
print "2. Initialise a Tag"
print "3. Set a new Month"
print "4. Run the Attendance System"
print "5. Exit"
print 67 * "-"
GPIO.output(13, False) # Turn off "In Operation" LED
loop=True
while loop: ## Stay in the Menu loop until loop = False
GPIO.output(13, False)
print_menu() ## Display menu
choice = input("Enter your Selection [1-5]: ")
if choice==1:
with nfc.Contactless
print
print "Place a tag on the reader"
def connected(tag): return False
tag = clf.connect(
print "Tag information:"
print
elif choice==2:
print "Tag Creation App"
print
tagname = raw_input("Name of Employee: ")
tagmob = raw_input("Mobile Number: ")
print
print "Default new tag status is OUT"
tagstatus = "OUT" #Tag will be initialsed as "OUT"
tagtime = str(datetime.now())
tagtime1 = (tagtime[0:19])
record1 = nfc.ndef.
record2 = nfc.ndef.
record3 = nfc.ndef.
record4 = nfc.ndef.
print "Place a tag on the reader"
with nfc.Contactless
def connected(tag): return False
tag = clf.connect(
print
print "Tag written with the following information:"
print
elif choice==3:
print
#global current_month
#global working_month
print
print "Month " + current_month + " was created"
print
elif choice==4:
print 'Now in Attendance Mode'
print
print 'USE THE ADMIN TAG TO STOP THE ATTENDANCE MODE'
print
# This is the tag reader scan loop. Will run forever until ended by 'XXX' in the status record (record3)
# i.e. The Admin card
while True:
# Scan the tag reader
with nfc.Contactless
def connected(tag): return False
tag = clf.connect(
if tag.ndef:
if tagcheck == 'XXX':
if tagcheck == 'OUT':
# Write the tag
# Light the correct LED
if tagstatus == ' IN':
# Now save the new tag data to file
# print (file_time + ' ' + file_name + ' ' + file_mob + ' ' + file_status+'\n')
elif choice==5:
print "Program quit"
loop=False #Will force end of the while loop
else:
print
print
In your first version you've done a new search for tags right before writing the new NDEF message (now disabled in your last version). When you search for a tag there's again no guarantee that NDEF is discovered, this can indeed happen if communication is unstable. It is always good to consider that an NFC communication can be interrupted at any time by getting out of range. I'd recommend to use tag.is_present before writing (https:/
As another note, you are creating invalid NDEF Text Records (an 'urn:nfc:wkt:T' record has a language and content field in the payload). You should either use the TextRecord class or the 'text/plain' mime type or even use your own record type identifier (if you own a domain name like I do with 'nfcpy.org' it could be like 'urn:nfc:
As yet another note, if your script is to stay for longer you should be aware that the nfc.ndef package is going away at some future time. It will be replaced by the https:/
As a last note, you don't need to use clf.close() when using the 'with ... as clf:' statement. The close method is called automatically when execution leaves the 'with' context.
Can you help with this problem?
Provide an answer of your own, or ask ian@iwjohnston.com for more information if necessary.