1

My sketch looks somehow like this:

ClassA classA;

void setup() {
    Serial.begin(9600);

    classA = ClassA();
    Serial.println("Bar")

    ...
}

void loop() {
    classA.functionFoo();
}

The constructor of ClassA however calls some internal methods that perform a Serial.println().

My problem is, that the first (implicit) call of the constructor of ClassA comes in the first line of the sketch with

ClassA classA;

This is before the Serial interface has been initialized (with Serial.begin(9600)). Therefore the Serial.monitor cannot interpret the first messages coming from the serial interface.

How can I either suppress any Serial.writes before the setup function or otherwise make sure that the Serial interface is initialized before the setup function?

1
  • Make classA a pointer and create the new object with new.
    – Majenko
    Nov 27 '16 at 17:07
2

You can make your current object a pointer so it isn't instantiated:

ClassA *classA;

Then create an object with new:

classA = new ClassA();

The constructor is then only called when the object is made.

Note that you will have to change your accessors to ->:

classA->myFunction();
1
  • 1
    Adding the classA = new ClassA(); in the setup() function just after calling Serial.begin(9600); will be a quite good solution, but as for the use of bool bSerialReady solution, the rework will be important (replacing classA.<function()> by classA-><function()>.
    – J. Piquard
    Nov 27 '16 at 18:31
2

In order to be sure that your classA object doesn't use the Serial.println() function before the baudrate is defined, it is necessary to add functions in your class ClassA and to inform from setup() your classA object.

The simplest way is to add:

  1. a boolean bool bSerialReadyto keep the status of the Serial object,
  2. in all ClassA constructors, initialize that bSerialReady = false;,
  3. add a function void SetSerialReady() to switch the status bSerialReady = true;. The function should be called in the setup()function just after Serial.begin(9600);,
  4. and in all functions of ClassA, add a if-condition if (bSerialReady) before calling the Serial.println().

Your class ClassA will become:

class ClassA
{
private:
    bool bSerialReady;
public:
    ClassA() {
        // ...
        // Initialize bSerialReady
        bSerialReady = false;
    };
    void SetSerialReady() {
        bSerialReady = true;
    };
};

Your setup() function becomes:

void setup() {
    Serial.begin(9600);
    classA.SetSerialReady();
    ...
}

And in functions of ClassA, it looks like:

if (bSerialReady) {
    Serial.println("Bar");
}
3
  • 1
    Plus, you'll probably want some logic to store and re-issue the messages that would have been issued before bSerialReady became set.
    – JRobert
    Nov 27 '16 at 12:15
  • not a bad solution, however, it is only about debug messages. I wouldn't want to change my code too much for them...
    – speendo
    Nov 27 '16 at 16:10
  • @speendo, could you provide an example of your class ClassA source code using the Serial.println() function to display debug message ? Did you have used the same template in the rest of the source code ?
    – J. Piquard
    Nov 27 '16 at 16:26
-2

Try something like this

void setup() {
      // nothing here
    }

void loop() {

    Serial.begin(9600);
    ClassA classA;
    while(1){


    }
}

Move everything inside setup to the loop just before while(1).

3
  • This would work, I guess. It's not very elegant, however.
    – speendo
    Nov 27 '16 at 16:11
  • then why down commented ! Does this work for you dear friend ? Nov 30 '16 at 8:35
  • I didn't downvote! I used another solution, but this would probably work.
    – speendo
    Dec 2 '16 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.