Template literal, weird output

61 Views Asked by At

I'm working with some lab equipment using ASTM over TCP/IP. Getting some weird behavior. Using just Node and the net package.

socket.on('data', data => {
        let str = data.toString('ascii');
        console.log(`the string ---- ${str}`);
    if (str === ENQ) {
        socket.write(ACK);
    } else {
        console.log(str);
    }

outputs (given correct input):
E1 string ----  1H|\^&|||1^Analyzer 1^6.0|||||||P||20201216150358
E1|\^&|||1^Analyzer 1^6.0|||||||P||20201216150358

I need the stuff on the top line after the dashes, but "The" becomes E1, then E1 moves down to the next line and replaces 1H. What's going on here? I'm hoping it just has something to do with console.log so I can still get to the results I'm looking for.

1

There are 1 best solutions below

0
On

So it looks like some of the control characters are making the output weird. Towards the end of the line, there is a CR and ETX at the end of the line followed by a checksum of the line. So it seems that the carriage return is sending the cursor back to the front of the line and putting the ETX and checksum in place of "The"