【发布时间】: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 node.js node-serialport