Serial.print only print 2 characters when encapsulated in a class

Below is the simplified code (taken from a much more complex project but illustrates the issue nicely). If I run the begin and print command within the setup() I get 'hello' on the serial monitor. If I run it as below, I get 'he'. I am new to C++/Arduino but am a programmer and this has me scratching my bald patch....suspect it is something obvious. Am running on a UNO R3.

class commsInterface{
  public:
  commsInterface();
};

commsInterface::commsInterface() {
  Serial.begin(9600);
  Serial.print("hello\n");
};

commsInterface comms;

void setup() {
}

void loop () {
}

The Arduino software environment is not ready for use until main calls setup. Best not to use the Serial object in your constructor.

DavidOConnor: The Arduino software environment is not ready for use until main calls setup. Best not to use the Serial object in your constructor.

The conclusion is correct. The premise is wrong. It is the hardware, like the UART, that is not ready until init() has been called, just before setup() (and long after your constructor(s)).

PaulS:

DavidOConnor: The Arduino software environment is not ready for use until main calls setup. Best not to use the Serial object in your constructor.

The conclusion is correct. The premise is wrong. It is the hardware, like the UART, that is not ready until init() has been called, just before setup() (and long after your constructor(s)).

I beg to differ. The hardware is ready to use. The OP can get serial output from his constructor by writing to the USART registers.

I beg to differ.

Beg all you want. "The Arduino software environment" is on the PC and has nothing to do with the Arduino hardware.

Thanks all. I fixed this by moving the constructor code into a new init() method which I called from setup()

commsInterface comms;

void setup() { comms.init(); }

Thanks all. I fixed this by moving the constructor code into a new init() method which I called from setup()

The convention is to call that method begin(), as in Serial.begin().

Noted and amended, thank you. Is this an Arduino specific convention - and where can I learn these conventions ?

You can view the source and see how the program actually starts:

https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/main.cpp#L22 http://arduino.land/FAQ/content/2/2/en/can-i-use-int-main-with-arduino.html

Is this an Arduino specific convention

Yes.

and where can I learn these conventions ?

Good question. In my experience, that phrase indicates that a lousy answer is about to follow. This is no exception.

PaulS:

Is this an Arduino specific convention

Yes.

and where can I learn these conventions ?

Good question. In my experience, that phrase indicates that a lousy answer is about to follow. This is no exception.

hmmm, yes, and if you search the forums, you are likely to find 47000 other lousy answers.

SolidGuy:
Noted and amended, thank you. Is this an Arduino specific convention - and where can I learn these conventions ?

Take a look at this…

http://arduino.cc/en/Reference/APIStyleGuide

DavidOConnor:

SolidGuy: Noted and amended, thank you. Is this an Arduino specific convention - and where can I learn these conventions ?

Take a look at this...

http://arduino.cc/en/Reference/APIStyleGuide

Interesting. I'd never seen that. I disagree with a lot of it.

PaulS: Interesting. I'd never seen that. I disagree with a lot of it.

Me neither, and me too.

Some of these run counter to professional programming practice. We’re aware of that, but it’s what’s made it possible for so many beginners to get started with Arduino easily.

It suggests that many of the design mistakes in the Arduino API were made deliberately rather than out of ignorance, which is a little surprising.