Serial Reference doesn't work for .begin()

hi all

I did the reference to serial because with reference I can write myserial.print();

Stream &pc = Serial;
Stream &gsm = Serial1;

but with reference I cannot write gsm.begin();

how fix this (not much complicated) that I could write and my.print and my.begin ?

Do you really need the more general Stream class? Because the Stream class does indeed not contain a begin() method. Otherwise you could just use HardwareSerial as type which has Stream as parent.

but with reference I cannot write gsm.begin();

You have our permission to.

Why can't you?

It would be worth your while to pay a visit to the fine folks at http://snippets-r-us.com, for help with your snippets.

septillion:
Because the Stream class does indeed not contain a begin() method.

yes exactly, that why I ask your help. Stream does not contain begin. But Serial contain but not name a type.

septillion:
you could just use HardwareSerial as type which has Stream as parent.

I cannot use HardwareSerial for Serial invalid initialization of reference of type 'HardwareSerial&' from expression of type 'Serial_'. only for Serial1

On what board? On the normal boards Serial1 is just another HardwareSerial. If it's not you have two options:

  1. just use the correct type. I would say that's preferred.
  2. Cheat and cast it to HardwareSerial type.

It's time you told us what Arduino you are programming.

hi all

septillion:

  1. just use the correct type. I would say that's preferred.

what will be correct type for Serial reference?
as I mentioned above
Stream &pc = Serial;
pc.begin(57600); //will get error here

I'm using micro, nano, and pro mini, and is preferred that code will be the same for all boards

Stream (an abstract class) does not have a .begin method because not all classes that inherit from it need a .begin(). I suppose, in retrospect, it could have been defined with a virtual .begin() function that could be overridden (or not) by inheriting classes:

   virtual void begin(uint32_t i) {}

But, that wasn’t done. So, there you are. If you need to call a .begin function then you need to do it on a pointer (or reference) to the proper class.

I'm using micro, nano, and pro mini, and is preferred that code will be the same for all boards

The code that passes the reference to your code should have already invoked the begin() method for the object. Why do you think your code needs to?

For Serial that will be HardwareSerial.

Can't really say for Serial1 as micro, nano, and pro mini don't have any... If you would use software serial that would be SoftwareSerial.

If you would have the need for different types in different board you could place it in pre-processor blocks to define them as the correct type.

@septillion
The micro is based on 32U4 and has both Serial (native USB) and Serial1 (uart).

sterretje:
@septillion
The micro is based on 32U4 and has both Serial (native USB) and Serial1 (uart).

Be aware though, that the man page incorrectly says the micro's pin 0 and 1 are for Serial: they should be in the next column as Serial1.

micro_serial1_pins.GIF

micro_serial1_pins.GIF

Thanks both, I was aware it's based on a 32U4. I have a couple but it's not my usual goto. So yeah, I was mislead by the man page...

So on a Micro Serial1 is a HardwareSerial and Serial is of type Serial_.

PS Now I know where the stupid counting comes from. Even the IDE does it :s Serial, Serial1, Serial2... Arg

septillion:
So on a Micro Serial1 is a HardwareSerial and Serial is of type Serial_.

yes. and how with one (the same) instruction (declaration) to make reference for Serial and for Serial1

HardwareSerial is not valid for Serial. or its impossible to make with the same type/declaration ?

You'd do it just like your original post:

Stream &pc = Serial;
Stream &gsm = Serial1;

That's perfectly valid. But, Serial_ inherits directly from Steam. It apparently does not require a .begin() method. And, you have been told Stream does not have a .begin() method. You can't do what you're trying to do because what you're trying to do want is simply wrong.

The answer was given to you in Reply #8. The setup() code should call the .begin() -- if required -- on the specific object (not on a reference to it, not on a base class) right at start up. I'd probably use pointers to Stream rather than references. That way you could assign them in the same part of setup(). After that, the remainder of the code is agnostic to the actual type of Stream being pointed to.

If you want to support multiple boards in the same code, use compiler '#if' directives to provide the required versions of the above setup() functionality.

so if I understand you correct, you suggest like this?

Stream &pc = Serial;
Stream &gsm = Serial1;

setup{
Serial.begin(57600);
}

loop{
pc.print();
}

#if
which instruction /register can help me determine the board/famaly mcu?

I'd do something like this:

#include "SoftwareSerial.h"

Stream *pc;
Stream *gsm;

void setup() {
#if defined(ARDUINO_AVR_MICRO)
  Serial.begin(115200);
  pc = &Serial;
  gsm = &Serial1;
#elif defined(ARDUINO_AVR_UNO)
  Serial.begin(115200);
  pc = &Serial;
  SoftwareSerial *tempPtr = new SoftwareSerial(10, 11);
  tempPtr->begin(9600);
  gsm = (Stream *)tempPtr;
#else
#error Unsupport Board
#endif

  pc->println("Hello World");
  gsm->println("Hello World");

}

void loop() {
}

got you! thnx

Hello guys!

I can't understand.

when we have Arduino Micro with native USB then Serial instance inherits from Serial_ from CDC.cpp

But when we have Arduino Nano. I can't understand from where Serial inherits. I know from hardwareserial.cpp
But I dont see something lik e this

HardwareSerial Serial;

how it inherits?

Search your Arduino installation for the file ‘HardwareSerial0.cpp’.