SoftwareSerial *serial -> readdata(Stream *s)

How to please pass the pointer serial to Stream *s inside class?

class test {
    SoftwareSerial *serial 
    
    void begin() {
       serial->begin(9600);
       if  (readdata(&serial)) {
      }
    }
    
    boolean readdata(Stream *s) {
      if (! s->available()) return false;
      return true;
    }
};

just readdata(serial)

I wouldn't restrict it to SoftwareSerial. Instead, I'd make it more general by passing a reference to a Stream object into the class's constructor:

#include <SoftwareSerial.h>

class Test {
  public:
    Test(Stream &str) : serial(str) {}

    void sayHello() {
      serial.println("Hello World");
    }

  private:
    Stream &serial;
};

SoftwareSerial softSer(4, 5);
Test instance(softSer);

void setup() {
  softSer.begin(9600);
  instance.sayHello();
}

void loop() {
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.