【问题标题】:How to reset this code after detecting a smartcard without terminating it, thus continues to listen for other smartcards?如何在检测到智能卡后重置此代码而不终止它,从而继续侦听其他智能卡?
【发布时间】:2019-04-04 20:15:22
【问题描述】:

我有一个代码正在运行并成功打印 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 后,它会等待卡片被移除并输入另一个卡片,然后循环重置。

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

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

【问题讨论】:

  • 您显示的代码不会尝试重复自己。请展示您尝试的涉及循环的修复,因为我们无法判断您未向我们展示的代码有什么问题。
  • 由于我对 python 和 pyscard 的了解有限,我无法理解如何循环连接。代码现在等待连接,当连接建立时,它遵循一个设置命令(打印等)然后终止。我希望它重置而不是终止,但我不知道如何。希望有任何意见或建议
  • 我认为你需要使循环更大。目前所有卡检测和连接的东西只发生一次,在开始时。您可能希望每次循环都发生这种情况,因此将while True 行进一步向上移动!您可能需要在循环底部添加一些清理代码来重置内容,但我对您使用的库了解不够,无法猜测它的外观。
  • 我整理了循环,现在程序永远运行(这就是我想要的)。我不知道如何在 while 循环中打印一次。有什么想法吗?

标签: python-3.x smartcard uid acr122 pyscard


【解决方案1】:

这样做会导致 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多