简体   繁体   中英

Template literal, weird output

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM