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
| import sys
import usb.core
import usb.util
def lecture_rfid():
VENDOR_ID = 0x0403
PRODUCT_ID = 0x6001
DATA_SIZE = 16 # 224
BADGE_CODE = {0x1E:'1', 0x1F:'2', 0x20:'3', 0x21:'4', 0x22:'5', 0x23:'6', 0x24:'7'
, 0x25:'8', 0x26:'9', 0x27:'0', 0x28:''} # 28=enter
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if device is None:
sys.exit("Could not find RFID Reader.")
if device.is_kernel_driver_active(0): # On détache le périphérique du kernel, plus d'envoi sur stdin
try:
device.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver: %s" % str(e))
endpoint = device[0][(0,0)][0]
data = []
lu = False
print "Waiting to read..."
lecture=''
while 1:
try:
data += device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
if not lu:
print "Waiting to read..."
lu = True
except usb.core.USBError as e:
if e.args == (110,'Operation timed out') and lu:
if len(data) < DATA_SIZE:
print "Lecture incorrecte, recommencez. (%d bytes)" % len(data)
print "Data: %s" % ''.join(map(hex, data))
data = []
lu = False
continue
else:
for n in range(0,len(data),16):
print ' '.join(map(hex,data[n:n+16]))
lecture+=BADGE_CODE[data[n+2]]
break # Code lu
return lecture
if __name__ == '__main__':
print lecture_rfid() |
Partager