/* | |
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 | |
}); |
This comment has been minimized.
This comment has been minimized.
Thanks. That's an interesting approach, building the forming ID in the array rather than growing it in a temporary variable. The idioms are nice as it slims the code and reduces semicolons. I just question how it affects readability. |
This comment has been minimized.
This comment has been minimized.
ELLIOTTCABLE
commented
Feb 2, 2011
The style is my own; I perpetrate it across all of my projects. It’s quite different from the usual pop-culture JavaScript (or even, at this point, Node.js) styles, but then again, I don’t really care whether most of those types of people can read my code. Those who need to be able to read it are far and away used to it by now; and most would agree that the style is more concise, more readable, and downright more beautiful than the awkward and splayed syntax that most JavaScript developers use. Sorry, I didn’t mean to get into writing a treatise on my style decisions! (-: The point is, that code works however you style it. If you prefer, this works just as well:
(The following changes were made:
… I hope that mostly covers what others consider to be “quality” code!) |
This comment has been minimized.
This comment has been minimized.
ELLIOTTCABLE
commented
Feb 2, 2011
I should also add that a bit of that is psuedo-code; I’m not sure if there’s a |
This comment has been minimized.
This comment has been minimized.
ELLIOTTCABLE
commented
Feb 2, 2011
Neat to see your own incorporation of my changes! (-: |
This comment has been minimized.
This comment has been minimized.
I appreciate you sticking to your style and explaining your rationale with passion. Even though I'm not fully adopting your sly coding aesthetic, there is a lot I like about it. I've added you as a contributor. |
This comment has been minimized.
This comment has been minimized.
fperreault
commented
Nov 5, 2012
Hi, Chmod of ttyUSB0 is like this: Here my script: Where I disconnect the Sparkfun RFID USB Reader, I receive the "end" event, so ttyUSB0 is file. |
This comment has been minimized.
This comment has been minimized.
fperreault
commented
Nov 5, 2012
[...] When I disconnect the Sparkfun RFID USB Reader, I receive the "end" event, so "/dev/ttyUSB0" is the right file. [...] |
This comment has been minimized.
This comment has been minimized.
fperreault
commented
Nov 5, 2012
It's works now. I'm rolling back to v0.8 of nodejs. Now, my problem is that I get a empty buffer when I scan a tag like this: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00> |
This comment has been minimized.
This comment has been minimized.
freak4pc
commented
Jul 22, 2014
I know this code is insanely old but how would you go about writing data TO the tag ? |
This comment has been minimized.
This comment has been minimized.
@freak4pc I don't know how you'd write to a RFID tag. Never tried, and I don't have such hardware. The tags I used for this project are read-only. Would certainly be interested if you figure out some solution. |
This comment has been minimized.
This comment has been minimized.
lokesh-oruganti
commented
May 24, 2019
•
i have the issue with retrieving the USB serial streams . could you anyone please |
This comment has been minimized.
ELLIOTTCABLE commentedFeb 2, 2011
I refactored this slightly. I haven’t got the hardware to test, but the functionality should be nearly identical. Just used a few more common JavaScript and Node.js idioms.