简体   繁体   English

Beaglebone Black revC

[英]Beaglebone Black revC

I am connecting BBB and a set of arduinos over the serial port (ttyO2). 我正在通过串行端口(ttyO2)连接BBB和一组arduino。 I have an array to be sent from BBB to a set of arduinos. 我有一个数组要从BBB发送到一组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. 我需要让BBB发送请求并等待来自arduino之一的答复,但是如果间隔内没有arduino答复,则BBB必须在数组中发送以下值。 I have the connection and arduinos ready for their jobs. 我已经为他们的工作准备好了联系和arduinos。 The problem is that the BBB will listen on the port and complete the execution of the code at the same time. 问题在于,BBB将监听端口并同时完成代码的执行。 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! 我一直在尝试使用setTimeout,递归,但没有成功! I am using the following code to listen and write on ttyO2: 我正在使用以下代码在ttyO2上进行侦听和编写:

` var b = require('bonescript'); `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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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