Card Reader turns off after removal of RFID tag, won't turn back on until program is restarted

184 Views Asked by At

I am trying to use a ACR122u NFC reader to communicate with an NFC tag. The program starts fine, and it is able to read and do all the desired operations on a tag when one is connected. However, once the NFC tag is removed, the reader turns off and will not turn on until the program restarts. Here is my code for polling the reader:

def CheckNFCCard():
    getUIDCommand = [0xFF, 0xCA, 0x00, 0x00, 0x00]
    turnOffBeepCommand = [0xFF, 0x00, 0x52, 0x00, 0x00]
    getDataCommand = [0xFF, 0xB0, 0x00, 0x07, 0x10]
    updateBlockCommandFirst=[0xFF, 0xD6, 0x00, 0x07, 0x04, 0x65, 0x6E, 0x75, 0x73]
    updateBlockCommandSecond=[0xFF, 0xD6, 0x00, 0x08, 0x04, 0x65, 0x64, 0xFE, 0x00]

  #get NFC reader
    r = readers()
    reader = r[0]
    #select reader, connect
    conn = reader.createConnection()
    while(True):
        print('NFC routine restart')
        try:
            conn.connect()
        except:
            time.sleep(1)
            continue
        #send hex command to turn off beeps on scan.
        conn.transmit(turnOffBeepCommand)

        #get data encoded on tag.
        data, sw1, sw2 = conn.transmit(getDataCommand)
        if (sw1, sw2) == (0x90, 0x0):
            print("Status: The operation completed.")

        #Turn decimal numbers into characters
        stringList = [chr(x) for x in data]
        #
        identifierString = ''.join(stringList)[2:6]
        if (identifierString == "used"):
            pub.sendMessage('NFCDetected', arg1='used')
        elif (identifierString == "new_"):
            data, sw1, sw2 = conn.transmit(updateBlockCommandFirst)
            if (sw1, sw2) == (0x90, 0x0):
                conn.transmit(updateBlockCommandSecond)
            Thread(target=pub.sendMessage, args=('NFCDetected',), kwargs={'new' : arg1}).Start()
        time.sleep(1)
        continue

The loop continues to run as expected, but the reader is shut off and will not connect again.

Any help would be much appreciated.

0

There are 0 best solutions below