How to use Teensy USB Serial in external library?

Status
Not open for further replies.

Projectitis

Well-known member
Hi all,

I'm modifying a third-party library to work with Teensy, and I'm updating code that writes to stout (debug code using printf mainly) to Serial.printf and equivalents.
Snippet of code is something like this:

Code:
#include <usb_serial.h>

#ifdef HAS_DEBUG
#define DEBUG(fmt, ...) do { Serial.printf("%s(): " fmt "\n", __func__, __VA_ARGS__); } while(0)
#else
#define DEBUG(...)
#endif

However, I'm getting the compiler error:

Code:
error: 'Serial' undeclared

I've also tried including <Arduino.h> and alternatively <HardwareSerial.h> but no avail.
How do I get Serial output to work in an external library like this?

Cheers,
Peter
 
Last edited:
include your library after the other libraries have been included, the order always matters to the compiler
#include “Stream.h” should be there as well (in your library, not the sketch)
you library needs access to the other libraries, putting includes in your sketch wont help it see anything at all
 
Might help to see the main sketch... Also things like when you build what is the USB Type set to?

There is a source file usb_inst.cpp in core that instantiates the Serial object. But these again depend on what #defines are set as part of the Serial type.

Maybe try adding something like: Serial.begin(115200) to main sketch, to sort of force it to include the USB Serial code.
 
Hmm, still can't get it to work even with a minimal example:

Create a new library called "test_serial" that contains these two files:

test_serial.h
Code:
void doSomething( void );

test_serial.c
Code:
#include <Arduino.h>

#define XM_DEBUG

#ifdef XM_DEBUG
#define DEBUG(fmt, ...) do { Serial.printf("%s(): " fmt "\n", __func__, __VA_ARGS__); } while(0)
#else
#define DEBUG(...)
#endif

void doSomething( void ){
	DEBUG("Something %d", 123);
}

And then this is my sketch:
Code:
#include <test_serial.h>

void setup() {
	// put your setup code here, to run once:
	doSomething();
}

void loop() {
  // put your main code here, to run repeatedly:

}

This is the error output:
Code:
Compiling libraries...
Compiling library "test_serial"
"C:\Program Files (x86)\Arduino\hardware\teensy/../tools/arm/bin/arm-none-eabi-gcc" -c -O2 -g -Wall -ffunction-sections -fdata-sections -nostdlib -MMD  -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -D__MK66FX1M0__ -DTEENSYDUINO=141 -DARDUINO=10805 -DF_CPU=180000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3" "-ID:\Users\peter\Documents\Arduino\libraries\test_serial" "D:\Users\peter\Documents\Arduino\libraries\test_serial\test_serial.c" -o "C:\Users\peter\AppData\Local\Temp\arduino_build_468611\libraries\test_serial\test_serial.c.o"
D:\Users\peter\Documents\Arduino\libraries\test_serial\test_serial.c: In function 'doSomething':

D:\Users\peter\Documents\Arduino\libraries\test_serial\test_serial.c:6:30: error: 'Serial' undeclared (first use in this function)

 #define DEBUG(fmt, ...) do { Serial.printf("%s(): " fmt "\n", __func__, __VA_ARGS__); } while(0)

                              ^

D:\Users\peter\Documents\Arduino\libraries\test_serial\test_serial.c:12:2: note: in expansion of macro 'DEBUG'

  DEBUG("Something %d", 123);

  ^

I'm guessing it's because I'm not telling the compiler to compile the library with the correct flags for T_3.6. How do I do this?
 
Couple of things I wonder...

Again did you try referencing something like Serial.begin in the sketch... Did not see anything...

Then the other thing is .c extension is usually setup for C programming, where as .cpp is setup for C++. So if it compiles as C, than Serial object is probably not defined... Try renaming file to test_serial.cpp

Or if you really want to do C code, then don't use Serial.print, you may be able to use the limited C USB Serial language functions.
(Look in usb_serial.h)... There are functions like usb_serial_putchar... But being C you don't have all of the stream functions, which includes the
Serial.print, Serial.printf... functions.
 
Thanks KurtE! It is working. It was as simple as:

Then the other thing is .c extension is usually setup for C programming, where as .cpp is setup for C++. So if it compiles as C, than Serial object is probably not defined... Try renaming file to test_serial.cpp

I was assuming that the compiler would not care about the file extension and would compile a mix of c/c++ (whatever was in the file). I will be very careful of that in the future!
 
Status
Not open for further replies.
Back
Top