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?

asked Nov 27, 2016 at 10:12
1
  • Make classA a pointer and create the new object with new. Commented Nov 27, 2016 at 17:07

2 Answers 2

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();
answered Nov 27, 2016 at 17:26
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()>. Commented Nov 27, 2016 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");
}
answered Nov 27, 2016 at 11:19
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. Commented Nov 27, 2016 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... Commented Nov 27, 2016 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 ? Commented Nov 27, 2016 at 16:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.