Xbee arduino通讯编程

xbee arduino communication programming

本文关键字:编程 arduino Xbee      更新时间:2023-10-16

我试着测试系统,我不确定问题是与xbee的,发送代码,还是接收代码。在我发布我的代码之前,我将解释我们正在用信号做什么。我们有三个模拟信号,将使用arduino和xbee屏蔽通过一个xbee串行发送。我们希望将这些信号发送到接收xbee, arduino将输出这些信号,通过导线连接到第三个arduino,用于Simulink程序。我们使用arduino mega作为发送端,arduino uno作为接收端。我被告知我需要做串行流,但我不确定如何做到这一点。我知道xbee和arduinos都是数字化信号,但我们希望得到的信号非常类似于我们正在传输的模拟信号。任何数量的帮助是非常感激的!!
以下是我在AT模式下配置xbees(系列1)的方式:

传输Xbee:
频道:10
Pan id: 1234
我:10
DL: 11
收到Xbee:
频道:10
Pan ID: 1234
我:11
DL: 10

传输Arduino代码:

void setup() {  
    Serial.begin(9600);  
}  
void loop() {  
// read the input on analog pins  
int sensorValue1 = analogRead(A0);  
int sensorValue2 = analogRead(A1);  
int sensorValue3 = analogRead(A2);  
// print out the value you read:  
Serial.println(sensorValue1);  
Serial.println(sensorValue2);  
Serial.println(sensorValue3);  
delay(1);          
}  

接收Arduino代码:

int received1=8;  
int received2=9;  
int received3=10;  
void setup(){  
    pinMode(received1, OUTPUT);  
    pinMode(received2, OUTPUT);  
    pinMode(received3, OUTPUT);  
    Serial.begin(9600);  
}  
void loop(){  
    if(Serial.available() )  
    {  
        byte output1 = Serial.read();  
        byte output2 = Serial.read();  
        byte output3 = Serial.read();  
        digitalWrite(received1, HIGH);  
        digitalWrite(received2, HIGH);  
        digitalWrite(received3, HIGH);  
    }  
}

听起来你正在使用"AT模式"或"透明串行"模式下的XBee模块,其中模块A的串行端口上接收的任何内容都是从模块B的串行端口发送出去的,反之亦然。

如果是这种情况,那么将两个设备的串行端口直接连接在一起进行初始开发可能会有所帮助。在那里找出您的串行协议,然后尝试在XBee模块作为串行电缆替代品的情况下运行它。

考虑你发送的数据的格式,以及你将如何在另一端处理它。如何分离读数并确定它们属于哪个模拟输入?使用当前的代码,您将读数打印在单独的行上,但不清楚哪个是A0。也许你想把它们写成一行,每次阅读之间用逗号隔开?

在接收端,您需要使用atoi()strtoul()等C函数将文本转换回整数。

如果您试图在Arduino上创建模拟输出,则可能使用PWM(脉宽调制)的数字输出。