Go Down

Topic: Where's the (hardware) "Serial" object instantiated in the Arduino ecosystem? (Read 3283 times) previous topic - next topic

snoop911

Looking at the SoftwareSerial example ( arduino.cc/en/Reference/SoftwareSerial ),  I see setup() uses a 'Serial' object that was never declared:

Serial.begin(57600);

It does globally declare a software-based Serial object:

SoftwareSerial mySerial(10, 11);
mySerial.begin(4800);

but where is the original 'Serial' object at?  I'm sure it's buried deep within some core library cpp

Also is there a reason why someone would want to use a Software Serial instead of the (hardware Usart) Serial ?

Thanks!


rogerClark

Serial is created by default as part of the Arduino infrastructure, in the same way as it has the ISR's that work the millis() function for time keeping etc.

The reason there is Software serial, is for use on devices like the ATMega328 e.g in the Uno, which only have one hardware serial device, - which is connected to the PC
So if you need the Uno to communicate to another Serial device e.g. a Serial Bluetooth module , you need to use Software serial which gives you serial on pins other than D0 and D1 (which are hard wired to the PC on the Uno etc).
Freelance developer and IT consultant
www.rogerclark.net

PeterH


but where is the original 'Serial' object at?  I'm sure it's buried deep within some core library cpp


The Serial object is declared in HardwareSerial.h and defined in HardwareSerial.cpp. On platforms that have multiple hardware serial ports, these files also define Serial1, Serial2 and so on. It also defines Serial_ for the odd platforms where there is a USB virtual serial port. HardwareSerial.h is automagically #included in your sketch via the #include Arduino.h which the IDE inserts for you before compilation.

snoop911

I'm trying to migrate over to AtmelStudio and I included HardwareSerial0.cpp/HardwareSerial.h which has the Serial defined and extern'd

but AtmelStudio still complains of
" 'Serial' was not declared in this scope."

Is this a C++ thing?   How do you use the Serial object declared in HardwareSerial0.cpp/h, if it's not just including the file?




pYro_65

The Serial variables are declared extern in HardwareSerial.h as you say.

Before you include the header in your code, do you make the defines visible to HardwareSerial.h

Quote
#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
  #define HAVE_HWSERIAL0
#endif


Maybe write some code using the defines in your local code, see if they are actually visible.
Forum Mod anyone?
https://arduino.land/Moduino/

PeterH


I'm trying to migrate over to AtmelStudio and I included HardwareSerial0.cpp/HardwareSerial.h


Rather than #including individual files such as HardwareSerial.h I suggest you just #include Arduino.h, which will include the other files needed. (Note that you'd have to include that in each compilation unit i.e. each .c/.cpp source file that referred to Arduino declarations.

MarkT



but where is the original 'Serial' object at?  I'm sure it's buried deep within some core library cpp



That's the kind of question to ask grep, not the forum!  You have the source code and
a computer capable of searching it...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

snoop911

Adding 'Arduino.h' fixed it!

Interestingly, in HardwareSerial.h, the  UBRROH symbol is defined (in iomxx0_1.h) and this code gets compiled:

#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSeddrial Serial;
  #define HAVE_HWSERIAL0
#endif

yet, if try to use the same UBRR0H symbol in my local code it doesn't see it!   

I suppose it's academic at this point, but it's interesting that HardwareSerial.h doesn't 'include' any file that has the UBRRH/UBRR0H, so why wouldn't my local code see it?





pYro_65

I suppose it's academic at this point, but it's interesting that HardwareSerial.h doesn't 'include' any file that has the UBRRH/UBRR0H, so why wouldn't my local code see it?


The defines get included from an include in Arduino.h

The serial stuff is included after it. As includes are copy/paste jobs, Arduino.h becomes a long file with the UBRRH stuff first then the Serial, so its visible there.

For example, Func() will be defined as the include order allows SOME_DEFINE to be visible before its used ( if you include myheader.h ):

Code: (myfunc.h) [Select]
#ifdef SOME_DEFINE
  inline void Func(){ /*some action*/ };
#endif


Code: (myheader.h) [Select]
#define SOME_DEFINE
#include "myfunc.h"


Forum Mod anyone?
https://arduino.land/Moduino/

Go Up