How to connect barcode scanner in electron ( Node.js )

Martin Thompson picture Martin Thompson · Nov 12, 2017 · Viewed 7.1k times · Source

I want to connect a standard Zebra scanner to an electron ( node.js windows ) application. I want to do it properly , not just set up as a keyboard input.

I need some direction - maybe something has been written ( no commercial products please )

Thanks.

Answer

Martin Thompson picture Martin Thompson · Aug 23, 2018

FYI , to get this going I used https://www.npmjs.com/package/node-hid

Also , Because I wanted to broadcast it over the network , I used https://www.npmjs.com/package/isomorphic-ws to communicate between a browser and the barcode scanner.

Additionally , capturing the barcode is a bit of a challenge. I used the following to get the barcode. It removes all non-word ascii characters , and the start of the returned string. Feels like a bit of a hack , but it works in most cases should work unless you want to pass non-standard characters in your barcodes. Please let me know if you find a better way!

function receiveBarcode(data){
    const barcode = data.toString('ascii').replace(/\W/g, '')
    const decodedBarcode = barcode.substring(2,barcode.length-1)
    return decodedBarcode
}   

I am recalling this part from memory - I can't remember exactly how I built it.. but this is my package.json . I think electron builder solved some of my build issues. https://github.com/electron-userland/electron-builder

{
  "name": "zimpla.device.manager",
  "version": "1.0.0",
  "main": "main.js",
  "dependencies": {
    "electron-log": "^2.2.14",
    "electron-settings": "^3.1.4",
    "moment": "^2.21.0",
    "node-hid": "^0.7.2",
    "serialport": "^6.1.1",
    "ws": "^5.0.0"
  },
  "scripts": {
    "start": "electron .",
    "debug": "electron --inspect=5858 .",
    "install": "electron-rebuild",
    "postinstall": "electron-rebuild --force -m . -w node-hid && electron-builder install-app-deps",
    "pack": "electron-builder --dir",
    "dist": "build"
  },
  "devDependencies": {
    "electron": "latest",
    "electron-builder": "^20.4.1",
    "electron-rebuild": "^1.7.3"
  },
  "build": {
    "appId": "zimpla.device.manager",
    "win": {
      "target": "nsis"
    }
  }
}