How do I use Node.js to communicate with my remote IO Module?

FAQs

Node.js is a JavaScript runtime library which is a widely used language for server side programming, particularly when IO is needed.

You can use Brainboxes npm (Node Package Manager) package called @brainboxes/io. Using this package in your Node.js application you can communicate with brainboxes remote io device (product code ED-xxx) and the Brainboxes BB-400.

Please note the Brainboxes Node.js package is still in beta please send feedback and feature requests to [email protected].

Node.js Code Sample

'use strict';

// ensure to install package using npm: npm install @brainboxes/io
const { EDDevice } = require('@brainboxes/io');

// create a new device
var ed588 = new EDDevice("192.168.0.33", 8, 8);

// listen to all responses
ed588.on('response', (data) => {
    console.log("received: " + data);
});

// listen for all connection events
ed588.on('connect', () => {
    console.log("Connected!");
});
// listen for all disconnection events, equivalent to node socket 'end' or 'closed'
ed588.on('disconnect', () => {
    console.log("Disconnected!");
    // could do some reconnecting here!
});

// listen for all error events
ed588.on('error', (err) => {
    console.log("Error: " + err);
});

// try to connect then perform some functions
ed588.connect().then( () => {

    // example functions:

    // send commands and receive associated responses using ASCII API
    // for more on ASCII API see: http://www.brainboxes.com/faq/items/ascii-protocol-and-commands

    // get io state
    // see: http://www.brainboxes.com/files/pages/support/faqs/docs/AsciiCommands/%40AA.pdf
    ed588.sendCommand("@01").then( response => console.log(`Current IO State ${response}`));

    // set output state to all closed
    // see: http://www.brainboxes.com/files/pages/support/faqs/docs/AsciiCommands/%40AA(Data).pdf
    ed588.sendCommand("@01FF").then( response => console.log(`Set successful: ${response == ">"}`));

    // or use brainboxes.io node API built in function
    // get DIN0 line count
    ed588.getDigitalInputLineCount(0).then( response => console.log(`DIN0 count: ${response}`) );

    ed588.getAllDigitalLineStates().then(response => console.log(`IO Line values as int array [${response.join(',')}]`));


    var newOutputLineState = [1,0,1,0,1,0,1,0];
    ed588.setAllDigitalOutputStates(newOutputLineState).then(response => {
        console.log(`successfully set digital outputs to [${newOutputLineState.join(',')}]`);
    })

    var i = 0;
    const intervalMS = 1000;
    // simple test function fires every interval
    setInterval( () => {
        // set output see:
        console.log("DateTime: "+Date())
        // rand either 0 or 1
        let newRandValue = Math.round(Math.random());
        let outputLineNumber = i % ed588.numOutputs;
        // set an output to a random value
        ed588.setDigitalOutputLineState(outputLineNumber, newRandValue );
        // read io state
        ed588.getAllDigitalLineStates().then(response => console.log(`IO Line values as int array [${response.join(',')}]`));
        // note the above 2 commands are asynchronous, so there is no guarantee that the setDigitalOutput command above will have taken effect before the getAllDigitalLineStates command is processed

        // to ensure the ordering of the above two commands is sequential and synchronous do the following:
        ed588.setDigitalOutputLineState(outputLineNumber, newRandValue)
                .then( response => ed588.getAllDigitalLineStates() )
                .then( response => console.log(`IO Line values as int array [${response.join(',')}]`));

        // alternative notation using await, this is equivalent the the block above
        // note: async and await can only be used in node.js version 7.6 and above
        // var asyncTest = async function() {
        //     await ed588.setDigitalOutputLineState(outputLineNumber, newRandValue);
        //     let response = await ed588.getAllDigitalLineStates();
        //     console.log(`IO Line values as int array [${response.join(',')}]`)
        // }
        // asyncTest();

        i++;
    },intervalMS)
}, (err) => {
    console.log(`Connection to ED failed reason: ${err}`);
});


FAQs