Search code examples
javascriptbufferbarcode-scanner

Javascript to turn Barcode scanner output of multiline buffer to array or json


Good Morning!

I'm trying to get a USB Barcode scan into a single array or even better yet a JSON object, but I can't figure out how to turn this multi-character buffer into an object.

Below is the Raw HID stream coming from the Scan (Zebra LS2208)

<Buffer 00 00 24 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 23 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 22 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 24 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 22 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 23 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 26 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 20 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 23 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 24 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>

And when I turn it into characters, it's outputted like so:

7

6

5

7

5

6

9

3

1

2

6

7

Now these are the numbers in the UPC, but I've been having a hard time turn this into an array or object because each of these new lines (including the spaces) are seen as a new array.

What I ultimately want this turned into is either: [7,6,5,7,5,6,9,3,1,2,6,7] OR

{
 "upc": "765756931267"
}

This is the very basic code I'm using:

var KeyboardCharacters = require('node-hid-stream').KeyboardCharacters;
var characters = new KeyboardCharacters({ vendorId: 1504, productId: 4608 });

characters.on("data", function(data) {
 console.log(data);
});

Is anyone able to offer any insight? Would love the help!


Solution

  • In case someone else comes around and has a similar problem, I found the solution to my problem.

    With the USB Scanner I was using, a Zebra LS2208, it comes with multiple scanning modes which can be set by scanning a bar code. Over the last day, I've been using it as USB Keyboard (HID) which means each digit in the UPC was a different Key Down/Key Up, which this morning dawned on me.

    I scanned the bar code which switched it's scanning mode to IBM HAND-HELD USB and attempted the scan, but I received an error in the code. I didn't change any code and the path of the device was the same, but to double check the device settings, I used the following, which is part of node-hid to check my device settings:

    var devices = HID.devices();
    console.log(devices);
    

    I noticed that the vendorId was the same, but the productId had changed... I updated the ID in the code, re-scanned and voila, one Buffer was outputted and a simple toString() changed it to the correct numbers.

    Beyond the Buffer fix, I also stopped using the NPM package which inherited node-hid and I just used it myself and wrote whatever else I needed.

    Code fragment of the fix:

    // Use the serialport module to get scanner  data
    var HID = require('node-hid');
    
    // Set the device we're using for Scanning Barcodes
    var device = new HID.HID('/dev/hidraw0');
    
    // Register a scanned UPC and start the rest of the program process
    device.on('data', function(data) {
        console.log('Scan prompted');
        console.log('UPC = ' + data.toString());
    });