【问题标题】:Send CR LF in JavaScript string to node.js serialport server将 JavaScript 字符串中的 CR LF 发送到 node.js 串行端口服务器
【发布时间】:2016-06-30 22:31:34
【问题描述】:

我已成功按照说明创建网页以与找到here 的串行端口通信。这是GitHub repository for his project。我稍微修改了它以使用 Windows COM 端口并伪造 Arduino 数据。现在我正在尝试进一步修改它以与我公司的一个测试板交谈。我已经建立了双向通信,所以我知道我可以通过串口进行双向通信。

通过串行方式向开发板发送id?CRLF 会得到类似id=91 的响应。我可以在 PuTTY 中通过输入 id? 并按 Enter 键来执行此操作,或者在 DockLight 中通过创建发送序列 id?rn 来执行此操作,两者都按预期工作,我得到了 id=91 响应。

但是,在 client.js JavaScript 中,尝试在控制台中发送:socket.send("id?\r\n"); 不起作用,但我看到它在服务器响应中显示了额外的一行。所以我看到了这样的东西:

Message received
id?
                                                                  <=blank line

所以我尝试通过以下方式发送 ASCII 等价物:

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

这也不起作用,尽管服务器中显示了两条额外的行。

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

编辑:我也试过:socket.send('id?\u000d\u000a'); 与上面收到的第一条消息相同的结果。

我看到发送的命令到达服务器(我已经对其进行了一些修改,以便在收到来自客户端的消息后执行 console.log):

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
});
}

我不肯定 CRLF 是问题,但我很确定它是。可能是被服务器吞了?

如何将它嵌入到要发送到服务器的字符串中,以便正确解释并发送到串行端口?

我读过的其他 SO 页面:

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

JavaScript string newline character?

【问题讨论】:

    标签: javascript node.js node-serialport


    【解决方案1】:

    好吧,事实证明问题并不像我想象的那样完全是 CRLF,而是字符串终止符的处理方式。在处理命令时,我们所有的设备都使用“S 提示符”(s&gt;)。当它完成后,板子做的最后一件事就是返回一个 S 提示,所以我修改了原始的服务器解析器代码来寻找它。但是,这是响应终止符,而不是请求终止符。一旦我把它改回parser: serialport.parsers.readline('\n'),它就开始工作了。

    // 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>')
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      相关资源
      最近更新 更多