Skip to content

Instantly share code, notes, and snippets.

@basham
Last active May 1, 2024 12:56
Show Gist options
  • Save basham/806605 to your computer and use it in GitHub Desktop.
Save basham/806605 to your computer and use it in GitHub Desktop.

Revisions

  1. basham revised this gist Oct 4, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,10 @@ 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.
  2. basham revised this gist Feb 2, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -69,7 +69,7 @@ var ids = [];

    // ARGUMENT 2:
    // Simplifies restruction of stream if one bit comes at a time.
    // However, I don't know if or how it affects performance with this setting.
    // However, I don't know if or how this setting affects performance.
    fs.createReadStream('/dev/cu.usbserial-A600exqM', { bufferSize: 1 })

    .on('open', function(fd) {
  3. basham revised this gist Feb 2, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -69,6 +69,7 @@ var ids = [];

    // ARGUMENT 2:
    // Simplifies restruction of stream if one bit comes at a time.
    // However, I don't know if or how it affects performance with this setting.
    fs.createReadStream('/dev/cu.usbserial-A600exqM', { bufferSize: 1 })

    .on('open', function(fd) {
  4. basham revised this gist Feb 2, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /*
    ABOUT
    -----
    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
    @@ -58,6 +58,7 @@ 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:
  5. basham revised this gist Feb 2, 2011. 1 changed file with 48 additions and 16 deletions.
    64 changes: 48 additions & 16 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,26 @@ ABOUT
    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
    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
    02/01/2011
    https://github.com/basham
    CONTRIBUTORS
    ------------
    Elliott Cable
    https://github.com/elliottcable
    SOFTWARE
    --------
    @@ -38,31 +52,49 @@ EXAMPLE RFID IDs
    */

    // NodeJS includes
    var sys = require('sys');
    var fs = require('fs');

    // This will be unique to your hardware.
    // Stores the RFID id as it reconstructs from the stream.
    var id = '';
    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.
    var serial = '/dev/cu.usbserial-A600exqM';

    // ARGUMENT 2:
    // Simplifies restruction of stream if one bit comes at a time.
    var f = fs.createReadStream(serial, { bufferSize: 1 });
    // Stores the RFID id as it reconstructs from the stream.
    var id = '';
    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.');
    })

    f.addListener('open', function(fd) {
    sys.puts('SCAN RFID TAGS');
    sys.puts('--------------');
    });
    .on('error', function(error) {
    sys.debug(error);
    })

    f.addListener('data', function(chunk) {
    var c = chunk.toString('ascii').match(/\w*/)[0]; // Only keep hex chars
    if ( c == '' ) { // Found non-hex char
    if ( id != '' ) // The ID isn't blank
    sys.puts(id); // The complete ID is reconstructed
    .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 += c; // Concat hex chars to the forming ID
    id += chunk; // Concat hex chars to the forming ID
    });
  6. basham revised this gist Feb 1, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -61,8 +61,8 @@ f.addListener('data', function(chunk) {
    if ( c == '' ) { // Found non-hex char
    if ( id != '' ) // The ID isn't blank
    sys.puts(id); // The complete ID is reconstructed
    id = ''; // Prepare for the next ID chunks
    id = ''; // Prepare for the next ID read
    return;
    }
    id += c; // Add to the ID
    id += c; // Concat hex chars to the forming ID
    });
  7. basham revised this gist Feb 1, 2011. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,14 @@ 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
    */

    var sys = require('sys');
    @@ -40,25 +48,21 @@ var fs = require('fs');
    var serial = '/dev/cu.usbserial-A600exqM';
    // Simplifies restruction of stream if one bit comes at a time.
    var f = fs.createReadStream(serial, { bufferSize: 1 });

    // Stores the RFID id as it reconstructs from the stream.
    var id = '';

    f.addListener('open', function(fd) {
    f.addListener('open', function(fd) {
    sys.puts('SCAN RFID TAGS');
    sys.puts('--------------');
    });

    f.addListener('data', function(chunk) {

    f.addListener('data', function(chunk) {
    var c = chunk.toString('ascii').match(/\w*/)[0]; // Only keep hex chars

    if ( c == '' ) { // Found non-hex char
    if ( id != '' ) // The ID isn't blank
    sys.puts(id);
    sys.puts(id); // The complete ID is reconstructed
    id = ''; // Prepare for the next ID chunks
    return;
    }

    id += c; // Add to the ID

    });
  8. basham revised this gist Feb 1, 2011. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -32,15 +32,21 @@ http://www.trossenrobotics.com/store/p/3620-Blue-Key-Fob.aspx

    var sys = require('sys');
    var fs = require('fs');

    // This will be 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.
    var serial = '/dev/cu.usbserial-A600exqM';
    // Simplifies restruction of stream if one bit comes at a time.
    var f = fs.createReadStream('/dev/cu.usbserial-A600exqM', { bufferSize: 1 });
    var f = fs.createReadStream(serial, { bufferSize: 1 });

    var id = '';

    f.addListener('open', function(fd) {
    sys.puts('SCAN RFID TAGS');
    sys.puts('--------------');
    })
    });

    f.addListener('data', function(chunk) {

  9. basham created this gist Feb 1, 2011.
    58 changes: 58 additions & 0 deletions nodejs-rfid.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    /*
    ABOUT
    -----
    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
    AUTHOR
    ------
    Chris Basham
    @chrisbasham
    http://bash.am
    02/01/2011
    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
    */

    var sys = require('sys');
    var fs = require('fs');
    // Simplifies restruction of stream if one bit comes at a time.
    var f = fs.createReadStream('/dev/cu.usbserial-A600exqM', { bufferSize: 1 });

    var id = '';

    f.addListener('open', function(fd) {
    sys.puts('SCAN RFID TAGS');
    sys.puts('--------------');
    })

    f.addListener('data', function(chunk) {

    var c = chunk.toString('ascii').match(/\w*/)[0]; // Only keep hex chars

    if ( c == '' ) { // Found non-hex char
    if ( id != '' ) // The ID isn't blank
    sys.puts(id);
    id = ''; // Prepare for the next ID chunks
    return;
    }

    id += c; // Add to the ID

    });