Skip to content

Instantly share code, notes, and snippets.

@m3doune
Forked from basham/nodejs-rfid.js
Created September 8, 2017 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m3doune/2ed78c35058d56a86865d36c920b350d to your computer and use it in GitHub Desktop.
Save m3doune/2ed78c35058d56a86865d36c920b350d to your computer and use it in GitHub Desktop.
Use NodeJS to read RFID ids through the USB serial stream.
/*
DESCRIPTION
-----------
Use NodeJS to read RFID ids through the USB serial stream. Code derived from this forum:
http://groups.google.com/group/nodejs/browse_thread/thread/e2b071b6a70a6eb1/086ec7fcb5036699
CODE REPOSITORY
---------------
https://gist.github.com/806605
VIDEO DEMO
----------
http://www.flickr.com/photos/chrisbasham/5408620216/
CHANGE LOG
----------
02/02/2011 - Refactored code and acknowledged contributors. Listening for other helpful stream Events.
02/01/2011 - Created initial code.
AUTHOR
------
Chris Basham
@chrisbasham
http://bash.am
https://github.com/basham
CONTRIBUTORS
------------
Elliott Cable
https://github.com/elliottcable
SOFTWARE
--------
NodeJS, version 0.2.6
http://nodejs.org
HARDWARE
--------
Sparkfun RFID USB Reader
http://www.sparkfun.com/products/8852
RFID Reader ID-20
http://www.sparkfun.com/products/8628
EM4102 125 kHz RFID Tags
http://www.trossenrobotics.com/store/p/3620-Blue-Key-Fob.aspx
EXAMPLE RFID IDs
----------------
2800F7D85D5A
3D00215B3671
31007E05450F
31007E195503
2800F77C5BF8
*/
// NodeJS includes
var sys = require('sys');
var fs = require('fs');
// Stores the RFID id as it reconstructs from the stream.
var id = '';
// List of all RFID ids read
var ids = [];
// ARGUMENT 1:
// Stream path, unique to your hardware.
// List your available USB serial streams via terminal and choose one:
// ls /dev | grep usb
// Had trouble with TTY, so used CU.
// ARGUMENT 2:
// Simplifies restruction of stream if one bit comes at a time.
// However, I don't know if or how this setting affects performance.
fs.createReadStream('/dev/cu.usbserial-A600exqM', { bufferSize: 1 })
.on('open', function(fd) {
sys.puts('Begin scanning RFID tags.');
})
.on('end', function() {
sys.puts('End of data stream.');
})
.on('close', function() {
sys.puts('Closing stream.');
})
.on('error', function(error) {
sys.debug(error);
})
.on('data', function(chunk) {
chunk = chunk.toString('ascii').match(/\w*/)[0]; // Only keep hex chars
if ( chunk.length == 0 ) { // Found non-hex char
if ( id.length > 0 ) { // The ID isn't blank
ids.push(id); // Store the completely reconstructed ID
sys.puts(id);
}
id = ''; // Prepare for the next ID read
return;
}
id += chunk; // Concat hex chars to the forming ID
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment