mfrc522-python


Namemfrc522-python JSON
Version 0.0.7 PyPI version JSON
download
home_page
SummaryThe mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
upload_time2024-02-01 08:19:56
maintainer
docs_urlNone
authorAditya
requires_python>=3.7
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # MFRC522-python
### The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.
<br>

## Table of Contents
- [Introduction](#introduction)
- [Installation](#installation)
- [Connections](#connections)
- [Usage](#usage)
	- [Using `mfrc522.MFRC522`](#using-mfrc522-class)
	- [Using `mfrc522.SimpleMFRC522`](#using-simplemfrc522-class)
	- [Using `mfrc522.BasicMFRC522`](#using-basicmfrc522-class)
- [Example Code](#example-code)
	- [Using `mfrc522.MFRC522`](#using-mfrc522-class-1)
	- [Using `mfrc522.SimpleMFRC522`](#using-simplemfrc522-class-1)
	- [Using `mfrc522.BasicMFRC522`](#using-basicmfrc522-class-1)
- [Contributions](#contributions)
- [License](#license)

## Introduction
### **MFRC522**
The MFRC522 is a popular RFID module that supports contactless communication using the 13.56 MHz frequency. It can read and write data to RFID cards or tags, making it ideal for projects that require identification or access control. This repository provides a library and example code to interact with the MFRC522 module using various microcontroller platforms.

### **Sectors and Blocks**
Sectors and Blocks refer to the organization and structure of memory on an RFID tag or card.
<br>
The memory of card is divided into **16 sectors** (for classic MIFARE 1k). Each sector contains **4 blocks** and each block can hold upto **16 bytes of data**.
<br>
So a classic MIFARE 1k has **16 sectors, 64 blocks (16  sectors x 4 blocks)  and can hold upto 1024 bytes (64 blocks x 16 bytes)**  in total.
<br>
Not Every blocks are writeable, the first block of the first sector and the last block of every sectors are not writeable.
The first block of **first sector typically holds the manufaturer's details** *(like **manufacturer's identification number (MID) and the application identifier (AID)**. The MID is a unique number that identifies the manufacturer of the card. The AID is a unique number that identifies the application that is stored on the card.)*. **The last block of every sector is called a trailer block**, the trailer block contains information about the sector, such as the **sector number, the number of blocks in the sector, and the checksum**. The checksum is used to verify the integrity of the data in the sector. **If you ever write to a trailer block then the entire sector will be unwritable.**

| Sectors | Block Number   |
|---------|----------------|
| 0       | 0, 1, 2, 3     |
| 1       | 4, 5, 6, 7     |
| 2       | 8, 9, 10, 11   |
| 3       | 12, 13, 14, 15 |
| 4       | 16, 17, 18, 19 |
| 5       | 20, 21, 22, 23 |
| 6       | 24, 25, 26, 27 |
| 7       | 28, 29, 30, 31 |
| 8       | 32, 33, 34, 35 |
| 9       | 36, 37, 38, 39 |
| 10      | 40, 41, 42, 43 |
| 11      | 44, 45, 46, 47 |
| 12      | 48, 49, 50, 51 |
| 13      | 52, 53, 54, 55 |
| 14      | 56, 57, 58, 59 |
| 15      | 60, 61, 62, 63 |

Manfacturer's details is stored in block number 0  and 
Trailer blocks are 3, 7, 11, etc,.
<br>
**( Tips: It's better to not store any data in the first sector. Start from the first sector )**
<br>
**Note: Different cards have diiferent number of sectors and blocks. For example, Classic MIFARE 4k has 40 Sectors. It's better to check the datasheet for the RFID tag/card you use before writing data to it.**

## Installation
### 1. From PyPI (Stable)
```
pip install mfrc522-python 
```
### 2. From Github ( Beta/dev version)
```
pip install git+https://github.com/1AdityaX/mfrc522-python
```
## Connections
| RF522 Module | Raspberry Pi          |
|--------------|---------------------- |
| SDA          | Pin 24 / GPIO8 (CE0)  |
| SCK          | Pin 23 / GPIO11 (SCKL)|
| MOSI         | Pin 19 / GPIO10 (MOSI)|
| MISO         | Pin 21 / GPIO9 (MISO) |
| IRQ          | –                     |
| GND          | GND                   |
| RST          | Pin 22 / GPIO25       |
| 3.3V         | 3.3V                  |

## Usage
### Using `MFRC522` class
1. Import and create an instance of class `MFRC522` from `mfrc522` module.
```py
from mfrc522 import MFRC522

reader = MFRC522() 
```
2. Request communication with a PICC *(Proximity Integrated Circuit Card A.K.A rfid card)* and check if the communication is established.
```py
status =  None
while status != reader.MI_OK:
	(status, TagType) = reader.Request(reader.PICC_REQIDL)
	if status == reader.MI_OK:
		print("Connection Success!")
```
3.  Perform an Anti-collision Algorithm *(Picks one rfid card if multiple cards are placed )* and return the UID.
```py
(status, uid) = reader.Anticoll()
if status == reader.MI_OK:
	print(uid)
```
4.  Select the tag to Authenticate and perform funtions like reading or writing data to the card.
```py
reader.SelectTag(uid)
``` 
5. Authenticate a particular sector with a key to read or write data to it.
```py
trailer_block = 11
#This is the default key for MIFARE Cards
key = [0xFF, 0xFF, 0xFF , 0xFF, 0xFF, 0xFF]
status = reader.Authenticate(
        reader.PICC_AUTHENT1A, trailer_block , key, uid)
```
6. Now read or write to the block numbers in the sector that you have authenticated.

**To read**
```py
block_nums = [8, 9, 10]
data = []
for block_num in block_nums:
	block_data = reader.ReadTag(block_num)
	if block_data:
		data += block_data
if data:
	print(''.join(chr(i) for i in data))
```
**To write**
```py
block_nums = [8, 9, 10]
text = "Hello, World"
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
```
7. Once you business with the RFID card or Tag is over. Always Stop the Authenciation/communiction with the card.
**Note: If you miss out this step, you won't be able to use a different card.**
```py
reader.StopAuth()
```

### Using `SimpleMFRC522` class
1. Import and create an instance of class `SimpleMFRC522` from `mfrc522` module
```py
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()
```
2. Use `read()`  to read and `write()` to write
**To read** 
```py
id, text = reader.read()
print(f"ID: {id}")
print(f"Text: {text}")
```
**To write**
```py
text = "hello_world"
id, text_written = reader.write(text)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
```
### `mfrc522.SimpleMFRC522()` Methods
#### `__init__()`

Initializes a `SimpleMFRC522` instance. It sets up the MFRC522 module, defines the default authentication key, sets the trailer block number to 11, and initializes the BasicMFRC522 module.

#### `read()`

Reads data from the RFID tag.

-   Returns: A tuple containing the tag ID (as an integer) and the data read (as a string).

#### `read_id()`

Reads the tag ID from the RFID tag.

-   Returns: The tag ID as an integer.

#### `write(text)`

Writes the given text to an RFID tag.

-   Args:
    -   `text` (str): A string to be written to the RFID tag.
-   Returns: A tuple containing the ID of the tag and the text that was written to the tag.
    
### Using `BasicMFRC522` class
1. Import and create an instance of the class `BasicMFRC522` from `mfrc522` module
```py
from mfrc522 import BasicMFRC522

reader = BasicMFRC522()
```
2. Use any methods.

### `mfrc522.BasicMFRC522` Methods


####  `__init__(KEY=[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])`
Initializes a `BasicMFRC522` instance.
-   Args:
    -   `KEY` (list): The authentication key used for reading and writing data. The default key is `[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]`.



#### `read_sector(trailer_block=11)`
Reads data from a sector of the RFID tag.
-   Args:
    -   `trailer_block` (int): The block number of the sector trailer.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the data read (as a string).

#### `read_sectors(trailer_blocks=[11])`
Reds data from multiple sectors of the RFID tag.
-   Args:
    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated data read from all sectors (as a string).

#### `read_id()`
 Reads the tag ID from the RFID tag.
-   Returns:
    -   `int`: The tag ID as an integer.

#### `read_id_no_block()`
Attempts to read the tag ID from the RFID tag without blocking.
-   Returns:
    -   `int`: The tag ID as an integer, or `None` if the operation fails.

#### `read_no_block(trailer_block)`
Attempts to read data from the RFID tag without blocking.
-   Args:
    -   `trailer_block` (int): The block number of the sector trailer.
    -   `block_addr` (tuple): The block numbers of the data blocks to read.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the data read (as a string), or `(None, None)` if the operation fails.

#### `write_sector(text, trailer_block=11)`
Writes data to a sector of the RFID tag.
-   Args:
    -   `text` (str): The data to write.
    -   `trailer_block` (int): The block number of the sector trailer.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the data written (as a string).

#### `write_sectors(text, trailer_blocks=[11])`
 Writes data to multiple sectors of the RFID tag.
-   Args:
    -   `text` (str): The data to write.
    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated data written to all sectors (as a string).

#### `write_no_block(text, trailer_block)`
Attempts to write data to the RFID tag without blocking.
-   Args:
    -   `text` (str): The data to write.
    -   `trailer_block` (int): The block number of the sector trailer.
    -   `block_addr` (tuple): The block numbers.
    
#### `clear_sector(trailer_block=11)`
Clears a sector of the RFID tag by writing empty data to all blocks.
-   Args:
    -   `trailer_block` (int): The block number of the sector trailer.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string).

#### `clear_sectors(trailer_blocks=[11])`
Clears multiple sectors of the RFID tag by writing empty data to all blocks.
-   Args:
    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated cleared data (as an empty string).

#### `clear_no_block(trailer_block)`
Attempts to clear a sector of the RFID tag without blocking.
-   Args:
    -   `trailer_block` (int): The block number of the sector trailer.
    -   `block_addr` (tuple): The block numbers of the data blocks to clear.
-   Returns:
    -   `tuple`: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string), or `(None, None)` if the operation fails.

**Note: Clearing a sector will permanently erase the data stored in the blocks of that sector. Use with caution as this operation cannot be undone.**

## Example Code
### Using `MFRC522` class 
 **read.py**
 
 To read a particular sector and convert the bytes to plain string/text.
```py
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 = [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.
```py
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)
print(text_in)
```
### Using `SimpleMFRC522` class
**read.py**
```py
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

id, text = reader.read()
print(f"ID: {id}")
print(f"Text: {text}")
```
**write.py**
```py
from mfrc522 import SimpleMFRC522

writer = SimpleMFRC522()
text = "Hello, world"

id, text_written = writer.write(text)
print(f"ID: {id}")  
print(f"Text Written: {text_written}")
```
### Using `BasicMFRC522` class
**read_sector.py**
To read a particular sector
```py
from mfrc522 import BasicMFRC522

reader = BasicMFRC522()
sector = 15
id, text = reader.read_sector(sector)
print(f"ID: {id}")
print(f"Text: {text}")
```
**read_sectors.py**
To read multiple sectors
```py
from mfrc522 import BasicMFRC522

reader = BasicMFRC522()
sectors = [11, 15]
id, text = reader.read_sectors(sectors)
print(f"ID: {id}")
print(f"Text: {text}")
```
**write_sector.py**
To write a particular sector
```py
from mfrc522 import BasicMFRC522

writer = BasicMFRC522()

sector =  15
text =  "Example to use write_sector method"
id, text_written = writer.write_sector(text, sector)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
```
**write_sectors.py**
To write multiple sectors
```py
from mfrc522 import BasicMFRC522

writer = BasicMFRC522()

sector = [11, 15]
text =  "Example to use write_sector method"

id, text_written = writer.write_sectors(text, sector)
print(f"ID: {id}")
print(f"Text Written: {text_written}")
```
**clear_sector.py**
To clear data written in a sector
```py
from mfrc522 import BasicMFRC522

writer = BasicMFRC522()
sector = 11

id = writer.clear_sector(sector)
print("Cleared")
```

**clear_sectors.py**
To clear data written in multiple sectors
```py
from mfrc522 import BasicMFRC522

writer = BasicMFRC522()
sectors  = [11, 15]

id = writer.clear_sectors(sectors)
print("Cleared")
```
## Contributions
Contributions to the `mfrc522-python` library are welcome. To contribute, follow these steps:

1.  Fork the repository on GitHub.
2.  Create a new branch for your changes.
3.  Make your changes and commit them.
4.  Push your changes to your forked repository.
5.  Submit a pull request to the main repository.

That's it! By submitting a pull request, you can contribute your changes to the `mfrc522-python` library.
 Provide a clear description of your changes in the pull request.
If you have any questions or need further assistance, feel free to open an issue.

## License
This project is licensed under the GNU General Public License (GPL) version 3.0. You can find the full text of the license in the [LICENSE](LICENSE) file.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mfrc522-python",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Aditya",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/e2/63/7a5ba63fcceea25f8d2a4101a648f97dfc4466b0615b78bd17977218abce/mfrc522-python-0.0.7.tar.gz",
    "platform": null,
    "description": "# MFRC522-python\r\n### The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.\r\n<br>\r\n\r\n## Table of Contents\r\n- [Introduction](#introduction)\r\n- [Installation](#installation)\r\n- [Connections](#connections)\r\n- [Usage](#usage)\r\n\t- [Using `mfrc522.MFRC522`](#using-mfrc522-class)\r\n\t- [Using `mfrc522.SimpleMFRC522`](#using-simplemfrc522-class)\r\n\t- [Using `mfrc522.BasicMFRC522`](#using-basicmfrc522-class)\r\n- [Example Code](#example-code)\r\n\t- [Using `mfrc522.MFRC522`](#using-mfrc522-class-1)\r\n\t- [Using `mfrc522.SimpleMFRC522`](#using-simplemfrc522-class-1)\r\n\t- [Using `mfrc522.BasicMFRC522`](#using-basicmfrc522-class-1)\r\n- [Contributions](#contributions)\r\n- [License](#license)\r\n\r\n## Introduction\r\n### **MFRC522**\r\nThe MFRC522 is a popular RFID module that supports contactless communication using the 13.56 MHz frequency. It can read and write data to RFID cards or tags, making it ideal for projects that require identification or access control. This repository provides a library and example code to interact with the MFRC522 module using various microcontroller platforms.\r\n\r\n### **Sectors and Blocks**\r\nSectors and Blocks refer to the organization and structure of memory on an RFID tag or card.\r\n<br>\r\nThe memory of card is divided into **16 sectors** (for classic MIFARE 1k). Each sector contains **4 blocks** and each block can hold upto **16 bytes of data**.\r\n<br>\r\nSo a classic MIFARE 1k has **16 sectors, 64 blocks (16  sectors x 4 blocks)  and can hold upto 1024 bytes (64 blocks x 16 bytes)**  in total.\r\n<br>\r\nNot Every blocks are writeable, the first block of the first sector and the last block of every sectors are not writeable.\r\nThe first block of **first sector typically holds the manufaturer's details** *(like **manufacturer's identification number (MID) and the application identifier (AID)**. The MID is a unique number that identifies the manufacturer of the card. The AID is a unique number that identifies the application that is stored on the card.)*. **The last block of every sector is called a trailer block**, the trailer block contains information about the sector, such as the **sector number, the number of blocks in the sector, and the checksum**. The checksum is used to verify the integrity of the data in the sector. **If you ever write to a trailer block then the entire sector will be unwritable.**\r\n\r\n| Sectors | Block Number   |\r\n|---------|----------------|\r\n| 0       | 0, 1, 2, 3     |\r\n| 1       | 4, 5, 6, 7     |\r\n| 2       | 8, 9, 10, 11   |\r\n| 3       | 12, 13, 14, 15 |\r\n| 4       | 16, 17, 18, 19 |\r\n| 5       | 20, 21, 22, 23 |\r\n| 6       | 24, 25, 26, 27 |\r\n| 7       | 28, 29, 30, 31 |\r\n| 8       | 32, 33, 34, 35 |\r\n| 9       | 36, 37, 38, 39 |\r\n| 10      | 40, 41, 42, 43 |\r\n| 11      | 44, 45, 46, 47 |\r\n| 12      | 48, 49, 50, 51 |\r\n| 13      | 52, 53, 54, 55 |\r\n| 14      | 56, 57, 58, 59 |\r\n| 15      | 60, 61, 62, 63 |\r\n\r\nManfacturer's details is stored in block number 0  and \r\nTrailer blocks are 3, 7, 11, etc,.\r\n<br>\r\n**( Tips: It's better to not store any data in the first sector. Start from the first sector )**\r\n<br>\r\n**Note: Different cards have diiferent number of sectors and blocks. For example, Classic MIFARE 4k has 40 Sectors. It's better to check the datasheet for the RFID tag/card you use before writing data to it.**\r\n\r\n## Installation\r\n### 1. From PyPI (Stable)\r\n```\r\npip install mfrc522-python \r\n```\r\n### 2. From Github ( Beta/dev version)\r\n```\r\npip install git+https://github.com/1AdityaX/mfrc522-python\r\n```\r\n## Connections\r\n| RF522 Module | Raspberry Pi          |\r\n|--------------|---------------------- |\r\n| SDA          | Pin 24 / GPIO8 (CE0)  |\r\n| SCK          | Pin 23 / GPIO11 (SCKL)|\r\n| MOSI         | Pin 19 / GPIO10 (MOSI)|\r\n| MISO         | Pin 21 / GPIO9 (MISO) |\r\n| IRQ          | \u2013                     |\r\n| GND          | GND                   |\r\n| RST          | Pin 22 / GPIO25       |\r\n| 3.3V         | 3.3V                  |\r\n\r\n## Usage\r\n### Using `MFRC522` class\r\n1. Import and create an instance of class `MFRC522` from `mfrc522` module.\r\n```py\r\nfrom mfrc522 import MFRC522\r\n\r\nreader = MFRC522() \r\n```\r\n2. Request communication with a PICC *(Proximity Integrated Circuit Card A.K.A rfid card)* and check if the communication is established.\r\n```py\r\nstatus =  None\r\nwhile status != reader.MI_OK:\r\n\t(status, TagType) = reader.Request(reader.PICC_REQIDL)\r\n\tif status == reader.MI_OK:\r\n\t\tprint(\"Connection Success!\")\r\n```\r\n3.  Perform an Anti-collision Algorithm *(Picks one rfid card if multiple cards are placed )* and return the UID.\r\n```py\r\n(status, uid) = reader.Anticoll()\r\nif status == reader.MI_OK:\r\n\tprint(uid)\r\n```\r\n4.  Select the tag to Authenticate and perform funtions like reading or writing data to the card.\r\n```py\r\nreader.SelectTag(uid)\r\n``` \r\n5. Authenticate a particular sector with a key to read or write data to it.\r\n```py\r\ntrailer_block = 11\r\n#This is the default key for MIFARE Cards\r\nkey = [0xFF, 0xFF, 0xFF , 0xFF, 0xFF, 0xFF]\r\nstatus = reader.Authenticate(\r\n        reader.PICC_AUTHENT1A, trailer_block , key, uid)\r\n```\r\n6. Now read or write to the block numbers in the sector that you have authenticated.\r\n\r\n**To read**\r\n```py\r\nblock_nums = [8, 9, 10]\r\ndata = []\r\nfor block_num in block_nums:\r\n\tblock_data = reader.ReadTag(block_num)\r\n\tif block_data:\r\n\t\tdata += block_data\r\nif data:\r\n\tprint(''.join(chr(i) for i in data))\r\n```\r\n**To write**\r\n```py\r\nblock_nums = [8, 9, 10]\r\ntext = \"Hello, World\"\r\ndata = bytearray()\r\ndata.extend(bytearray(text.ljust(  len(block_addrs)  *  16).encode('ascii')))\r\ni = 0\r\nfor block_num in block_addrs:\r\n\treader.WriteTag(block_num, data[(i*16):(i+1)*16]) \r\n\ti +=  1\r\n```\r\n7. Once you business with the RFID card or Tag is over. Always Stop the Authenciation/communiction with the card.\r\n**Note: If you miss out this step, you won't be able to use a different card.**\r\n```py\r\nreader.StopAuth()\r\n```\r\n\r\n### Using `SimpleMFRC522` class\r\n1. Import and create an instance of class `SimpleMFRC522` from `mfrc522` module\r\n```py\r\nfrom mfrc522 import SimpleMFRC522\r\n\r\nreader = SimpleMFRC522()\r\n```\r\n2. Use `read()`  to read and `write()` to write\r\n**To read** \r\n```py\r\nid, text = reader.read()\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text: {text}\")\r\n```\r\n**To write**\r\n```py\r\ntext = \"hello_world\"\r\nid, text_written = reader.write(text)\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text Written: {text_written}\")\r\n```\r\n### `mfrc522.SimpleMFRC522()` Methods\r\n#### `__init__()`\r\n\r\nInitializes a `SimpleMFRC522` instance. It sets up the MFRC522 module, defines the default authentication key, sets the trailer block number to 11, and initializes the BasicMFRC522 module.\r\n\r\n#### `read()`\r\n\r\nReads data from the RFID tag.\r\n\r\n-   Returns: A tuple containing the tag ID (as an integer) and the data read (as a string).\r\n\r\n#### `read_id()`\r\n\r\nReads the tag ID from the RFID tag.\r\n\r\n-   Returns: The tag ID as an integer.\r\n\r\n#### `write(text)`\r\n\r\nWrites the given text to an RFID tag.\r\n\r\n-   Args:\r\n    -   `text` (str): A string to be written to the RFID tag.\r\n-   Returns: A tuple containing the ID of the tag and the text that was written to the tag.\r\n    \r\n### Using `BasicMFRC522` class\r\n1. Import and create an instance of the class `BasicMFRC522` from `mfrc522` module\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nreader = BasicMFRC522()\r\n```\r\n2. Use any methods.\r\n\r\n### `mfrc522.BasicMFRC522` Methods\r\n\r\n\r\n####  `__init__(KEY=[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])`\r\nInitializes a `BasicMFRC522` instance.\r\n-   Args:\r\n    -   `KEY` (list): The authentication key used for reading and writing data. The default key is `[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]`.\r\n\r\n\r\n\r\n#### `read_sector(trailer_block=11)`\r\nReads data from a sector of the RFID tag.\r\n-   Args:\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the data read (as a string).\r\n\r\n#### `read_sectors(trailer_blocks=[11])`\r\nReds data from multiple sectors of the RFID tag.\r\n-   Args:\r\n    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated data read from all sectors (as a string).\r\n\r\n#### `read_id()`\r\n Reads the tag ID from the RFID tag.\r\n-   Returns:\r\n    -   `int`: The tag ID as an integer.\r\n\r\n#### `read_id_no_block()`\r\nAttempts to read the tag ID from the RFID tag without blocking.\r\n-   Returns:\r\n    -   `int`: The tag ID as an integer, or `None` if the operation fails.\r\n\r\n#### `read_no_block(trailer_block)`\r\nAttempts to read data from the RFID tag without blocking.\r\n-   Args:\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n    -   `block_addr` (tuple): The block numbers of the data blocks to read.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the data read (as a string), or `(None, None)` if the operation fails.\r\n\r\n#### `write_sector(text, trailer_block=11)`\r\nWrites data to a sector of the RFID tag.\r\n-   Args:\r\n    -   `text` (str): The data to write.\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the data written (as a string).\r\n\r\n#### `write_sectors(text, trailer_blocks=[11])`\r\n Writes data to multiple sectors of the RFID tag.\r\n-   Args:\r\n    -   `text` (str): The data to write.\r\n    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated data written to all sectors (as a string).\r\n\r\n#### `write_no_block(text, trailer_block)`\r\nAttempts to write data to the RFID tag without blocking.\r\n-   Args:\r\n    -   `text` (str): The data to write.\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n    -   `block_addr` (tuple): The block numbers.\r\n    \r\n#### `clear_sector(trailer_block=11)`\r\nClears a sector of the RFID tag by writing empty data to all blocks.\r\n-   Args:\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string).\r\n\r\n#### `clear_sectors(trailer_blocks=[11])`\r\nClears multiple sectors of the RFID tag by writing empty data to all blocks.\r\n-   Args:\r\n    -   `trailer_blocks` (list): The list of block numbers of the sector trailers.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the concatenated cleared data (as an empty string).\r\n\r\n#### `clear_no_block(trailer_block)`\r\nAttempts to clear a sector of the RFID tag without blocking.\r\n-   Args:\r\n    -   `trailer_block` (int): The block number of the sector trailer.\r\n    -   `block_addr` (tuple): The block numbers of the data blocks to clear.\r\n-   Returns:\r\n    -   `tuple`: A tuple containing the tag ID (as an integer) and the cleared data (as an empty string), or `(None, None)` if the operation fails.\r\n\r\n**Note: Clearing a sector will permanently erase the data stored in the blocks of that sector. Use with caution as this operation cannot be undone.**\r\n\r\n## Example Code\r\n### Using `MFRC522` class \r\n **read.py**\r\n \r\n To read a particular sector and convert the bytes to plain string/text.\r\n```py\r\nfrom mfrc522 import MFRC522\r\n\r\nreader = MFRC522()\r\ndef read(trailer_block, key, block_addrs):\r\n    (status, TagType) = reader.Request(reader.PICC_REQIDL)\r\n    if status != reader.MI_OK:\r\n        return None, None\r\n    (status, uid) = reader.Anticoll()\r\n    if status != reader.MI_OK:\r\n        return None, None\r\n    id = uid\r\n    reader.SelectTag(uid)\r\n    status = reader.Authenticate(\r\n        reader.PICC_AUTHENT1A, trailer_block , key, uid)\r\n    data = []\r\n    text_read = ''\r\n    if status == reader.MI_OK:\r\n        for block_num in block_addrs:\r\n            block = reader.ReadTag(block_num)\r\n            if block:\r\n                data += block\r\n        if data:\r\n            text_read = ''.join(chr(i) for i in data)\r\n    reader.StopAuth()\r\n    return id, text_read\r\n\r\ntrailer_block = 11\r\nkey = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]\r\nblock_addrs = [8,9,10]\r\nid, text = read(trailer_block, key, block_addrs)\r\nwhile not id:\r\n    id, text = read(trailer_block, key, block_addrs)\r\n\r\nprint(id)\r\nprint(text)\r\n```\r\n\r\n**write.py**\r\n\r\nTo write a particular sector.\r\n```py\r\nfrom mfrc522 import MFRC522\r\n\r\nreader = MFRC522()\r\n\r\ndef write(trailer_block, key, block_addrs, text):\r\n    (status, TagType) = reader.Request(reader.PICC_REQIDL)\r\n    if status != reader.MI_OK:\r\n        return None, None\r\n    (status, uid) = reader.Anticoll()\r\n    if status != reader.MI_OK:\r\n        return None, None\r\n    reader.SelectTag(uid)\r\n    status = reader.Authenticate(\r\n        reader.PICC_AUTHENT1A, trailer_block, key, uid)\r\n    reader.ReadTag(trailer_block)\r\n    if status == reader.MI_OK:\r\n        data = bytearray()\r\n        data.extend(bytearray(text.ljust(\r\n            len(block_addrs) * 16).encode('ascii')))\r\n        i = 0\r\n        for block_num in block_addrs:\r\n            reader.WriteTag(block_num, data[(i*16):(i+1)*16])\r\n            i += 1\r\n    reader.StopAuth()\r\n    return uid, text[0:(len(block_addrs) * 16)]\r\n\r\ntrailer_block = 11\r\nblock_addrs = [8, 9, 10]\r\nkey = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]\r\ntext = \"some random text\"\r\nuid, text_in = write(trailer_block, key, block_addrs, text)\r\nwhile not uid:\r\n    uid, text_in = write(trailer_block, key, block_addrs, text)\r\nprint(uid)\r\nprint(text_in)\r\n```\r\n### Using `SimpleMFRC522` class\r\n**read.py**\r\n```py\r\nfrom mfrc522 import SimpleMFRC522\r\n\r\nreader = SimpleMFRC522()\r\n\r\nid, text = reader.read()\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text: {text}\")\r\n```\r\n**write.py**\r\n```py\r\nfrom mfrc522 import SimpleMFRC522\r\n\r\nwriter = SimpleMFRC522()\r\ntext = \"Hello, world\"\r\n\r\nid, text_written = writer.write(text)\r\nprint(f\"ID: {id}\")  \r\nprint(f\"Text Written: {text_written}\")\r\n```\r\n### Using `BasicMFRC522` class\r\n**read_sector.py**\r\nTo read a particular sector\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nreader = BasicMFRC522()\r\nsector = 15\r\nid, text = reader.read_sector(sector)\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text: {text}\")\r\n```\r\n**read_sectors.py**\r\nTo read multiple sectors\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nreader = BasicMFRC522()\r\nsectors = [11, 15]\r\nid, text = reader.read_sectors(sectors)\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text: {text}\")\r\n```\r\n**write_sector.py**\r\nTo write a particular sector\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nwriter = BasicMFRC522()\r\n\r\nsector =  15\r\ntext =  \"Example to use write_sector method\"\r\nid, text_written = writer.write_sector(text, sector)\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text Written: {text_written}\")\r\n```\r\n**write_sectors.py**\r\nTo write multiple sectors\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nwriter = BasicMFRC522()\r\n\r\nsector = [11, 15]\r\ntext =  \"Example to use write_sector method\"\r\n\r\nid, text_written = writer.write_sectors(text, sector)\r\nprint(f\"ID: {id}\")\r\nprint(f\"Text Written: {text_written}\")\r\n```\r\n**clear_sector.py**\r\nTo clear data written in a sector\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nwriter = BasicMFRC522()\r\nsector = 11\r\n\r\nid = writer.clear_sector(sector)\r\nprint(\"Cleared\")\r\n```\r\n\r\n**clear_sectors.py**\r\nTo clear data written in multiple sectors\r\n```py\r\nfrom mfrc522 import BasicMFRC522\r\n\r\nwriter = BasicMFRC522()\r\nsectors  = [11, 15]\r\n\r\nid = writer.clear_sectors(sectors)\r\nprint(\"Cleared\")\r\n```\r\n## Contributions\r\nContributions to the `mfrc522-python` library are welcome. To contribute, follow these steps:\r\n\r\n1.  Fork the repository on GitHub.\r\n2.  Create a new branch for your changes.\r\n3.  Make your changes and commit them.\r\n4.  Push your changes to your forked repository.\r\n5.  Submit a pull request to the main repository.\r\n\r\nThat's it! By submitting a pull request, you can contribute your changes to the `mfrc522-python` library.\r\n Provide a clear description of your changes in the pull request.\r\nIf you have any questions or need further assistance, feel free to open an issue.\r\n\r\n## License\r\nThis project is licensed under the GNU General Public License (GPL) version 3.0. You can find the full text of the license in the [LICENSE](LICENSE) file.\r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "The mfrc522-python library is used to interact with RFID readers that use the MFRC522 chip interfaced with a Raspberry Pi.",
    "version": "0.0.7",
    "project_urls": {
        "Bug Tracker": "https://github.com/1AdityaX/mfrc522-python/issues",
        "Homepage": "https://github.com/1AdityaX/mfrc522-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e010b5b40c9f5626d8438e9ff09794b3b6839de4aa2a7b4cd4cefdc85e3148e",
                "md5": "ff2762a7548bb7942936df8b9fd225d3",
                "sha256": "027d92127daf88cbf7ad4b3cd994b1d5e0e77f764e0c91c20154fc1d2f6aa413"
            },
            "downloads": -1,
            "filename": "mfrc522_python-0.0.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff2762a7548bb7942936df8b9fd225d3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 27226,
            "upload_time": "2024-02-01T08:19:54",
            "upload_time_iso_8601": "2024-02-01T08:19:54.050556Z",
            "url": "https://files.pythonhosted.org/packages/9e/01/0b5b40c9f5626d8438e9ff09794b3b6839de4aa2a7b4cd4cefdc85e3148e/mfrc522_python-0.0.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e2637a5ba63fcceea25f8d2a4101a648f97dfc4466b0615b78bd17977218abce",
                "md5": "840cbc388ea155c8e3d9bc6dde52ed6d",
                "sha256": "293758c4f6344a6c0f72fb0dbda72e3a003eeb8810cedefce97d26826ab20904"
            },
            "downloads": -1,
            "filename": "mfrc522-python-0.0.7.tar.gz",
            "has_sig": false,
            "md5_digest": "840cbc388ea155c8e3d9bc6dde52ed6d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 30021,
            "upload_time": "2024-02-01T08:19:56",
            "upload_time_iso_8601": "2024-02-01T08:19:56.199984Z",
            "url": "https://files.pythonhosted.org/packages/e2/63/7a5ba63fcceea25f8d2a4101a648f97dfc4466b0615b78bd17977218abce/mfrc522-python-0.0.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-01 08:19:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "1AdityaX",
    "github_project": "mfrc522-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mfrc522-python"
}
        
Elapsed time: 0.37492s