使用Arduino类,使用SoftwareSerial作为变量



尝试在Arduino 1.0中使用类并将SoftwareSerial设置为变量,但没有成功。

class SensorGPS: public Thread
{
  private: 
        SoftwareSerial* serialGPS; // RX, TX
  public:
        SensorGPS()
        {
          serialGPS = new SoftwareSerial(10,11);
          serialGPS.begin(4800);
        }
}

serialGPS。Begin返回错误

arduino_sketch.ino: In constructor 'SensorGPS::SensorGPS()':
arduino_sketch:31: error: request for member 'begin' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'
arduino_sketch.ino: In member function 'virtual void SensorGPS::run()':
arduino_sketch:37: error: request for member 'read' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'
arduino_sketch:44: error: request for member 'write' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'

如果在设置变量

时删除*
SoftwareSerial serialGPS(10,11); // RX, TX

变量

的结果错误
arduino_sketch:21: error: expected identifier before numeric constant
arduino_sketch:21: error: expected ',' or '...' before numeric constant

这个问题适用于所有需要值作为初始化的类。另一个使用Dht11模块的例子(1);生成相同的错误

请注意示例Dht11库的相同问题。所以我改变了构造函数(库https://github.com/adalton/arduino/tree/master/projects/Dht11_Library)。

示例Dht11打开"Dht11.h"文件从<<strong>/strong>

Dht11(uint8_t newPin) : humidity(-1), temperature(-1), pin(newPin) { }

To(带setPin函数)

Dht11() : humidity(-1), temperature(-1), pin(-1) { }
void setPin(uint8_t newPin) {
   pin = newPin;
}

所以现在在Arduino草图中,我可以在使用前设置引脚。这个解决方案对于大多数库都是可行的。

class SensorTemperatureHumidity: public Thread
{
  private: 
        static const int Pin = 3;
        Dht11 module;
  public:
        SensorTemperatureHumidity() {
          module.setPin(Pin); // Set my pin usage
        }
}

最新更新