[英]How to reset this code after detecting a smartcard without terminating it, thus continues to listen for other smartcards?
I have a code running and successfully printing the ATR, UID, and status, however, the program ends after detecting and printing the UID.我有一个代码正在运行并成功打印 ATR、UID 和状态,但是,程序在检测并打印 UID 后结束。 How do I make my code reset after detecting and wait for the removal and insertion of a different card (or same card)
如何在检测到并等待拔出和插入不同的卡(或同一张卡)后重置我的代码
I've tried using while loops, but that causes the program to print the ATR, UID, and status many time and when the card is removed the programm terminates with an error.我曾尝试使用 while 循环,但这会导致程序多次打印 ATR、UID 和状态,并且当卡被移除时,程序会因错误而终止。
from smartcard.CardRequest import CardRequest
from smartcard.Exceptions import CardRequestTimeoutException
from smartcard.CardType import AnyCardType
from smartcard import util
WAIT_FOR_SECONDS = 60
# respond to the insertion of any type of smart card
card_type = AnyCardType()
# create the request. Wait for up to x seconds for a card to be attached
request = CardRequest(timeout=WAIT_FOR_SECONDS, cardType=card_type)
# listen for the card
service = None
try:
service = request.waitforcard()
except CardRequestTimeoutException:
print("ERROR: No card detected")
exit(-1)
# when a card is attached, open a connection
conn = service.connection
conn.connect()
# get and print the ATR and UID of the card
get_uid = util.toBytes("FF CA 00 00 00")
print("ATR = {}".format(util.toHexString(conn.getATR())))
data, sw1, sw2 = conn.transmit(get_uid)
uid = util.toHexString(data)
status = util.toHexString([sw1, sw2])
print("UID = {}\tstatus = {}".format(uid, status))
The fix it tried (This keeps printing
and when I remove the card it terminates
(solved)):它尝试的修复(这会继续打印
,当我取出卡时它会终止
(已解决)):
from smartcard.CardRequest import CardRequest
from smartcard.Exceptions import CardRequestTimeoutException
from smartcard.CardType import AnyCardType
from smartcard import util
WAIT_FOR_SECONDS = 60
# respond to the insertion of any type of smart card
card_type = AnyCardType()
# create the request. Wait for up to x seconds for a card to be attached
request = CardRequest(timeout=WAIT_FOR_SECONDS, cardType=card_type)
while True:
# listen for the card
service = None
try:
service = request.waitforcard()
except CardRequestTimeoutException:
print("ERROR: No card detected")
# could add "exit(-1)" to make code terminate
# when a card is attached, open a connection
try:
conn = service.connection
conn.connect()
# get the ATR and UID of the card
get_uid = util.toBytes("FF CA 00 00 00")
data, sw1, sw2 = conn.transmit(get_uid)
uid = util.toHexString(data)
status = util.toHexString([sw1, sw2])
# print the ATR and UID of the card
print("ATR = {}".format(util.toHexString(conn.getATR())))
print("UID = {}\tstatus = {}".format(uid, status))
except:
print("no connection")
The problem is I can't figure out how to create a loop where it prints once.问题是我不知道如何创建一个打印一次的循环。 After printing the UID it waits for card to be removed and another to be inputed and the cycle resets.
打印 UID 后,它等待取出卡并输入另一个卡,然后循环重置。
Edit: I managed to make the code reset, however, I can't figure out how to make the program print once rather than keep printing.编辑:我设法使代码重置,但是,我无法弄清楚如何使程序打印一次而不是继续打印。 Any suggestions?
有什么建议?
I expect the code to print once after smartcard is present and then when the smartcard is removed, resets and listens to the smartcard again.我希望代码在智能卡出现后打印一次,然后当智能卡被移除时,重置并再次监听智能卡。
This will do that but it will make the UI unclickable since it's still running in the background.这会做到这一点,但它会使 UI 无法点击,因为它仍在后台运行。
# respond to the insertion of any type of smart card
card_type = AnyCardType()
# create the request. Wait for up to x seconds for a card to be attached
request = CardRequest(timeout=0, cardType=card_type)
hasopened = False
while True:
# listen for the card
service = None
try:
service = request.waitforcard()
except CardRequestTimeoutException:
print("ERROR: No card detected")
ui.lineEdit_2.setText('NO CARD DETECTED')
ui.lineEdit_2.setStyleSheet("background-color: #e4e0e0;\n" "color: #000000;")
ui.label_5.setStyleSheet("background-color: #ff0000")
# could add "exit(-1)" to make code terminate
# when a card is attached, open a connection
try:
conn = service.connection
conn.connect()
# get the ATR and UID of the card
get_uid = util.toBytes("FF CA 00 00 00")
data, sw1, sw2 = conn.transmit(get_uid)
uid = util.toHexString(data)
status = util.toHexString([sw1, sw2])
# print the ATR and UID of the card
if conn and not hasopened:
print("ATR = {}".format(util.toHexString(conn.getATR())))
print("UID = {}\tstatus = {}".format(uid, status))
ui.lineEdit_2.setText(uid)
ui.lineEdit_2.setStyleSheet("background-color: #e4e0e0;\n" "color: #000000;")
ui.label_5.setStyleSheet("background-color: #86dc3d")
hasopened=True
except:
print("no connection")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.