简体   繁体   English

xbee arduino通信编程

[英]xbee arduino communication programming

I tried testing the system and I'm not sure if the problem is with the xbee's, the transmitting code, or the recieveing code. 我尝试测试系统,但不确定是xbee,传输代码还是接收代码出问题。 Before I post my code I will explain what we are doing with the signals. 在发布代码之前,我将解释我们如何处理信号。 We have three analog signals that will be sent serially through one xbee using an arduino and xbee shield. 我们有三种模拟信号,它们将使用arduino和xbee屏蔽通过一个xbee串行发送。 We want to send these signals to the receiving xbee where the arduino will output these signals to be connected to a third arduino through wires to be used in a Simulink program. 我们想将这些信号发送到接收xbee,在那里arduino将输出这些信号,并通过Simulink程序中使用的电线将其连接到第三arduino。 We are using an arduino mega for the transmitting side and an arduino uno for the receiving side. 我们在发送侧使用arduino mega,在接收侧使用arduino uno。 I was told I need to do serial streaming but I'm not sure how that's done. 有人告诉我我需要进行串行流传输,但是我不确定该怎么做。 I understand the xbee and arduinos both digitize signals but we are hoping to get a signal very similar to the analog signals we are transmitting. 我了解xbee和arduinos都将信号数字化,但是我们希望获得与我们正在传输的模拟信号非常相似的信号。 Any amount of help is greatly appreciated!! 任何帮助都将不胜感激!
This is how I have my xbees configured (series 1) both in AT mode: 这就是我在AT模式下配置xbees(系列1)的方式:

Transmitting Xbee: 传输Xbee:
Channel:10 通道:10
Pan id: 1234 窗格编号:1234
MY: 10 我:10
DL: 11 DL:11
Receiving Xbee: 接收Xbee:
Channel:10 通道:10
Pan ID: 1234 潘ID:1234
MY: 11 我:11
DL: 10 DL:10

transmitting Arduino code: 传输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);          
}  

Receiving Arduino code: 接收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);  
    }  
}

It sounds like you're using the XBee modules in "AT mode" or "transparent serial" mode where anything received on the serial port of module A is sent out of the serial port of module B, and vice versa. 听起来您好像在“ AT模式”或“透明串行”模式下使用XBee模块,其中在模块A的串行端口上接收的所有内容都从模块B的串行端口发送出去,反之亦然。

If that's the case, it may help to do your initial development with the serial ports of your two devices connected directly to each other. 如果是这种情况,将两个设备的串行端口直接彼此连接可能会有助于您进行初始开发。 Work out your serial protocol there, and then try to run it with the XBee modules in place as a serial cable replacement. 在此处制定您的串行协议,然后尝试在安装了XBee模块的情况下运行它作为串行电缆的替代品。

Consider the format of the data that you're sending, and how you will process it on the other end. 考虑要发送的数据的格式,以及在另一端如何处理它。 How will you separate the readings and identify which analog input they belong to? 您将如何分离读数并确定它们属于哪个模拟输入? With your current code, you print the readings on separate lines, but it won't be clear which is A0 . 使用当前代码,您可以在单独的行上打印读数,但是不清楚是哪个A0 Maybe you want to send them on a single line with a comma in between each reading? 也许您想将它们发送给一行,并且每次阅读之间都用逗号隔开?

On the receiving end, you need to convert the text back to an integer using a C function like atoi() or strtoul() . 在接收端,您需要使用像atoi()strtoul()这样的C函数将文本转换回整数。

If you're trying to create an analog output on the Arduino, it might be possible with a digital output that's using PWM (pulse width modulation). 如果您试图在Arduino上创建模拟输出,则可能使用使用PWM(脉冲宽度调制)的数字输出。 This Instructable does a decent job of describing that concept. 这个Instructable很好地描述了这个概念。

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

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