I've set up a RC522 Card on my pi using This Tutorial and hacked together the Read.py and Write.py scripts into a Clone.py script:

#!usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

    print("Scan Card Now")
    id, text = reader.read()
    print("Current ID:")
    print("Place Card for Write, Then Press ENTER")
    print("Write Complete")

Which works just fine when testing on blank cards. However, when I tried to clone an old hotel keycard I get:

Traceback (most recent call last):
  File "clone.py, line 16 in <module>
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 60 in write
    id, text_in = self.write_no_block(text)
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 78, in write_no_block
    data.extended(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
UnicodeEncodeError: 'ascii' codec cant encode characters in position 1-2: ordinal not in range(128)

What can I do to fix this?

  • Hello. This looks to be an error with the SimpleMFC522 library not handling 8-bit data, have you tried raising an issue with the author after checking you have the latest version? github.com/pimylifeup/MFRC522-python/issues – Roger Jones Feb 23 '20 at 20:37
  • @RogerJones I've raised the issue on the website, But I'll see if the github gets more of a response. Thank you. – RLahey Mar 4 '20 at 4:34

Ah, your hotel card might contain non ASCII characters, say Unicoded Chinese. So you need to decode the Unicoded Chinese characters each of which is denoted by two bytes. Of course hotel cards usually use other tricks to punish the bad guys trying to clone.

  • Thanks for the reply. That would make sense, but when I run the read script, I just get a 12-character number string and 'xE' and I'm fairly confident that those are covered by unicode.... – RLahey Feb 21 '20 at 16:26
  • Ah my apologies. I was just making a wild guess, that you were a bad guy, don't know nothing about even ASCII. My second guess is that you are after all a good guy, and that a now 5 star hotel is asking you for help, how to avoid the real bad guys cloning their cards and eating free lunch. Now let me see if me the NFC newbie, can wiki, google, and do some experiments to catch you up. Cheers. – tlfong01 Feb 22 '20 at 1:38
  • 1
    That's alright, anything you can find would be appreciated. – RLahey Feb 23 '20 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.