我一直在研究一个简单的脚本来监控我实验室中的6个arduino单元。我好像差不多了!
我目前的问题是将每个数据集很好地捆绑在一个字符串中。实际上,我可以打开与所有串口的连接,在他们发出数据时接收他们的数据,但缺少将其组织到捆绑中的代码块。我曾考虑过尝试创建一个临时数组来附加块或生成子进程。但是,我遇到了子进程的困难,并且不确定如何以适当的方式构造数组。一旦我有一个捆绑的字符串,我将发送到我的服务器上的数据库。我确实有这种感觉,我可能会以一种不合适的方式做到这一点,任何和所有的建议都是非常苛刻的!
PS:您可能会注意到只有5个arduinos显示,这只是为了让事情变得不那么复杂,因为第6个arduino也显示为com9。谢谢
守则
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);
});
}
});
})
})
输出
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
答案 0 :(得分:2)
从串口库的文档中 https://www.npmjs.com/package/serialport
在遇到新行之前,可以选择读取数据。可以使用它来代替baudRate来读取整行数据。
更改以下行
var serialPort = new serialport.SerialPort(port.comName, {
baudrate: 9600
}, false);
到
var serialPort = new serialport.SerialPort(port.comName, {
parser: serialport.parsers.readline("\n")
}, false);
然后在你的回调中,数据将以整行而不是分段
答案 1 :(得分:1)
编辑:
您应该创建一个处理服务器通信部分的收集器。只需将其放在名为collector.js
的同一目录中的另一个文件中即可。然后主脚本会这样工作:
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);
});
});
});
});
收藏家的工作方式如下:
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] = '';
}
}
}
答案 2 :(得分:0)
这有点长的评论..
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);
}
});
我认为您正在寻找的是一种复制模式(工厂,读者/作家?)但是,Begin,Stuff和End可能适合您?然后,您可以生成事件或致电您已传入的回调。
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);
});