Using Serial in a library

I ran into a problem when trying to use Serial from a library. Only two characters get outputted and then nothing else happens. Here is my code.

Here is my header called TestLib.h

class TestClass
{
public:
TestClass();
void Print();
};

Here is my cpp file called TestLib.cpp

#include <TestLib.h>
#include <HardwareSerial.h>

TestClass::TestClass()
{
Serial.begin(9600);
Serial.println("Loaded");
}

void TestClass::Print()
{
Serial.println("Beat");
}

Here is my sketch

#include <TestLib.h>

TestClass myclass;

int led = 13;

void setup()
{
pinMode(led, OUTPUT);
}

void loop()
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);

myclass.Print();

Serial.println("Beat loop");
}

When I check the serial monitor I get the following: “Lo”. That’s it. Pin 13 blinks the whole time so I know I am in the loop. Heck, it’s blinking now, but nothing shows up.

I am using a Mega 2560 R3.

Any ideas? I’m stumped on this one.

Move the contents of 'TestClass::TestClass()' into a class member named 'TestClass::begin()'.

Call 'myclass.begin()' from within 'setup'.

Your global object 'myclass' gets constructed BEFORE the Arduino API have been initialized.

So globals are constructed THEN 'main' is called which then in turn calls 'init' (which initializes the Arduino API's) then turns control to your 'setup' setup return then your 'loop' function is continually called in a for-ever loop.

EDIT: Sorry if that doesn't make sense - I'm to tired to write coherently

which initializes the Arduino API's

No, it doesn't. It initializes the hardware, including the serial port.

OP: The problem with the code that you have written is that you don't know if the HardwareSerial class has been instantiated when your class is called. So, Serial (an instance of HardwareSerial) may not have been constructed yet. It might still be NULL. Calling begin() on NULL is really not a good idea.

Moving the code in the constructor, as lloyddean suggests, assures that Serial is not NULL when you call the Serial.begin() method, assuring that it does what it is supposed to.

You may also find flush() useful to ensure that all the text has actually been sent down the wire before you move on to the next item...

Rgds

Damon

You may also find flush() useful to ensure that all the text has actually been sent down the wire before you move on to the next item...

Why? Serial data transmission is supposed to be asynchronous. Forcing it to be synchronous doesn't feel right.

It's not "supposed" to be anything. And I also said "may", ie depends on circumstances and taste.

It may be important to ensure:

a) that everything is sent before the Serial subsystem is turned off or reset with the .begin() for example

b) that huge amounts of stuff isn't being queued unexpectedly, or even causing overruns and loss later. The fact that only some of the output was seen was what made me think that flush() might help pin it down. Synchronous behaviour is easier to understand and debug.

Rgds

Damon

PaulS:

which initializes the Arduino API's

No, it doesn't. It initializes the hardware, including the serial port.

Actually in my opinion it does - maybe not Arduino hardware, but on Arduino "compatible" API devices using other architectures indeed it does as it also has to do some emulation of Atmel hardware features.