Beaglebone 黑色 revC

Beaglebone Black revC

我正在通过串行端口 (ttyO2) 连接 BBB 和一组 arduino。 我有一个数组要从 BBB 发送到一组 arduino。 我需要让 BBB 发送请求并等待其中一个 arduino 的回复,但如果在一个时间间隔内没有 arduino 回复,则 BBB 必​​须在数组中发送以下值。 我已经为他们的工作准备好了连接和 arduino。 问题是BBB会在监听端口的同时完成代码的执行。如果收到数据=> 处理它,我需要让它监听一个特定的时间;否则完成以下代码部分(发送数组的剩余部分)。这项工作需要循环进行。 我一直在尝试使用setTimeout,递归,但没有成功! 我正在使用以下代码在 ttyO2 上进行监听和写入:

` var b = require('bonescript');

//opening the serial port
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort('/dev/ttyO2', {
baudrate: 115200
});
var i = 0;

serialPort.on("open", function () {
console.log('opened');
serialPort.on('data', function(data) {
console.log('data received: ' + data);
serialPort.write( i + "\n", function(){});
});
});

serialPort.on("data", function (data) {
console.log("here: "+data);
});

`

var b = require('bonescript');


//opening the serial port

var SerialPort = require("serialport").SerialPort;

var serialPort = new SerialPort('/dev/ttyO2', {

  baudrate: 115200

});


var i = 0;
var waiting_interval = 5000;

var slaves = ["S1", "S2" , "S3", "S4", "S5"];



serialPort.on('open',function my(){ 
console.log("opened");
serialPort.on('data', function listenToSlaves(data){
        console.log("returned: " + data);
    });
writeToSlaves();

});


function writeToSlaves(){

//  setInterval(serialPort.write(slaves[i], function(){ console.log("I 
wrote to slave: " + i)}), 5000);

serialPort.write(slaves[i], function(){ });
console.log("I wrote to slave: " + i);
if(i<slaves.length - 1) i++;
else i=0;
setTimeout(writeToSlaves, waiting_interval);

}