Search code examples
javascriptnode.jsnode-serialport

Send CR LF in JavaScript string to node.js serialport server


I've successfully followed the instructions for creating a webpage to talk to serial ports found here. And here's the GitHub repository for his project. I've modified it slightly to use Windows COM ports and fake the Arduino data. Now I'm trying to modify it further to talk to one of my company's test boards. I've established two-way communication, so I know I can talk in both directions over the serial port.

Sending id?CRLF over serial to the board will get a response of something like id=91. I can do this in PuTTY by just typing in id? & hitting the Enter key, or in DockLight by creating a send sequence id?rn, both of which work as expected, I get the id=91 response.

However, in the client.js JavaScript, trying to send: socket.send("id?\r\n"); in the console doesn't work, but I see it show up with an extra line in the server response. So I see something like this:

Message received
id?
                                                                  <=blank line

So I tried to send the ASCII equivalents by doing:

var id = String.fromCharCode(10,13);
socket.send("id?" + id);

Which also doesn't work, although two extra lines show up in the server.

Message received
id?
                                                                  <=blank line
                                                                  <=another blank line

EDIT: I've also tried: socket.send('id?\u000d\u000a'); Same results as the first Message received blurb above.

I see the sent command arrive at the server (I've modified it a bit to do a console.log upon receipt of a message from the client):

function openSocket(socket){
console.log('new user address: ' + socket.handshake.address);
// send something to the web client with the data:
socket.emit('message', 'Hello, ' + socket.handshake.address);

// this function runs if there's input from the client:
socket.on('message', function(data) {
    console.log("Message received");
    console.log(data);
    //here's where the CRLF should get sent along with the id? command
    myPort.write(data);// send the data to the serial device
});

// this function runs if there's input from the serialport:
myPort.on('data', function(data) {
    //here's where I'm hoping to see the response from the board
    console.log('message', data);  
    socket.emit('message', data);       // send the data to the client
});
}

I'm not positive that the CRLF is the problem, but I'm pretty sure it is. Possibly it's being swallowed by the server?

How can I embed it in a string to be sent to the server so it get interpreted properly and sent along to the serial port?

Other SO pages I've read:

How can I insert new line/carriage returns into an element.textContent?

JavaScript string newline character?


Solution

  • Well, it turns out that the problem wasn't exactly the CRLF like I thought, it was how the string terminator was being handled. All of our devices use what we can an "S prompt" (s>) for when a command has been processed. When it's done the last thing the board does is return an S prompt, so I'd modified the original server parser code to look for that. However that's a response terminator, not a request terminator. Once I changed it back to parser: serialport.parsers.readline('\n') it started to work.

    // 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,
        // call myPort.on('data') when a newline is received:
        parser: serialport.parsers.readline('\n')
        //changed from '\n' to 's>' and works.
        //parser: serialport.parsers.readline('s>')
    };