M5StickC-Plus icon indicating copy to clipboard operation
M5StickC-Plus copied to clipboard

If (Serial) always returns true

Open sylque opened this issue 2 years ago • 0 comments

Describe the bug

According to Arduino's documentation:

if(Serial) Description Indicates if the specified Serial port is ready. On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true.

The M5StickC-Plus has native USB, so I think if (Serial) should return true only when a USB connection is established. This doesn't seem to be the case.

To reproduce

Use this code from Arduino's documentation:

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }

  // Display something in the terminal
  Serial.println("Hello World");
}

void loop() {}

This code doesn't display "Hello World" in the terminal, because the line Serial.println("Hello World"); is called before the USB connection is established.

Expected behavior

You should see "Hello World" in the terminal.

Screenshots

No response

Environment

  • OS: Windows
  • IDE &IDE Version: VSCode + PlatformIO
  • Repository Version: latest

Additional context

No response

Issue checklist

  • [X] I searched for previous reports in the issue tracker
  • [X] My report contains all necessary details

sylque avatar Jan 11 '24 21:01 sylque