Skip to content

Instantly share code, notes, and snippets.

@andrewn
Created May 15, 2015 09:11
Show Gist options
  • Save andrewn/4ef79d6d99dd8bc88b13 to your computer and use it in GitHub Desktop.
Save andrewn/4ef79d6d99dd8bc88b13 to your computer and use it in GitHub Desktop.
RFID reader using node.js and USB-UART bridge

Wiring

The wiring from the USB-UART bridge and the RFID reader

RFID Reader view:

   .-----------------------------.
   | [1][2]      [5][4][3][2][1] |
   |  J3              J1         |
   |                             |
   |           RDM880 (top view) |
   |                             |
   |                  J2         |
   |             [5][4][3][2][1] |
   .-----------------------------.
RFID Breakout pin USB-UART pin
J2 5 5V
J2 4 GND
J2 3 TXD
J2 2 RXD

Installation

npm install serialport debug

Run

Set the SERIAL_PORT env variable to the port your RFID reader is connected to:

SERIAL_PATH=/dev/tty.SLAB_USBtoUART node rfid.js

>> Connected

Placing an RFID tag into the reader's field will print out its serial number. Removing the tag will print "Removed". The MFRC500 chip doesn't allow you to select which card to read if there are more than one. It will just print the first.

More info

Info: http://www.seeedstudio.com/wiki/index.php?title=13.56Mhz_RFID_module_-_IOS/IEC_14443_type_a Protocol information: http://neophob.com/files/rfid/PROTOCOL-821-880%20_2_.pdf

// Read serial number of any RFID tags
// connected over serial to a reader
// The path to the serial port
// the card is connected to
var serialPath = process.env.SERIAL_PORT || '/dev/tty.SLAB_USBtoUART';
var debug = require('debug')('rfid:debug'),
log = console.log,
serialport = require('serialport');
var Cmd = {
start: 0xAA
, ok : 0x00
, end : 0xBB
};
var serial = new serialport.SerialPort(serialPath, {
baudrate: 9600,
parser: serialport.parsers.byteDelimiter(Cmd.end)
});
// MF_GET_SNR (0x25) command
// http://neophob.com/files/rfid/PROTOCOL-821-880%20_2_.pdf
var query = [Cmd.start, 0x00, 0x03, 0x25, 0x26, 0x00, 0x00, Cmd.end];
function hex(num) {
return (num < 10 ) ? '0' + num : num.toString(16);
}
function hexString(array) {
return array
.map(hex)
.join('');
}
var lastTagId = null;
// Prints the ID and keeps a reference
// to it since we're polling the reader
// for tags and don't want to repeatedly
// print the same id
function done(id) {
if (id && lastTagId != id) {
lastTagId = id;
log('id', id);
} else if (id == null && lastTagId != null) {
lastTagId = null;
log('Removed');
}
setTimeout(doQuery, 0);
}
// Parse a response from the reader,
// an array of bytes
function parseResponse(b) {
var status = b[3], // query success
numTags = b[4], // how many tags found
id = [];
if (status == Cmd.ok) {
debug('read: ok', b);
debug(numTags);
switch(numTags) {
case 0x00: debug('one tag readable');
break;
case 0x01: debug('at least 2 tags readable');
break;
}
id = b.slice(5, 9); // 4 bit device serial number
debug('ID: ' + id);
id = hexString(id); // turn the 4 bits into a hex string
done(id);
} else {
done();
}
}
// Write a query to the reader to
// find any tags within it's range
// This triggers data to be sent
// back over the serial port
function doQuery () {
serial.write(query);
}
// On serial port open set up listener
// for incoming data and trigger
// done() to kick off polling
serial.on('open', function () {
log('Connected');
serial.on('data', parseResponse);
done();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment