Reading Tag information via Serial in Python


I’m another noob floundering in what is no doubt an extremely simple problem - using an NFC tag open and play an MP3 on my computer. I was hoping I might find some suggestions here.

Setup is as follows:

  • Arduino Uno R3
  • Adafruit PN532 NFC Shield
  • Computer OS Windows XPx64 running Python 2.7 & Pyserial 2.7

Each NFC tag has its own serial number. My plan was to use this unique serial number as the ‘key’ to play a particular song on the computer. Below is someone’s nice Arduino code I’ve butchered:

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);
NfcAdapter nfc = NfcAdapter(pn532_i2c);

void setup(void) {

void loop(void) {

    if (nfc.tagPresent())
        NfcTag tag =;

This is how it appears when it runs & scans a tag (note I’m trying to use the serial number which is in Bold Red):

Found chip PN532
Firmware ver. 1.6
NFC Tag - Mifare Classic
UID A5 27 9F 2A

NDEF Message 1 record, 15 bytes
NDEF Record etc etc etc

Python Code:

import os
import mp3play
import serial
import time

ser = serial.Serial('com4', 9600, timeout=0)
clip = mp3play.load (r'C:\file\01.mp3')

while 1:
 line = ser.readline()
 print ser.readline()
 x =

 if x == "UID A5 27 9F 2A":

But of course, it doesn’t work.

Interestingly if I make the following modifications, it does work:

while 1:
 print ser.readline()
 x =

 if x == "U":

but this isn’t very useful as I’d like to use multiple cards (each cards serial starts with U)
Any help you might be able to offer would be greatly appreciated!

But of course, it doesn't work.

I wouldn't expect it to.

 line = ser.readline()
 print ser.readline()

Read and store one line. Read and print another line.

 x =

Then, take a nap, and then read one character.

 if x == "UID A5 27 9F 2A":

Then, expect that one character to, somehow, magically, be equal to "UID A5 27 9F 2A".

I'd be studying the readline() function, and understanding what makes one [u]line[/u] different from another [u]line[/u].

I'd also be thinking about replacing the call to tag.print() with something that printed JUST the tag ID without all that other crap.