forked from MiczFlor/RPi-Jukebox-RFID
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.py.pcsc
More file actions
61 lines (43 loc) · 1.91 KB
/
Reader.py.pcsc
File metadata and controls
61 lines (43 loc) · 1.91 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# Reader.py supporting PC/SC-readers
# Uses the first available reader
# Requirements
# apt install pcscd python3-pyscard
from smartcard.scard import *
from smartcard.util import toHexString
from smartcard.util import *
class Reader:
def __init__(self):
pass
def readCard(self):
response = []
try:
hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
if hresult != SCARD_S_SUCCESS:
raise error('Failed to establish context: ' + SCardGetErrorMessage(hresult))
try:
hresult, readers = SCardListReaders(hcontext, [])
if hresult != SCARD_S_SUCCESS:
raise error('Failed to list readers: ' + SCardGetErrorMessage(hresult))
readerstates = []
for i in range(len(readers)):
readerstates += [(readers[i], SCARD_STATE_UNAWARE)]
hresult, newstates = SCardGetStatusChange(hcontext, 0, readerstates)
hresult, newstates = SCardGetStatusChange(
hcontext,
INFINITE,
newstates)
reader = readers[0]
hresult, hcard, dwActiveProtocol = SCardConnect(
hcontext,
reader,
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
hresult, response = SCardTransmit(hcard, dwActiveProtocol, [0xFF, 0xCA, 0x00, 0x00, 0x00])
finally:
hresult = SCardReleaseContext(hcontext)
if hresult != SCARD_S_SUCCESS:
raise error('Failed to release context: ' + SCardGetErrorMessage(hresult))
return (toHexString(response, PACK))
except error as e:
print(e)