-
-
Save basham/806605 to your computer and use it in GitHub Desktop.
/* | |
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 | |
}); |
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.
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:
var sys = require('sys');
var ids = [];
require('fs').createReadStream("/dev/cu.usbserial-A600exqM", {bufferSize: 1})
.on('open', function(fd){
sys.puts("Begin scanning tags!");
})
.on('end', function(fd){
fd.close();
})
.on('data', function(chunk){
chunk = chunk.toString('ascii').match(/\w*/)[0]
if (chunk.length !== 0) {
if (ids.length !== 0) { ids[ids.length - 1] += chunk }
else { ids[ids.length - 1] = chunk };
} else if (typeof(ids[ids.length - 1]) !== 'undefined' && ids[ids.length - 1].length !== 0) {
sys.puts(ids[ids.length - 1]);
ids.push('');
}
});
(The following changes were made:
- Explicitly declaring all semicolons
- Separating
.on()
calls with whitespace - Ensuring no function’s content appeared on the same line as the
function
keyword - Ensuring we didn’t use shortcuts such as
if(foo.length)
instead ofif(foo.length !== 0)
- Lining up code elements by standard indentation rules instead of apportioned alignment
- Judicious use of whitespace
- Post-commas instead of pre-commas
- Avoiding continuing statements on multiple lines, except where it massively simplifies the code, as in the continued
.on().on().on()
chain - Utilizing shortcut literals instead of explicit constructors, such as
''
instead ofnew String()
… I hope that mostly covers what others consider to be “quality” code!)
I should also add that a bit of that is psuedo-code; I’m not sure if there’s a close()
method for whatever type is passed to the 'open'
event; Node.js’s API has changed quite a bit since I last looked at it in-depth. The basic concepts are valid, but be sure to test thorougly or even start from scratch with your own code and simply keep the lessons my code teaches in mind (-;
Neat to see your own incorporation of my changes! (-:
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.
Hi,
it's not works for me, need help please.
I have the same hardware: Sparkfun RFID USB Reader, RFID Reader ID-12 and EM4102 125 kHz RFID Tags.
I'm logged in with root, I can open a stream from "dev/ttyUSB0", but when I scan a tag, I don't have output.
Chmod of ttyUSB0 is like this:
crw-rw---- 1 root dialout 188, 0 2012-11-05 12:25 ttyUSB0
Here my script:
var fs = require('fs')
fs.createReadStream("/dev/ttyUSB0", {
bufferSize: 1
})
.on('open', function(fd){
console.log('Stream read');
})
.on('end', function(){
console.log('Stream end');
})
.on('data', function(data){
console.log('Stream data');
})
.on('error', function(error) {
console.log('Stream error');
})
Where I disconnect the Sparkfun RFID USB Reader, I receive the "end" event, so ttyUSB0 is file.
I use a virtual machine with VMware player.
How can I fix that, I don't know, I'm confused about that
Thank you for your help.
[...] When I disconnect the Sparkfun RFID USB Reader, I receive the "end" event, so "/dev/ttyUSB0" is the right file. [...]
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>
What did it mean ? Thank you
I know this code is insanely old but how would you go about writing data TO the tag ?
@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.
i have the issue with retrieving the USB serial streams . could you anyone please
help me ?
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.