简体   繁体   中英

Beaglebone Black revC

I am connecting BBB and a set of arduinos over the serial port (ttyO2). I have an array to be sent from BBB to a set of arduinos. I need to make the BBB sends a request and wait for a reply from one of the arduinos, but if no arduino replies within an interval, the BBB must send the following value in the array. I have the connection and arduinos ready for their jobs. The problem is that the BBB will listen on the port and complete the execution of the code at the same time. I need to make it listen for a specific time, if data received=> process it; else complete the following part of code (send the remaining part of the array). This job need to be in a loop. I have been trying to use setTimeout, recursion, but with no success! I am using the following code to listen and write on 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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM