user35668: I am working on an Arduino project, that would use HardwareSerial object reference stored in a class and call methods on it. However, there is problem with writing output using the reference.
Minimal use case to demonstrate behavior is in following files:
main.ino
#include <Arduino.h>    
#include "testclass.h"

TestClass testClassInstance(Serial);

void setup() {

}

void loop() {
    testClassInstance.print("Hi There\r\n");
    delay(1000);
}

testclass.h
#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <Arduino.h>

class TestClass {
public:
    TestClass(HardwareSerial &serial);
    void print(const char * text);

private:
    HardwareSerial _serial;
};

#endif

testclass.cpp
#include "testclass.h"

TestClass::TestClass(HardwareSerial &serial) : _serial(serial) {
    _serial.begin(9600);
    _serial.print("Initializing\r\n");
}

void TestClass::print(const char *text) {
    _serial.print(text);
}

After reset, there are only two characters sent: "In"
In the example above, initialization is done before setup() method and Serial's state is probably reset before calling it. Having changed that to dynamic allocation in setup method did not change this behavior as well.
modification of main.ino
TestClass *testClassInstance;

void setup() {
  testClassInstance = new TestClass(Serial);
}

There might be problem with passing the reference to Serial, but in that case I would expect to not have any output at all.
Any help is greatly appreciated.

user4143: You can't do complex tasks (or anything that relies on the Arduino API in any form) in your constructor. Simply because that all gets called before the Arduino API initializes, and in so doing the serial port gets disabled.
Instead you should do your complex operations and API-based operations in .begin(), which is why 99.99% of all libraries (including HardwareSerial) have a .begin() method.

35668: <p>I am working on an Arduino project, that would use HardwareSerial object reference stored in a class and call methods on it. However, there is problem with writing output using the reference.</p>

<p>Minimal use case to demonstrate behavior is in following files:</p>

<h2>main.ino</h2>

<pre><code>#include &lt;Arduino.h&gt;    
#include "testclass.h"

TestClass testClassInstance(Serial);

void setup() {

}

void loop() {
    testClassInstance.print("Hi There\r\n");
    delay(1000);
}
</code></pre>

<h2>testclass.h</h2>

<pre><code>#ifndef TESTCLASS_H
#define TESTCLASS_H

#include &lt;Arduino.h&gt;

class TestClass {
public:
    TestClass(HardwareSerial &amp;serial);
    void print(const char * text);

private:
    HardwareSerial _serial;
};

#endif
</code></pre>

<h2>testclass.cpp</h2>

<pre><code>#include "testclass.h"

TestClass::TestClass(HardwareSerial &amp;serial) : _serial(serial) {
    _serial.begin(9600);
    _serial.print("Initializing\r\n");
}

void TestClass::print(const char *text) {
    _serial.print(text);
}
</code></pre>

<p>After reset, there are only two characters sent: "In"</p>

<p>In the example above, initialization is done before setup() method and Serial's state is probably reset before calling it. Having changed that to dynamic allocation in setup method did not change this behavior as well.</p>

<h2>modification of main.ino</h2>

<pre><code>TestClass *testClassInstance;

void setup() {
  testClassInstance = new TestClass(Serial);
}
</code></pre>

<p>There might be problem with passing the reference to Serial, but in that case I would expect to not have any output at all.</p>

<p>Any help is greatly appreciated.</p>
 4143: <p>You can't do complex tasks (or anything that relies on the Arduino API in any form) in your constructor. Simply because that all gets called <em>before</em> the Arduino API initializes, and in so doing the serial port gets disabled.</p>

<p>Instead you should do your complex operations and API-based operations in <code>.begin()</code>, which is why 99.99% of all libraries (<em>including HardwareSerial</em>) have a <code>.begin()</code> method.</p>
