【发布时间】:2012-06-14 04:32:48
【问题描述】:
我目前正在做一个项目,我将 GPS 模块、数字罗盘和 ZigBee(XBee 调制解调器)连接到 Arduino。顺便说一句,我使用Eclipse IDE 来做这个项目,所以我把我的代码分解成几个库。
XBee 当前使用硬件UART 并且工作正常。 GPS模块和数字罗盘都使用Software Serial。
我的问题是,如果我同时实例化两个软件序列,那么只有最新的可以工作。
下面是我的示例代码。
void CSensor::initSensors()
{
//For GPS
this->gpsSerial = new SoftwareSerial(2, 3);
this->gpsSerial->begin(9600);
//For digital compass
this->compassSerial = new SoftwareSerial(6, 7);
this->compassSerial->begin(9600);
}
gpsSerial 被声明为CSensor 类的指针成员。 ->SoftwareSerial* gpsSerial; compassSerial 也被声明为相同 -> SoftwareSerial* compassSerial;
在上面的示例代码中,只有数字罗盘可以工作,GPS 不能工作。但是如果我交换代码的位置,它会看起来像这样:
void CSensor::initSensors()
{
//For digital compass
this->compassSerial = new SoftwareSerial(6, 7);
this->compassSerial->begin(9600);
//For GPS
this->gpsSerial = new SoftwareSerial(2, 3);
this->gpsSerial->begin(9600);
}
GPS 现在可以工作,但数字罗盘不能。我认为软件序列号有问题。解决办法是什么?
【问题讨论】:
标签: gps serial-port arduino