c++ - Storing class instance in new variable causes weird behavior (Arduino Serial) -


i trying create new variable store serial object in arduino provides default.

now here don't understand:

why output of first code 334

hardwareserial serialb = serial;  void setup() {   serialb.begin(115200);   serialb.print(0x33, hex);   serialb.print(0x44, hex);   serialb.print(0x55, hex); }  void loop() {   //do nothing } 

and output of second code 334455

void setup() {   serial.begin(115200);   serial.print(0x33, hex);   serial.print(0x44, hex);   serial.print(0x55, hex); }  void loop() {   //do nothing } 

why first code stop while printing second byte? misunderstanding here? shouldn't both codes result in same output?

as dfri said, making hardwareserial instance, disastrous results.

just use reference. it's pointer, except dot notation used instead of having use arrow notation:

hardwareserial & serialb = serial; // alias, not new instance  void setup() {   serialb.begin(115200);   serialb.print(0x33, hex); 

note ampersand.


Comments