Native Serial Port- What is it

I started to play a bit with the examples that are for my new MKR GSM. At the beginning of the sketch, there was a statement associated with initiating serial communications.

 `Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
 }`

The statement causes the sketch to halt when not connected to my Windows PC. I commented it out and the sketch works just fine independently, but I really would like to know what the "Native Port" business is and why the statement is necessary for some boards:

Hi!

As nobody answered before, I´ll give you my very simple and limited explanation.

Some of the Arduino boards like Uno requires a USB to Serial bridge chip to convert info. But others like Leonardo have built-in USB communication, eliminating the need for this secondary processor. They´re said to have native USB connections.

(EDIT: This depends on the processor that each board has)

On native USB boards, if you don´t use this while(!Serial) statement, any serial output done before you open Serial Monitor will be lost.

Obviously, if your application doesn´t require Serial comms, this line must be omitted, or the program will be stuck on it forever (as you´ve already noted).

Brazilno:

Great explanation. Many thanks,
Michael

1 Like
void setup()
{
  Serial.begin(9600);
  // wait up to 5 seconds for USB to connect. 
  while (!Serial && millis() < 5000) { }

  if (Serial)
    Serial.println("Serial connection established");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.