How to Use Node RED with Barcode Module on Windows 10

Node-RED is a flow-based programming tool built-on Node.js. It provides a browser-based editor that helps users to create awesome applications by wiring different nodes. This article shares my experience of building a barcode node with Dynamsoft Barcode Reader on Windows.

Installing and Running Node RED on Windows 10

Install Node-RED:

sudo npm install -g --unsafe-perm node-red

On Windows, the default installation directory is %userprofile%\ .node-red.

Run Node-RED:

node-red

It failed when I started Node-RED for the first time. Here is the error message:

Welcome to Node-RED
===================

5 Nov 14:29:12 - [info] Node-RED version: v0.19.5
5 Nov 14:29:12 - [info] Node.js  version: v8.11.3
5 Nov 14:29:12 - [info] Windows_NT 10.0.17763 x64 LE
5 Nov 14:29:13 - [info] Loading palette nodes
5 Nov 14:29:14 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
5 Nov 14:29:15 - [warn] ------------------------------------------------------
5 Nov 14:29:16 - [warn] [node-red/tail] Not currently supported on Windows.
5 Nov 14:29:16 - [warn] ------------------------------------------------------
5 Nov 14:29:16 - [info] Settings file  : \Users\xiao\.node-red\settings.js
5 Nov 14:29:16 - [info] Context store  : 'default' [module=memory]
5 Nov 14:29:16 - [info] User directory : \Users\xiao\.node-red
5 Nov 14:29:16 - [warn] Projects disabled : editorTheme.projects.enabled=false
5 Nov 14:29:16 - [info] Flows file     : \Users\xiao\.node-red\flows_DESKTOP-3PCHU10.json
5 Nov 14:29:16 - [info] Creating new flow file
5 Nov 14:29:16 - [info] Starting flows
5 Nov 14:29:16 - [info] Started flows
5 Nov 14:29:16 - [error] Uncaught Exception:
5 Nov 14:29:16 - [error] Error: listen EACCES 0.0.0.0:1880
    at Object._errnoException (util.js:992:11)
    at _exceptionWithHostPort (util.js:1014:20)
    at Server.setupListenHandle [as _listen2] (net.js:1338:19)
    at listenInCluster (net.js:1396:12)
    at doListen (net.js:1505:7)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

It seems something wrong with the default port 1880. We can use Resource Monitor to check the port usage.

windows resource manager

I did not find 1880 in the port list. I tried a port that is in use and got a different error message:

5 Nov 14:40:00 - [info] Node-RED version: v0.19.5
5 Nov 14:40:00 - [info] Node.js  version: v8.11.3
5 Nov 14:40:00 - [info] Windows_NT 10.0.17763 x64 LE
5 Nov 14:40:01 - [info] Loading palette nodes
5 Nov 14:40:01 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
5 Nov 14:40:03 - [warn] ------------------------------------------------------
5 Nov 14:40:03 - [warn] [node-red/tail] Not currently supported on Windows.
5 Nov 14:40:03 - [warn] ------------------------------------------------------
5 Nov 14:40:03 - [info] Settings file  : \Users\xiao\.node-red\settings.js
5 Nov 14:40:03 - [info] Context store  : 'default' [module=memory]
5 Nov 14:40:03 - [info] User directory : \Users\xiao\.node-red
5 Nov 14:40:03 - [warn] Projects disabled : editorTheme.projects.enabled=false
5 Nov 14:40:03 - [info] Flows file     : \Users\xiao\.node-red\flows_DESKTOP-3PCHU10.json
5 Nov 14:40:03 - [info] Creating new flow file
5 Nov 14:40:03 - [info] Starting flows
5 Nov 14:40:03 - [info] Started flows
5 Nov 14:40:03 - [error] Unable to listen on http://127.0.0.1:1551/
5 Nov 14:40:03 - [error] Error: port in use

The workaround is to change the port number in %userprofile%\.node-red\settings.js. For example, we can change the default port number to 18800 and then it will work:

node red

Building Barcode Node

Let’s create a barcode node with Dynamsoft Barcode Reader.

Use npm command to initialize a Node RED project:

npm init barcode

Add the node-red section to package.json:

"node-red": {
    "nodes": {
      "barcode": "barcode.js"
    }
  },

Install Dynamsoft Barcode Reader:

npm install dbr --save

Create barcode.js:

module.exports = function(RED) {
  var dbr = require('dbr');
  var barcodeTypes = 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000; // 1D, PDF417, QRCODE, DataMatrix, Aztec Code

  function BarcodeNode(config) {
    RED.nodes.createNode(this, config);
    this.license = config.license;
    this.template = config.template;
    var node = this;
    node.on('input', function(msg) {
      if (msg.filename) {
        dbr.initLicense(node.license);
        dbr.decodeFileAsync(msg.filename, barcodeTypes, function(err, results) {
          msg.payload = results;
          node.send(msg);
        }, node.template);
      } else {
        msg.payload = msg.payload.toLowerCase();
        node.send(msg);
      }
    });
  }
  RED.nodes.registerType('barcode', BarcodeNode);
}

When receiving the input event, we can call the barcode decoding method by setting license and parameter templates.

Create barcode.html:

<script type="text/javascript">
    RED.nodes.registerType('barcode',{
        category: 'Dynamsoft',
        color: '#a6bbcf',
        defaults: {
            name: {value:""},
            license: {value:""},
            template: {value: ""}
        },
        inputs:1,
        outputs:1,
        icon: "function.png",
        label: function() {
            return this.name||"barcode";
        }
    });
</script>

<script type="text/x-red" data-template-name="barcode">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Barcode Reader">
    </div>
    <div class="form-row">
        <label for="node-input-license"><i class="icon-tag"></i> License</label>
        <input type="text" id="node-input-license" placeholder="https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx">
    </div>
    <div class="form-row">
        <label for="node-input-template"><i class="icon-tag"></i> Template</label>
        <input type="text" id="node-input-template" placeholder="https://www.dynamsoft.com/help/Barcode-Reader/devguide/Template/TemplateSettingsList.html">
    </div>
</script>

<script type="text/x-red" data-help-name="barcode">
    <p>A simple node that read barcodes from image files.</p>
</script>

Install and test the project:

cd %userprofile%\.node-red
npm install <node-red barcode project>
node-red

Open http://127.0.0.1:18800/ in your web browser. Add inject nodefile nodebarcode node and debug node. You can set the image path in the file node:

node red debug

Click barcode node to set a valid license and barcode parameter template:

node red barcode license

Here is an example of a template:

{
  "ImageParameter": {
    "Name": "Custom_143301_827",
    "BarcodeFormatIds": [
      "OneD"
    ]
  }
}

If you do not set a template, the default template will be used.

Run the app:

node red barcode results

If there is something wrong with dbr.node(compatibility issue caused by Node.js version), you can use the source code to build it by yourself.

It everything works well, we can publish the module:

cd <node red barcode project>
npm publish

Use the module:

cd %userprofile%\.node-red
npm install node-red-contrib-barcode --save

References

https://nodered.org/docs/

Source Code

https://github.com/yushulx/node-red-contrib-barcode