[英]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頁:
好吧,事實證明問題並不完全像我想的那樣是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.