Hardware & setup¶
Supported readers¶
mifarepy talks to readers made by GIGA-TMS Inc. (sold under the PROMAG brand) that implement the GNetPlus® protocol and its MIFARE command set:
| Reader | Form factor | Connection |
|---|---|---|
| PCR310U | Desktop reader | USB, via a built-in Prolific PL2303 USB-serial bridge |
| MF5 | OEM module | RS-232 / TTL serial |
| MF10 | OEM module | RS-232 / TTL serial |
All of them run at 13.56 MHz (ISO/IEC 14443 Type A). mifarepy's API covers
MIFARE Classic 1K and 4K cards. The hardware can also detect MIFARE
Ultralight cards. get_second_sn() reads the Ultralight's second serial-number
part, but there is no Ultralight read/write API yet.
Serial settings are fixed by the reader firmware: 19200 baud, 8 data bits,
no parity, 1 stop bit. MifareReader uses these by default.
Find the reader automatically¶
from mifarepy import MifareReader
with MifareReader.auto() as reader:
print('Reader on', reader.port, '-', reader.get_version())
auto() looks for the Prolific PL2303 USB-serial chip (USB ID 067b:2303
and later revisions) and sends a POLLING command to confirm the device is a
GNetPlus reader. To see which ports it considers:
from mifarepy.discovery import find_ports
for port in find_ports(include_all=True):
print(port.device, port.description, '<- reader?' if port.likely_reader else '')
If nothing is found, ReaderNotFoundError lists every serial port on the
computer. For readers on an RS-232 adapter with another chip, use
MifareReader.auto(include_all=True); it probes every serial port, so only use
it when no other serial devices would mind a few stray bytes. Otherwise pass
the port name, as below.
Find the serial port¶
Plug the reader in and check the kernel log:
usb 1-1: New USB device found, idVendor=067b, idProduct=2303
pl2303 1-1:1.0: pl2303 converter detected
usb 1-1: pl2303 converter now attached to ttyUSB0
The reader is at /dev/ttyUSB0. You can also list ports from Python:
Open Device Manager → Ports (COM & LPT). The reader shows up as Prolific USB-to-Serial Comm Port (COM3) or similar. Use that name:
If it shows a yellow warning icon, install the current Prolific PL2303 driver from Windows Update or the Prolific website.
Permissions (Linux)¶
Serial devices belong to the dialout group (uucp on Arch). Add your user
once, then log out and back in:
Stable device name (optional)¶
/dev/ttyUSB0 can change when you plug in other USB-serial devices. A udev
rule gives the reader a fixed name:
SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SYMLINK+="promag", GROUP="dialout", MODE="0660"
Now use MifareReader('/dev/promag').
Note
Many USB-serial adapters use the same PL2303 chip. If you have several,
match on ATTRS{serial} as well.
Reading range and placement¶
- Hold the card flat against the reader. The usable range is a few centimetres.
- Metal surfaces and phones next to the card reduce the range.
- Several cards in the field at once are fine. See example 08.
Troubleshooting¶
| Symptom | Likely cause | Fix |
|---|---|---|
ReaderConnectionError: Unable to open port |
Wrong port name, port busy, or no permission | The message ends with a hint. Use MifareReader.auto(), or check the port as above. On Linux, join the dialout group. |
ReaderNotFoundError from auto() |
No PL2303 reader answered | Check the cable, try auto(include_all=True), or pass the port name. |
InvalidMessage / timeouts on every call |
Another program has the port open, or the wrong baud rate | Close other serial tools and keep the default 19200 baud |
NoCardError: No tag in field (NAK 0x1F) |
No card on the reader | Place the card flat on the reader, or wait with wait_for_card() |
AuthenticationError (NAK 0x09; 0x04 on some readers) |
Wrong key or key type for that sector | Check the key and Key A/B. See the primer. |
NotAuthenticatedError (NAK 0x0C read, 0x0A write) |
Read/write before authenticating, or a different sector | Call authenticate_sector() for the sector you access |
NoCardError (NAK 0x1F) with the card on the reader |
An earlier authentication or block command failed, so the card went back to idle | Call authenticate_sector() again |
| Card stops responding after an aborted transaction | Card state machine is stuck | Cycle the RF field with rf_power(False) / rf_power(True). See example 09. |
| Spurious events after reconnecting | Auto mode was left on | close() turns auto mode off. Call set_auto_mode(False) if you crashed before closing. |
| Anything else unexpected | Turn on frame tracing with enable_wire_trace() and look at the exact frames and NAK codes. |
The full list of reader error codes is in the protocol reference.