m5stack / M5StickC-Plus

M5StickCPlus Arduino Library
MIT License
359 stars 90 forks source link

If (Serial) always returns true #56

Closed sylque closed 2 months ago

sylque commented 8 months ago

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

Additional context

No response

Issue checklist

Tinyu-Zhao commented 2 months ago
image

Perhaps you just missed that message, establishing a serial connection takes time, and the content in setup will only execute once.

sylque commented 2 months ago

@Tinyu-Zhao, I'm not sure I get your point. Sure, you can wait 2 seconds at startup or, even better, use ESP32 log primitives (log_printf, etc.). But those workarounds are no fix for people who expect Arduino code to work among platforms.

Tinyu-Zhao commented 2 months ago

@Tinyu-Zhao, I'm not sure I get your point. Sure, you can wait 2 seconds at startup or, even better, use ESP32 log primitives (log_printf, etc.). But those workarounds are no fix for people who expect Arduino code to work among platforms.

Your device in the establishment of a serial port connection with esp32 takes time, and the serial port initialisation is very fast, successful initialisation that is the implementation of the output, the output information is likely to be in the serial port connection is established before you naturally will not see any output.