Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: HardwareSerial SERIAL_9BIT_SUPPORT

  1. #1
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29

    HardwareSerial SERIAL_9BIT_SUPPORT

    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.

  2. #2
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    963
    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.

  3. #3
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    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....

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,114
    Quote Originally Posted by sicco View Post
    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.

  5. #5
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    @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.
    sketch_aug26a-001.zip

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,114
    Quote Originally Posted by sicco View Post
    Come on...
    There must be an easier way out here.
    Yes, of course, all you need to do is edit the code to enable 9 bit mode. But you said you didn't want to do that.

  7. #7
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    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?

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,114
    Quote Originally Posted by sicco View Post
    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.

  9. #9
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    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.

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,114
    Many another copy of Arduino configured to "portable" mode.

    https://docs.arduino.cc/software/ide...ls/PortableIDE

    If you create the portable folder before running the Teensyduino installer, it will be detected by the installer and a portable-specific install is performed.

  11. #11
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    I found a workaround. But I'm not 100% sure what exactly was the crux that allowed the workaround to work.
    Sketch attached.

    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...

  12. #12
    Member
    Join Date
    Jan 2022
    Location
    Netherlands
    Posts
    29
    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.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •