Tag object reading

Asked by Matthew Crown on 2015-09-16

Hello all,

I have used the sample code within getting started to try and creating a program which can retrieve the unique id of a type 2 tag to be stored within a database along with a username. However, when I attempt to remove the the "Type 2 Tag=" part of the returned data from the tag function, I am unable to iterate over this as the object is not iterable. I can however force this data to be a string and then parse the result. My question is this: is there a way to retrieve only the id attribute itself, or is it a better idea to force the result to be a string and then use this within my solution?

Thanks,

Matt

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Tiedemann
Solved:
2015-09-19
Last query:
2015-09-19
Last reply:
2015-09-18

I'm not sure how you attempted to get the ID but here's a code snippet that print's the tag identifier, and then the tag object representation and the formatted string.

>>> import nfc
>>> clf = nfc.ContactlessFrontend("usb")
>>> tag = clf.connect(rdwr={'on-connect': lambda tag: False})
>>> tag.identifier
'\x04\x94\xff\xc2\xd78\x80'
>>> repr(tag)
'<nfc.tag.tt2_nxp.NTAG216 object at 0x7fec4ac09e50>'
>>> str(tag)
"Type2Tag 'NXP NTAG216' ID=0494FFC2D73880"

Matthew Crown (matthewcrown) said : #2

Thanks for the reply. Still just need a little more clarification:

When I use

>>> print(repr(tag))
>>>print(str(tag))

this works fine but when I run

>>> print(tag.identifier)

this doesn't appear to give an output - it does give me some sort of issue with regards the character coding though which I'm unsure about. Can you tell me will tag.identifier work for all cards or only some?

The tag.identifier is a raw byte string. If you want to print use a hexadecimal encoding:
>>> print tag.identifier.encode("hex")

The tag.identifier works for all cards.

Matthew Crown (matthewcrown) said : #5

Thanks Stephen Tiedemann, that solved my question.