Skip to main content

The mfrc522 library is used to interact with RFID readers that use the MFRC522 chip

Project description

MFRC522-python

The mfrc522 library is used to interact with RFID readers that use the MFRC522 chip.

Example Code

read.py

To read a particular sector and convert the bytes to plain string/text.

from mfrc522 import MFRC522

reader = MFRC522()
def read(trailer_block, key, block_addrs):
    (status, TagType) = reader.Request(reader.PICC_REQIDL)
    if status != reader.MI_OK:
        return None, None
    (status, uid) = reader.Anticoll()
    if status != reader.MI_OK:
        return None, None
    id = uid
    reader.SelectTag(uid)
    status = reader.Authenticate(
        reader.PICC_AUTHENT1A, trailer_block , key, uid)
    data = []
    text_read = ''
    if status == reader.MI_OK:
        for block_num in block_addrs:
            block = reader.ReadTag(block_num)
            if block:
                data += block
        if data:
            text_read = ''.join(chr(i) for i in data)
    reader.StopAuth()
    return id, text_read

trailer_block = 11
key = KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
block_addrs = [8,9,10]
id, text = read(trailer_block, key, block_addrs)
while not id:
    id, text = read(trailer_block, key, block_addrs)

print(id)
print(text)

write.py

To write a particular sector.

from mfrc522 import MFRC522

reader = MFRC522()

def write(trailer_block, key, block_addrs, text):
    (status, TagType) = reader.Request(reader.PICC_REQIDL)
    if status != reader.MI_OK:
        return None, None
    (status, uid) = reader.Anticoll()
    if status != reader.MI_OK:
        return None, None
    reader.SelectTag(uid)
    status = reader.Authenticate(
        reader.PICC_AUTHENT1A, trailer_block, key, uid)
    reader.ReadTag(trailer_block)
    if status == reader.MI_OK:
        data = bytearray()
        data.extend(bytearray(text.ljust(
            len(block_addrs) * 16).encode('ascii')))
        i = 0
        for block_num in block_addrs:
            reader.WriteTag(block_num, data[(i*16):(i+1)*16])
            i += 1
    reader.StopAuth()
    return uid, text[0:(len(block_addrs) * 16)]

trailer_block = 11
block_addrs = [8, 9, 10]
key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
text = "some random text"
uid, text_in = write(trailer_block, key, block_addrs, text)
while not uid:
    uid, text_in = write(trailer_block, key, block_addrs, text)
print(uid, text_in)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mfrc522-python-0.0.5.dev2.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

mfrc522_python-0.0.5.dev2-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

Details for the file mfrc522-python-0.0.5.dev2.tar.gz.

File metadata

  • Download URL: mfrc522-python-0.0.5.dev2.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for mfrc522-python-0.0.5.dev2.tar.gz
Algorithm Hash digest
SHA256 8f6915f838e7f0bd2f93f9fa913da7d9be64ea9db13d2307405ff51b7cf854d1
MD5 ec7f8c3e0404fbcafa44b7520f808533
BLAKE2b-256 0aab3bc8759f7d94650a2dab25d6276297d1d076ee6562e0ad48c95bc722524a

See more details on using hashes here.

File details

Details for the file mfrc522_python-0.0.5.dev2-py3-none-any.whl.

File metadata

File hashes

Hashes for mfrc522_python-0.0.5.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 fccafd6ba82829e44610cccd5d796b906a32e96203fa132db602cebb2162c203
MD5 a6e76d48500d918b8d533f3f7db24587
BLAKE2b-256 f12b4a0d9b1530229122c4dee9f8ebe7b1e8aed9dcc35593a493d3bb8ed8f52a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page