-
Notifications
You must be signed in to change notification settings - Fork 0
/
NFC_Module.py
33 lines (25 loc) · 1.21 KB
/
NFC_Module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from smartcard.System import readers
import smartcard.util
from smartcard.CardRequest import CardRequest
from smartcard.CardType import ATRCardType
from smartcard.CardType import AnyCardType
from smartcard.Exceptions import CardConnectionException
from smartcard.util import toBytes
## This module is responsible for reading the UID from RFID cards
class NFC_Module():
def readUID(self,ATR,apdu):
cardtype = ATRCardType(toBytes(ATR))
cardrequest = CardRequest(timeout=None, cardType=cardtype)
cardservice = cardrequest.waitforcard()
try:
cardservice.connection.connect()
response, sw1, sw2 = cardservice.connection.transmit(apdu)
except CardConnectionException:
return None
## print toHexString(cardservice.connection.getATR())
print "%x %x" % (sw1, sw2)
print "%x, %x, %x, %x" % (response[0],response[1],response[2],response[3])
if sw1 == 0x90 and sw2 == 0x00: #check if UID was read successfully
UID = str(hex(response[0]))+str(hex(response[1]))+str(hex(response[2]))+str(hex(response[3]))
return UID.replace("0x","")
return None