【发布时间】:2015-02-02 17:30:10
【问题描述】:
我想使用来自 Adafruit + Xbee Shield + Xbee S2 的 Arduino UNO R3 + Ultimate GPS Shield(内置 GPS)和另一个只有 Xbee Shield + Xbee 模块的 Arduino MEGA 制作一个项目
以下代码是来自 adafruit 的库,用于使用 115200 的波特率在控制台中输出 gps 数据。我的问题是 xbee 使用这种代码变得不可识别(使用 XCTU),因此我无法对其进行配置。
我的问题是我的 xbee 是否仍然可以工作,即使它无法识别或者我的软件串行引脚有问题或其他什么?
请注意,gps 可以正常工作并输出数据。
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7);
SoftwareSerial xbeeSerial(2,3);
Adafruit_GPS GPS(&mySerial);
#define GPSECHO false
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
void setup()
{
Serial.begin(115200);
xbeeSerial.begin(9600);
Serial.println("Adafruit GPS library basic test!");
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA);
useInterrupt(true);
delay(1000);
mySerial.println(PMTK_Q_RELEASE);
}
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
#endif
}
void useInterrupt(boolean v) {
if (v) {
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
void loop() // run over and over again
{
if (! usingInterrupt) {
char c = GPS.read();
if (GPSECHO)
if (c) Serial.print(c);
}
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
if (timer > millis()) timer = millis();
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
Serial.print(GPS.hour, DEC); Serial.print(':');
Serial.print(GPS.minute, DEC); Serial.print(':');
Serial.print(GPS.seconds, DEC); Serial.print('.');
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
Serial.print("Location (in degrees, works with Google Maps): ");
Serial.print(GPS.latitudeDegrees, 4);
Serial.print(", ");
Serial.println(GPS.longitudeDegrees, 4);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}
【问题讨论】:
-
你是说当XBee安装在Arduino上的扩展板中时,不能使用X-CTU配置XBee?除了打开它之外,您的代码似乎没有对该串行端口做任何事情。只需使用 XBee USB 适配器或其他开发板配置 XBee,然后将其安装在 Arduino 中。还可以考虑以 115200bps 的速度运行 XBee - 如果您推送大量数据,您将获得更好的性能。
-
不,我实际上是在说代码块“serial.begin()”,即使我的草图中只有该代码,它也会由于某种原因使 xbee 模块在 XCTU 中无法识别。但如果我使用一个空的草图或除 xbee 之外的其他东西是可识别的!我需要在 XCTU 配置中的某处配置串行引脚吗?