| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from smartcard.CardType import AnyCardType
- from smartcard.CardRequest import CardRequest
- from smartcard.util import toHexString
- from smartcard.ATR import ATR
- import logging
- # 通过下面的方式进行简单配置输出方式与日志级别
- logging.basicConfig(filename='logger.log', level=logging.INFO)
- # 初始化读卡器
- def init():
- print('初始化读卡器')
- card_type = AnyCardType()
- card_request = CardRequest(timeout=500, cardType=card_type)
- card_service = card_request.waitforcard()
- card_service.connection.connect()
- atr = ATR(card_service.connection.getATR())
- return card_service
- def trace_command(apdu):
- logging.info('sending: ' + toHexString(apdu))
- def trace_response(response, sw1, sw2):
- if response is None:
- response = []
- print(
- 'serial no.: ',
- toHexString(response),
- ' status words: ',
- "%x %x" % (sw1, sw2)
- )
- logging.info('serial no.: ' +
- toHexString(response) +
- ' status words: ' +
- "%x %x" % (sw1, sw2))
- def bytes2hexstr(data_bytes):
- data_number = len(data_bytes)
- data_hex_str = ''
- for i in range(0, data_number):
- data_int = int(str(data_bytes[i]))
- data_hex_str += "{:02X} ".format(data_int)
- return data_hex_str
- #发送指令
- def sendCommand(card_service,command):
- trace_command(command)
- res, s1, s2 = card_service.connection.transmit(command)
- trace_response(res, s1, s2)
- return res,s1,s2
- # 读取芯片UID
- def read_uid(card_service):
- rf_command_bytes = [0xFF,0xCA,0x00,0x00,0x00]
- res,s1,s2 = sendCommand(card_service,rf_command_bytes)
- return bytes2hexstr(res)
|