[英]Reading in Multiple Serial Inputs with Node.Js
I have been working on a simple script to monitor 6 arduino units in my lab. 我一直在研究一个简单的脚本来监控我实验室中的6个arduino单元。 I seem to have almost got it! 我好像差不多了!
My current issue is getting each dataset nicely bundled all in one string. 我目前的问题是将每个数据集很好地捆绑在一个字符串中。 as it is, I am able to open a connection with all the serial ports, recieve their data, as they spew it out, but lack the code block to organize it into the bundles. 实际上,我可以打开与所有串口的连接,在他们发出数据时接收他们的数据,但缺少将其组织到捆绑中的代码块。 I have considered attempting to make a temporary array to append the chunks to, or produce child processes. 我曾考虑过尝试创建一个临时数组来附加块或生成子进程。 However, I have had difficulties with child processes, and am not sure how to structure an array in an appropriate way. 但是,我遇到了子进程的困难,并且不确定如何以适当的方式构造数组。 Once I have a bundled string I will be sending to a database on my server. 一旦我有一个捆绑的字符串,我将发送到我的服务器上的数据库。 I do have the feeling I may be doing this in an innapropriate way, any and all advice is much apreciated! 我确实有这种感觉,我可能会以一种不合适的方式做到这一点,任何和所有的建议都是非常令人沮丧的!
PS: You may notice there are only 5 arduinos showing, that was just to make things a little less complicated due to the 6th arduino also showed up as com9. PS:你可能会注意到只有5个arduinos显示,这只是为了让事情变得不那么复杂,因为第6个arduino也显示为com9。
Thank you 谢谢
The Code 代码
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
// list serial ports:
serialport.list(function (err, ports) {
console.log(" Readout all availiable Com Ports and their pnpID: ");
ports.forEach(function(port) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
var serialPort = new SerialPort(port.comName, {baudrate: 9600
}, false);
serialPort.open(function (error) {
if ( error ) {
console.log('failed to open: '+error);
} else {
console.log('open');
serialPort.on('data', function(data) {
console.log('data received: ' + port.comName+ ' ' + data);
});
}
});
})
})
The Output 输出
Readout all availiable Com Ports and their pnpID:
COM11
USB\VID_2341&PID_0043\75430333437351517211
Arduino LLC (www.arduino.cc)
COM9
USB\VID_2341&PID_0043\7543033343735151C020
Arduino LLC (www.arduino.cc)
COM8
USB\VID_2341&PID_0043\754363434303516181F0
Arduino LLC (www.arduino.cc)
COM7
USB\VID_2341&PID_0043\7543033313735140B152
Arduino LLC (www.arduino.cc)
COM4
USB\VID_2341&PID_0043\754303334373515162D0
Arduino LLC (www.arduino.cc)
open
open
open
open
open
data received: COM8 L03
data received: COM4 L01
data received: COM8 , -0.00127 , 8.19188
data received: COM4 , -
data received: COM7 L02
data received: COM4 0.00127 , 8.
data received: COM7 , -0.00
data received: COM4 19188
data received: COM7 127 , 8.
data received: COM9 L
data received: COM7 19188
From the documentation of the serial port library https://www.npmjs.com/package/serialport 从串口库https://www.npmjs.com/package/serialport的文档中
There is an option to read the data until a new line is encountered. 在遇到新行之前,可以选择读取数据。 This can be used instead of the baudRate in order to read whole lines of data. 可以使用它来代替baudRate以读取整行数据。
Change the following line(s) 更改以下行
var serialPort = new serialport.SerialPort(port.comName, {
baudrate: 9600
}, false);
to 至
var serialPort = new serialport.SerialPort(port.comName, {
parser: serialport.parsers.readline("\n")
}, false);
Then in your callback the data will come as a full line instead of in pieces 然后在你的回调中,数据将作为一个完整的行而不是碎片
EDIT: 编辑:
You should make a collector then that handles the server communication part. 您应该创建一个处理服务器通信部分的收集器。 Just place it in another file in the same directory called collector.js
. 只需将其放在名为collector.js
的同一目录中的另一个文件中即可。 Then the main script would work like that: 然后主脚本会这样工作:
var serialport = require('serialport');
var collector = require('./collector');
// list serial ports:
serialport.list(function (err, ports) {
console.log(" Readout all available Com Ports and their pnpID: ");
ports.forEach(function (port) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
var serialPort = new serialport.SerialPort(port.comName, {
baudrate: 9600
}, false);
serialPort.open(function (error) {
if (error) {
return console.log('failed to open: ' + error);
}
console.log('open');
serialPort.on('data', function (data) {
console.log('data received: ' + port.comName + ' ' + data);
// add message to port message collection
collector.add(port.comName, data);
});
});
});
});
And the collector would work like: 收藏家的工作方式如下:
var os = require('os');
var messages = {};
function checkAndSendData(key, data, message) {
if (data === os.EOL || message.match(/^L\d\d , \d,\d\d\d\d\d , \d\.\d\d\d\d\d$/)) {
// TODO: send data to server in formatted way, maybe need key?
return true;
}
return false;
}
module.exports = {
add: function(key, data) {
if (typeof messages[key] === 'undefined') {
messages[key] = '';
}
messages[key] += String(data);
if (checkAndSendData(key, data, messages[key])) {
messages[key] = '';
}
}
}
This got a bit long for a comment.. 这有点长的评论..
serialPort.on('data', function(data) {
console.log('data received: ' + port.comName+ ' ' + data);
// switch(JSON.parse(data).type) { // if that's a possibility
if ("BEGIN" === data) {
console.log('begin data received: ' + port.comName+ ' ' + data);
}
});
I think what you're looking for is a paradigm to copy (Factory, Reader/Writer?) but maybe Begin, Stuff, and End would work for you? 我认为你要找的是一个复制的范例(工厂,读者/作家?)但也许Begin,Stuff和End会对你有用吗? Then you can generate events or call callbacks you've passed in. 然后,您可以生成事件或调用您传入的回调。
serialPort.on('data', function(data) {
console.log('data received: ' + port.comName+ ' ' + data);
// var _data = JSON.parse(data); // if that's a possibility
// switch(_data.type) {
// case "BEGIN": fnBegin(port.comName, _data.object); break;
// or
if ("BEGIN" === data) {
console.log('begin data received: ' + port.comName+ ' ' + data);
}
// or
factory.fn(port.comName, data);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.