From 7e788c4ae9e1d1dff669c0359514f2e9c8b0a43a Mon Sep 17 00:00:00 2001 From: kurte Date: Sat, 17 May 2025 13:33:57 -0700 Subject: [PATCH] If sketch is not connected to serial - dump serial output Current code would hang after a few Serial.print like statements happened when we are not connected to the Serial monitor. So I put overrides in for the Serial.write(buf, cnt) and Serial.flush() That would first check to see if Serial returns true (DTR)... This appears to match the behavior of the MBED Portenta H8 code. --- cores/arduino/SerialUSB.h | 2 ++ cores/arduino/USB.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/cores/arduino/SerialUSB.h b/cores/arduino/SerialUSB.h index 94d5cc83..442d28d0 100644 --- a/cores/arduino/SerialUSB.h +++ b/cores/arduino/SerialUSB.h @@ -18,6 +18,8 @@ class SerialUSB_ : public ZephyrSerial { void begin(unsigned long baudrate) { begin(baudrate, SERIAL_8N1); } operator bool() override; + size_t write(const uint8_t *buffer, size_t size) override; + void flush() override; protected: uint32_t dtr = 0; diff --git a/cores/arduino/USB.cpp b/cores/arduino/USB.cpp index 6f45e1f2..60f5fc7a 100644 --- a/cores/arduino/USB.cpp +++ b/cores/arduino/USB.cpp @@ -124,5 +124,16 @@ arduino::SerialUSB_::operator bool() { return dtr; } + +size_t arduino::SerialUSB_::write(const uint8_t *buffer, size_t size) { + if (!Serial) return 0; + return arduino::ZephyrSerial::write(buffer, size); +} + +void arduino::SerialUSB_::flush() { + if (!Serial) return; + arduino::ZephyrSerial::flush(); +} + arduino::SerialUSB_ Serial(usb_dev); #endif