繁体   English   中英

将JavaScript字符串中的CR LF发送到node.js串行端口服务器

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

我已经成功地遵循了创建网页以与此处找到的串行端口对话的说明。 是他的项目GitHub存储库 我已对其进行了少许修改,以使用Windows COM端口并伪造Arduino数据。 现在,我正在尝试对其进行进一步修改,以与公司的测试板之一进行交流。 我已经建立了双向通讯,所以我知道我可以通过串行端口双向通讯。

通过串行向板子发送id?CRLF会得到诸如id=91类的响应。 我可以通过输入id?在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?\ \ '); 与上面收到的第一封邮件相同的结果。

我看到发送的命令到达服务器(我已经对其进行了一些修改,以便在收到来自客户端的消息后执行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页:

如何将新行/回车符插入element.textContent?

JavaScript字符串换行符?

好吧,事实证明问题并不完全像我想的那样是CRLF,而是字符串终止符的处理方式。 处理命令后,我们所有的设备都将使用“ S提示符”( s> )。 完成后,开发板要做的最后一件事是返回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>')
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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