Help replacing hardware serial libraries

I use several Mega2560 boards in a home automation system and use three to four serial ports on each. I would like to reduce my RAM usage, so I'd like finer control of the transmit and receive buffer sizes for each port rather than the "one-size-fits-all" approach of the core code. To that end I've written new serial functions and ISRs to replace the core hardware serial port code. When I compile and link them however, I get an error regarding multiple definitions for the RX and TX interrupt vectors since my ISRs and the core code ISRs use the same ones.

I either need a way to remove the hardware serial core code from the build or a way to resolve the conflict with the vectors. I've looked but can't find anything like a makefile or other script which I could modify to prevent building the core serial port code.

Any help would be greatly appreciated. Thanks in advance.

You have not posted your program .... and I can't see over your shoulder to read your screen.

AFAIK if you don't have Serial.begin() the library is not included.


I had a similar problem with my HardwareSerial replacement class, NeoHWSerial, which just adds an interrupt callback function. You have to make sure that there are no references to Serial anywhere in your sketch, not even in the libraries you use. See notes on the github page for NeoHWSerial.


Thanks for your prompt replies.

Here is my code. Though it doesn’t call any of the Serial functions, the core serial files are compiled and linked-in resulting in the ISR vector conflict.

BTW, the stdahk.h file is completely unrelated #defines and typedefs.

Again thanks for your help.

hardware_serial.ino (4.4 KB)

Well, there must be a reference somewhere. I would move all the Libraries' subdirectories somewhere else, keeping just the libraries you really use. Move them all if you don't use any in your sketch.

Then use a search tool, like grep, to see if "Serial" appears anywhere in your code or in the Libraries. That's the only reason the linker pulls in the HardwareSerial files, causing the conflict.

I have not had to remove anything from the Arduino install directory nor the user Arduino/hardware directory. This says that the core does not require Serial.

Thanks, I'll look again.

OK, so for some reason, building the core pulls-in the serial code regardless of whether it's used or not. The sketch below merely flashes the onboard LED but caused the serial files to be compiled and linked. Any ideas as to how to prevent this from happening?

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

pinMode( 13, OUTPUT );

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

digitalWrite( 13, LOW );
delay( 1000 );
digitalWrite( 13, HIGH );
delay( 1000 );

It would be helpful if you would provide a simplified self contained sketch that demonstrates the issue. I can't compile your hardware_serial.ino. If I strip it down then it compiles fine for me. If I add in a call to Serial.begin() then I get the multiple definitions error. We also need to know which board you're compiling for and which version of the hardware package for that board you're using.

It's possible the serialEventRun() call in main() might be pulling in HardwareSerial.h but, as I said, my tests indicate that doesn't cause your issue.

It's possible the serialEventRun() call in main() might be pulling in HardwareSerial.h but, as I said, my tests indicate that doesn't cause your issue.


@ALKVA, have you disabled link time optimizations or changed other switches? Maybe that causes the "weak" attribute on SerialEvent to be ignored.

What IDE version?

You can use a command line to dump the assembly code version of your LED blink program:

   "C:\Program Files\Arduino 1.6.8\hardware\tools\avr\bin\avr-objdump.exe" -S myprog.elf > t.txt

Then search the output for Serial. I'll be really confused if only the ISRs appear.



Thanks again for your assistance.

I'm running version 1.6.12 of the IDE, using boards package version 1.6.14, and compiling for the Mega2560 board. I have not knowingly changed any compile/link switches or disabled link-time optimizations. I have not enabled code folding.

I'll try commenting out serialEventRun() and examining the object file.

Again, many thanks.

OK here's an update. So I commented out the serialEventRun() function in main and, as expected, it had no effect.

I reviewed my code yet again and there are no calls to any Serial functions anywhere yet when I dump the resulting object file the Serial routines get pulled in (after I commented out my replacement ISR functions).

I tried removing the arduino hardware serial core source and include files from the directory yet they still get compiled. How can that possibly be the case???? Is there a way to alter a build script to not include them or some other way to prevent them from being built?

I'm using the SPI, WIRE, SD, and EEPROM libraries.

I'm really confused.

OK, at least part of the mystery is solved. The file SdFatUtil.h, associated with the SD library, contains obscure functions that call Serial.write() which pulls-in the hardware serial libraries. SdFile.cpp also has some functions that call Serial.write().

I'm pretty sure I can do without these functions so I'll just alter the .cpp and .h files to remove them.

So... reply #2 & #4? :wink:

Glad you found it. grep is your friend. I use wingrep.