Preprocessor define to know if Serial exists

yeahtuna

Well-known member
For production builds, I remove the Serial interface. Is there a way I can tell if Serial exists by checking a macro?

For example, in SDFatConfig.h, I would like to do something like the following:

Code:
#ifndef USB_SERIAL_DTR
#if ENABLE_ARDUINO_SERIAL == 1
#warning Redefining ENABLE_ARDUINO_SERIAL 0 because Serial is not defined.
#undef ENABLE_ARDUINO_SERIAL
#define ENABLE_ARDUINO_SERIAL 0
#endif
#endif

That USB_SERIAL_DTR is defined in usb_serial.h, but it doesn't seem to be defined when compiling the USBHost_t36.
 
It may depend on which serial interface:
For main serial you might try: #if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
For Serial emulation you could check for: #if defined(SEREMU_INTERFACE) && !defined(CDC_STATUS_INTERFACE) && !defined(CDC_DATA_INTERFACE)
 
Thanks, KurtE. Do you think there's any chance of getting this added to Teensy cores?
Code:
#ifndef __TEENSY_AVAILABLE_CHECK__
#define __TEENSY_AVAILABLE_CHECK__

#include "Arduino.h"

// Check for hardware Serial
#if defined(CDC_STATUS_INTERFACE) && defined(CDC_DATA_INTERFACE)
#define TEENSY_HARDWARE_SERIAL_AVAILABLE
#endif

// Check for emulated Serial
#if defined(SEREMU_INTERFACE) && !defined(CDC_STATUS_INTERFACE) && !defined(CDC_DATA_INTERFACE)
#define TEENSY_EMULATED_SERIAL_AVAILABLE
#endif

// If either interface exists, define TEENSY_SERIAL_AVAILABLE
#if defined(TEENSY_HARDWARE_SERIAL_AVAILABLE) || defined(TEENSY_EMULATED_SERIAL_AVAILABLE)
#define TEENSY_SERIAL_AVAILABLE
#endif

#endif
View attachment teensy_serial_available_check.h
 
Back
Top