2

I'm working on an Intel Edison with Yoctoo 3.10, I have a barcode scanner on /dev/hidraw0 and I want to use the exact lines that are output when I run hexdump /dev/hidraw0 as the input for a program written in node js.

The node js program is the following:

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

rl.on('line', function(line){
    console.log(line);
})

I have tried piping it normally:

hexdump /dev/hidraw0 | node program.js

But I get nothing, I think it has to do with the fact that hexdump doesn't write \n so the buffer doesn't write it's content.

I have also tried opening /dev/hidraw0 as a file, like this:

var fs = require('fs');
fs.open('/dev/hidraw0', 'r', function(status, fd) {
    if (status) {
        console.log(status.message);
        return;
    }
    var buffer = new Buffer(100);
    fs.read(fd, buffer, 0, 100, 0, function(err, num) {
        console.log(buffer.toString('hex'));
    });
}); 

And using some hex dumpers like hexy but in this case I get some hex lines but not the same ones as with hexdump, which are the ones I need.

Just using hexdump /dev/hidraw0 gives me the following (when I use a card)

0000000 0000 0020 0000 0000 0000 0000 0000 0000
0000010 0000 0020 0000 0000 0000 001f 0000 0000
0000020 0000 0027 0000 0000 0000 0026 0000 0000
0000030 0000 0025 0000 0000 0000 0000 0000 0000
0000040 0000 0025 0000 0000 0000 0024 0000 0000
0000050 0000 0021 0000 0000 0000 0025 0000 0000
0000060 0000 0028 0000 0000 0000 0000 0000 0000

1 Answers1

0

The following works for me:

process.stdin.setEncoding('utf8');
process.stdin.on('readable', function () {
  var chunk = process.stdin.read();
  if (chunk !== null) {
    console.log(chunk);
  }
});

Running:

sudo hexdump /dev/hidraw0 | node this-script.js

Sample output (when I move a wireless mouse):

0000000 0120 0002 fd00 ffcf 0000 0000 0000 2000
0000010 0201 0000 cffc 00ff 0000 0000 0000 0120
...
Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60
  • Still nothing, maybe it helps to know that running lsusb the device is recognized as the following: – Nacho Rasche Oct 13 '16 at 16:35
  • Bus 001 Device 002: ID 13ba:0018 PCPlay Barcode PCP-BCG4209 – Nacho Rasche Oct 13 '16 at 16:35
  • @NachoRasche, what is displayed when you simply run `hexdump /dev/hidraw0`? I mean the state of the HID device should change before it shows something. Are you sure the HID device state changes (there is something to print)? – Ruslan Osmanov Oct 13 '16 at 17:15
  • yes, just running hexdump /dev/hidraw0 works fine, when I use one of the cards it displays some hex lines (the ones I edited on the question) the problem is when I try to redirect this output to another program, then it displays nothing. I have tried writing the result of cat /dev/hidraw0 to a file (cat /dev/hidraw0 > out), then hexdump out, that also works fine, even using hexdump out | node script.js works, it only doesn't when I directly redirect it using hexdump /dev/hidraw0 | node script.js (which is what I want because I want it to work in real time) – Nacho Rasche Oct 13 '16 at 18:21