c - 为什么我看不到使用 Arduino 串行监视器通过 XBees 发送的数据?

标签 c arduino xbee

我正在使用 3 个 Xbees。 2个配置为路由器AT,另一个配置为协调器AT。路由器上的代码用于从 LM 和 3 个 OneWire DS18B20 传感器读取温度:

#include <SoftwareSerial.h>

float temp;
int tempPin = 0;

SoftwareSerial xbee(4, 3);

void setup()
{
Serial.begin(9600);
xbee.begin(9600);
}

void loop()
{
delay(1000);
temp = analogRead(tempPin);
temp = temp * 0.48828125;
//Serial.print("TEMPRATURE = ");
//Serial.print(temp);
// Serial.print("*C");
//Serial.println();
//delay(1000);
xbee.print("Temp D: "); xbee.print(temp);
delay(1000);
}

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 8

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

DeviceAddress camera1Temp = { 0x28, 0xF6, 0x7C, 0xA0, 0x05, 0x00, 0x00, 0x0E };
DeviceAddress camera2Temp = { 0x28, 0x2A, 0x61, 0xDD, 0x03, 0x00, 0x00, 0xC3 };
DeviceAddress camera3Temp = { 0x28, 0x52, 0x8D, 0x30, 0x05, 0x00, 0x00, 0x04 };

SoftwareSerial xbee(4, 3); // RX, TX

void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(camera1Temp, 10);
sensors.setResolution(camera2Temp, 10);
sensors.setResolution(camera3Temp, 10);

xbee.begin(9600);

}

void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Eroare citire temperaturi");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}

void loop(void)
{
delay(2000);
//Serial.print("Citire temperaturi...\n\r");
sensors.requestTemperatures();

/*delay(1000);

Serial.print("Temperatura in camera 1: ");
printTemperature(camera1Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 2: ");
printTemperature(camera2Temp);
Serial.print("\n\r");
delay(1000);
Serial.print("Temperatura in camera 3: ");
printTemperature(camera3Temp);
*/

float temp1 = sensors.getTempC(camera1Temp);
float temp2 = sensors.getTempC(camera2Temp);
float temp3 = sensors.getTempC(camera3Temp);
/*Serial.print(int(temp1*100));
delay(1000);
Serial.println(int(temp2*100));
delay(1000);
Serial.println(int(temp3*100));
*/

//Serial.print("\n\r\n\r");

xbee.print("Temp A: "); xbee.print(temp1);
delay(100);
xbee.print("Temp B: "); xbee.print(temp2);
delay(100);
xbee.print("Temp C: "); xbee.print(temp3);
delay(100);

}

在 XCTU 中连接 Xbee 协调器时,我可以看到接收文本和温度。但是当将 xbee 协调器连接到 arduino 并使用以下代码时,我什么也看不到:

void setup()

{

Serial.begin(9600);

}

void loop()

{

if (Serial.available()>0)

{

Serial.write(Serial.read());

}

}

在此之后我想处理来自 4 个传感器的传入文本....但我在 arduino 上看不到任何内容。 你能告诉我我做错了什么吗?

最佳答案

在您的 Arduino 草图中;我认为这样做是不对的:
Serial.write(Serial.read());

从 Arduino 论坛看到这个:Help with Serial.Read() getting string.
此草图可能对您有所帮助:

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void loop()
{
   while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
       if(index < 19) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
   // Now do something with the string (but not using ==)
    for(int i = 0; i < index ; i++){
        Serial.print(inData[i]);
    }
}


在将数据写入串行接口(interface)之前,您必须缓冲数据。

关于c - 为什么我看不到使用 Arduino 串行监视器通过 XBees 发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24214583/

相关文章:

c - 生成整数的 4 位校验和

c - 如何用AST树或其他工具做静态代码逻辑分析?

c - 仅在 C 的链接列表中 append 唯一值

c++ - 如何使用 C 终止远程计算机上的进程?

arduino - 关注 Arduino 程序中 micros() 值的漂移

arduino - 将 API tx 帧从协调器发送到终端设备(zigbee+arduino)

实际应用中等待所有 child 的正确方法

arduino - 使用 Arduino 将serial.read()转换为可用的字符串

c++ - 串行数据最初错误 C++

arduino - 使用两个 xbees 进行 arduino 和 pc 的通信