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?
2 Answers 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();
-
1Adding the
classA = new ClassA();
in thesetup()
function just after callingSerial.begin(9600);
will be a quite good solution, but as for the use ofbool bSerialReady
solution, the rework will be important (replacingclassA.<function()>
byclassA-><function()>
.J. Piquard– J. Piquard2016年11月27日 18:31:10 +00:00Commented Nov 27, 2016 at 18:31
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:
- a boolean
bool bSerialReady
to keep the status of the Serial object, - in all
ClassA
constructors, initialize thatbSerialReady = false;
, - add a function
void SetSerialReady()
to switch the statusbSerialReady = true;
. The function should be called in thesetup()
function just afterSerial.begin(9600);
, - and in all functions of
ClassA
, add a if-conditionif (bSerialReady)
before calling theSerial.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");
}
-
1Plus, you'll probably want some logic to store and re-issue the messages that would have been issued before bSerialReady became set.JRobert– JRobert2016年11月27日 12:15:34 +00:00Commented 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...speendo– speendo2016年11月27日 16:10:43 +00:00Commented Nov 27, 2016 at 16:10
-
@speendo, could you provide an example of your
class ClassA
source code using theSerial.println()
function to display debug message ? Did you have used the same template in the rest of the source code ?J. Piquard– J. Piquard2016年11月27日 16:26:20 +00:00Commented Nov 27, 2016 at 16:26
classA
a pointer and create the new object withnew
.