6

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.

Martin Thompson
  • 3,415
  • 10
  • 38
  • 62

2 Answers2

3

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"
    }
  }
}
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62
  • Thanks Martin, ended up using node-hid myself. I have noticed though when outputting the response from device.on('data') and converting to .toString() you get odd characters like squares etc. Did you have something similar? – Dev.W Sep 04 '18 at 07:57
  • 1
    @Dev.Wol I've added the function I used to extract the barcode. Hope it makes sense. I think I also used this.emit('barcode',decodedBarcode) as well , I was quite new to Nodejs when I first asked this question – Martin Thompson Sep 04 '18 at 08:18
  • Thanks Martin, I had to tweak slightly for my project but the concept works! Thanks for that. – Dev.W Sep 04 '18 at 08:53
  • Apologies last question! when you added node-hid to Electron and when you build for production did you get a nasty error with no direction? I believe its node-hid with electron as when I remove the node-hid package the build works fine. – Dev.W Sep 04 '18 at 10:03
  • No worries @Dev.Wol - it's tough going it alone! , yes I do remember having some issues. I am not in that "space" right now , but I have added my package.json.. ( I was building on windows ) I think that the electron-rebuild package solved my issues. I had some issues with node-gyp , but it it's bulding without then it's probably all good. – Martin Thompson Sep 05 '18 at 01:03
1

Your case might be a device dependent thing, but I recommend to try this. I managed to make barcode scanner gun to work with Electron just simply reading keypress-events on the renderer process. The device is not a Zebra in my case, but I wouldn't be surprised if this will work on your case too.

  let buffer = ''; // buffer for constructing the barcode from key presses

  document.addEventListener('keypress', event => {
    let data = buffer || '';
    if (event.key !== 'Enter') { // barcode ends with enter -key
      data += event.key;
      buffer = data;
    } else {
      buffer = '';
      console.log(data); // ready barcode ready for a use
    }
  });
Ville Venäläinen
  • 2,444
  • 1
  • 15
  • 11
  • you're just using the barcode scanner like a keyboard in that case - but you can't differentiate from the keyboard. If you use a proper driver then only the barcode data will come through – Martin Thompson Jun 28 '19 at 05:04
  • 1
    I used it as above since my scanner is without driver. I set a prefix to sort out scanned key events. Works well. – three Jul 08 '20 at 10:31