如何在不终止智能卡的情况下检测到智能卡后重置此代码,从而继续侦听其他智能卡?

How to reset this code after detecting a smartcard without terminating it, thus continues to listen for other smartcards?

我有一个代码 运行 并成功打印了 ATR、UID 和状态,但是,程序在检测并打印 UID 后结束。如何在检测到并等待移除和插入不同卡(或同一张卡)后重置我的代码

我试过使用 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))

它尝试的修复(这一直在打印 并且当我取出卡时它终止 (已解决)):

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")

问题是我不知道如何创建一个只打印一次的循环。打印 UID 后,它等待卡被移除并输入另一张卡,然后循环重置。

编辑:我设法重置了代码,但是,我不知道如何让程序打印一次而不是继续打印。有什么建议吗?

我希望代码在智能卡出现后打印一次,然后当智能卡被移除时,重新设置并再次侦听智能卡。

这会做到这一点,但它会使 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")