我目前正在做一个项目,我将 GPS 模块、数字罗盘和ZigBee(XBee调制解调器)连接到 Arduino。顺便说一句,我使用Eclipse IDE 来做这个项目,所以我把我的代码分解成几个库。
XBee 目前使用硬件UART并且工作正常。GPS 模块和数字罗盘都使用软件串行。
我的问题是,如果我实例化两个软件序列号,则只有最新的序列号可以工作。
下面是我的示例代码。
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 现在可以工作,但数字罗盘不能。我认为软件序列号有问题。解决办法是什么?