简体   繁体   English

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

[英]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.

相关问题 码完后步进电机继续转动 - Stepper motor continues turning after code is finished 在给我的循环停止条件后,它会继续。 打印语句有效,但代码继续运行 - after giving a condition for my loop to stop it continues. the print statement works, but the code continues running 如何在 Tkinter 中重置/延迟 .after? - How to reset/delay .after in Tkinter? 终止子进程后如何自动从Popen获取返回码? - How to automatically get returncode from Popen after terminating child process? 如何制作 Python 程序,以便即使在终止后,变量、列表和其他对象的值也不会重置? - How can I make a Python program so that even after termination, the variable, list and other objects' values do not reset? 代码未终止,并一直在寻找输入 - Code not terminating and keeps looking for input 如何在不编译Event API原型的情况下收听Skaffold事件? - How to listen to Skaffold events without compiling the Event API protos? 如何让线程继续循环 - How to let threads continues cycle 如何在if else语句之后继续你的代码而不破坏? - How to continue your code after an if else statement without breaking? 将脚本变量保存在代码中,并在重启后将其重置 - Save a Script Variables inside code and reset them after reboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM