Search code examples
node.jsnode-webkitnode-modulesnw.jsnode-serialport

how can I get the result of a weight with nodeJS


I have a problem, I am trying to get the weight from a Scale with NodeJS and the npm module "Serialport" on Windows.

The problem that I have is that it doesn't matter the command that I sent to the Scale, it is not return any data.

I already compiled the module serialport for the project that I am making on nwJS, and I have the following code:

    var SerialPort = require('serialport');

            var port = new SerialPort('COM4', {
                parser: SerialPort.parsers.readline('W\n'),
                baudRate: 9600,
                dataBits: 7,
                stopBits: 1,
                parity: 'even',
                bufferSize: 1024
            });

function write() {
            port.open(function(err) {
                console.log("Writing serial data: ");
                port.on('data', function(data) {
                    console.log('Data: ' + data);
                });
                port.write(resultString, function(err, res) {
                    if (err) {
                        console.log(err);
                    }
                    port.close();
                });
            });
        }
    port.on('error', function(err) {
                console.log('Error: ', err.message);
            })
            setTimeout(write, 1000);

Note: the parser W\n is the string that the scale needs to sent the weight. I tested that with another application named coolterm

Thanks for any help.


Solution

  • Most probably the issue is with parser's config. Instead of a ReadLine parser:

     parser: SerialPort.parsers.readline('W\n'),
    

    Try a Delimiter parser :

    var weight_parser = port.pipe(new Delimiter({delimiter: new Buffer('W\n')}));
    ...
    parser: weight_parser,
    ...
    parser.on('data', console.log);
    

    If that does not work, to troubleshoot try a ByteLength parser to see if 'data' event is getting fired or not.