HardwareSerial SERIAL_9BIT_SUPPORT

sicco

Well-known member
I have several Teensy4 projects that work with HardwareSerial.h NOT edited with SERIAL_9BIT_SUPPORT. I do not want to change anything to them. They're out in the field and if needed I should be able to recompile them without issues.

I now have a new project that needs SERIAL_9BIT_SUPPORT.

So I have to edit HardwareSerial.h. But if I do that while it's in my PC's C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4 folder, then I likely corrupt things for my other projects that do not want or need the 9 bit UART support (which eats up more buffer RAM bytes if 9bit mode is enabled).

So I tried making a local copy of HardwareSerial.h in the Arduino new project folder.
Added
#include "HardwareSerial.h"
as the first line in my new Arduino project .ino file.

But that does not do the trick.
Problem is that by default the original library file gets used, like it or not, it then does already
#define HardwareSerial_h
way before anything that I could do in my Arduino .ino code.

What is the workaround? How do I tell my new project to stay away from the default libraries - without having to change names for HardwareSerial, so that I can compile a sketch that uses Serial1.begin(115200, SERIAL_9N1); without errors? WITHOUT possibly corrupting things for my other sketches maintained on the same PC that will not want it.
 
You could try to put your version of HardwareSerial.h and HardwareSerial.cpp into your project folder along with the .ino file that calls them.
It might be further sensible to change their names to HardwareSerial9 .h and .cpp.
 
That is what I tried. But it does not work.

Issue is that when compiling, the first time something includes the library version of HardwareSerial.h, that "HardwareSerial_h" flag gets set. That effectively blocks any subsequent attempts for redefining what is then defined already. Because in the .h file it says:

#ifndef HardwareSerial_h
#define HardwareSerial_h
... do what should be done ...
#endif

So the second time around "do what should be done" will be ignored.

If I brute force disable that check, then I get all sorts of other compiler errors because I'm attempting to recreate already created instances of classes and god knows what...

If I change the file names, then also that will not work because in the new files I'd need to change the names also for that flag "HardwareSerial_h", "extern HardwareSerial Serial1; extern void serialEvent1(void);" and so on....
 
That is what I tried. But it does not work.

It can work, but you must change all global scope names so there is nothing with the same (global scope) names as the original HardwareSerial.h.


Issue is that when compiling, the first time something includes the library version of HardwareSerial.h, that "HardwareSerial_h" flag gets set. That effectively blocks any subsequent attempts for redefining what is then defined already.

This issue means you didn't copy and rename **all** global scope items.

Please give it another try. If you still can't get it to work, we can try to help, but the only effective way to do so is if you show us your modified files. Nobody can guess which global scope things you missed if the modified code is invisible.
 
@Paul, are you trying to say that if I want to use 9BITS UARTs and I also want to keep my older projects that use the default 8BITS-only UARTs unaffected and recompile-able on the same PC, that I can no longer use the names Serial1, Serial2 etc?
So instead of Serial1, I must use for example Nine_Bits_Serial1 instaed?
And rename IRQHandler_Serial1() into IRQHandler_Nine_Bits_Serial1()...
And rename HardwareSerial into Nine_Bits_HardwareSerial..
And rename serialEvent1 into Nine_Bits_serialEvent1...
And and and.
Not only in the .h but also in the .c of HardwareSerial??

Come on...
There must be an easier way out here.
Any guidance appreciated.
Attached in next post my sketch that I cannot get to compile, with its .ino, the .h and the .c as copied from the library with only in the .h the 9BITS define uncommented.
View attachment sketch_aug26a-001.zip
 
I do want to edit code to enable 9 bit mode. But I do not want to see that change propagating into (=possibly corrupting!) all my other Teensy4 projects. Is it not possible to let a project use all of the default stuff from the one and only library on my PC, but tell it that there's one exception: just for this project only, ignore the library version but use what's in the sketch folder right next to the sketch .ino file?
 
Is it not possible to let a project use all of the default stuff from the one and only library on my PC, but tell it that there's one exception: just for this project only, ignore the library version but use what's in the sketch folder right next to the sketch .ino file?

The code was not designed to do that. The clear answer is the convenience you desire is not possible.

There are many possible ways you might deal with this situation, but none will meet all of your wishes. You will need to settle for something which is less than everything you wanted.
 
Thanks for the clarity and the responsiveness.

Can you elaborate which 'lesser' method you would use yourself when dealing with this situation? Like maybe use another PC just for the 9bit_uart projects? Or another Arduino install with its own library in another folder? Another disk partition? A USB drive install to isolate from the default libraries? I'm just thinking aloud here. I am more than happy to compromise on my desires, and would welcome expert suggestions on how to do it smarter than the tree crude resolutions I just listed, and smarter than renaming dozens of variables and file names.
 
I found a workaround. But I'm not 100% sure what exactly was the crux that allowed the workaround to work.
Sketch attached.

View attachment exp26aug-220826a.zip

I had to copy to my Sketch folder these files from the library folder C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4
HardwareSerial.c
HardwareSerial.h
WProgram.h
Arduino.h

Then I made the edit in HardwareSerial.h in my sketch folder so that it is set for 9 databits bit UART support. So uncomment line 37.
Then I added a new dummy class. Trick.h and Trick.c. In there I import "Arduino.h" and "WProgram.h" and the edited "HardwareSerial.h". In the new class Trick I added a setup_N91 method as the only method in the classroom.
And then it works the way I wanted. While keeping the library version for HardwareSerial.h unchanged. So that my other sketches will not suffer from such edits in the libraries. And so that this sketch will not suffer with every library update that I always incautiously accept.

But I'm still confused. Was creating the Trick class really necessary? Was maybe an import of WProgram.h and/or Arduino.h already enough?
And why do I need to copy HardwareSerial.cpp also to my sketch folder?
It's bedtime now...
 
Answering part of my own question: no, the Trick class is not what does the trick. Adding #include "Arduino.h" as the first line of code in my .ino AND copying that Arduino.h plus WProgram.h into the sketch folder is what makes the difference between failure and success. That still puzzle me. But it really is bed time now.
 

Attachments

  • exp26aug-220826b.zip
    12.9 KB · Views: 26
Back
Top