delliottg
delliottg

Reputation: 4150

Serialport parser doesn't work when configured in function

I have a server that talks to a serial device. If I directly configure the serial port inline in the code, it works as expected. However, if I pass in the configuration via a function to create a new serialport object, the parser doesn't work.

Code that works:

// serial port initialization:
var serialport = require('serialport'), // include the serialport library
SerialPort = serialport.SerialPort, // make a local instance of serial
portName = process.argv[2], // get the port name from the command line
portConfig = {
    baudrate : 9600,
    databits : 8,
    parity : 'none',
    stopBits : 1,
    buffersize : 4096,
    parser : serialport.parsers.readline('\n')
};
console.log(portConfig);

// open the serial port:
var myPort = new SerialPort(portName, portConfig);
console.log(myPort);

Code that doesn't work:

function SetSerialPortConfig(data) {
    var portBundle = JSON.parse(data);
    var serialport = require('serialport'), // include the serialport library
    SerialPort = serialport.SerialPort,
    portName = portBundle[1].portName,
    portConfig = {
        baudrate : portBundle[0].baudrate,
        databits : portBundle[0].databits,
        parity : portBundle[0].parity,
        stopBits : portBundle[0].stopBits,
        buffersize : portBundle[0].buffersize,
        parser : serialport.parsers.readline('\n')
        };
    return new SerialPort(portName, portConfig);
}

And the data object passed into the function (this is hard coded for now with parameters we know work):

function configureSerialPort(){
var portBundle = [{
    baudrate: 9600,
    databits: 8,
    parity: 'none',
    stopBits: 1,
    buffersize: 4096,
},
{
    portName: 'com21'
}];
socket.send(JSON.stringify(portBundle));
}

The port myPort is configured using input from a button on a website, which is read over the socket:

// this function runs if there's input from the client:
socket.on('message', function (data) {
    console.log("Client request received.");
    console.log("SerialSend: " + data);
    //check to see if the port is configured, if not, run configuration
    if (typeof myPort === 'undefined') {
        myPort = SetSerialPortConfig(data);
        //prevents a write to the port with configuration data.
        return false;
    }
    myPort.write(data); // send the data to the serial device
});

We need the web page to be able to pass in any set of configuration variables, so I need to make the function method work. If I do a console.log(myPort); in either case, the ports appear to be identical, so I can't see why the parser isn't working. I can visually see that data is being transmitted via the LED Tx & Rx lights on the RS-485 converter, so I know the port on the device is sending and receiving data, but the parser isn't seeing the EOL character (I think), so it's just waiting.

Upvotes: 0

Views: 938

Answers (1)

ChrisAdmin
ChrisAdmin

Reputation: 1002

I encountered similar, and the fix for me was to use parseInt() on all the likely integer types, such as baudrate and stopbits.

portConfig = {
    baudrate : parseInt(portBundle[0].baudrate),
    databits : parseInt(portBundle[0].databits),
    parity : portBundle[0].parity,
    stopBits : parseInt(portBundle[0].stopBits),
    buffersize : parseInt(portBundle[0].buffersize),

Upvotes: 0

Related Questions