Arduino wrap or Subclass print() to work with multiple SerialAES Simplified for Arduino - Having hard time achieving desired resultAdvice for checking integrity of serial char strings?How to read long value sent by Arduino in CHelp with a Memory IssueHow to break out of a loop if it is contained in a functionNeed help with the basics on serial controlWhy is integer-to-string not working in this sketch?Arduino to read from RS232 converter to TTL serial module (updated)47Effects MIDI Library and serial debuggingESP8266 sends data to the website but doesn't return status

How do i export activities related to an account with a specific recordtype?

2019 gold coins to share

What are the implications when matrix's lowest eigenvalue is equal to 0?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Why Does Mama Coco Look Old After Going to the Other World?

Who voices the small round football sized demon in Good Omens?

Math cases align being colored as a table

C++ logging library

Why was this person allowed to become Grand Maester?

Is Lambda Calculus purely syntactic?

How can one's career as a reviewer be ended?

Possible runaway argument using circuitikz

Do people with slow metabolism tend to gain weight (fat) if they stop exercising?

Derivative of a double integral over a circular region

Section numbering in binary

Reference to understand the notation of orbital charts

How can I remove material from this wood beam?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

What STL algorithm can determine if exactly one item in a container satisfies a predicate?

Should I put programming books I wrote a few years ago on my resume?

Who is "He that flies" in Lord of the Rings?

Please figure out this Pan digital Prince

Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?

Do you need to let the DM know when you are multiclassing?



Arduino wrap or Subclass print() to work with multiple Serial


AES Simplified for Arduino - Having hard time achieving desired resultAdvice for checking integrity of serial char strings?How to read long value sent by Arduino in CHelp with a Memory IssueHow to break out of a loop if it is contained in a functionNeed help with the basics on serial controlWhy is integer-to-string not working in this sketch?Arduino to read from RS232 converter to TTL serial module (updated)47Effects MIDI Library and serial debuggingESP8266 sends data to the website but doesn't return status













2















I am writing an Arduino program that uses Bluetooth on Serial1 to print text to a Bluetooth terninal on an Android phone and also normal Serial to print text to the serial monitor on a laptop. I would like to wrap the Serial.print() and Serial.println() functions so that they work with either or both Serial and Serial1. For example the code below works fine depending on the values of the global variables. But this only works for single chars, but print() and println() can take a very wide variety of datatypes. If I also define overloading functions for int and String types it works fine, but that is a very verbose and maybe fragile solution, it also ignores the optional inputs to the underlying functions. What is the proper way to do this ?



void print(char x) 
if (g_use_Serial)
Serial.print(x);
if (g_use_Serial1)
Serial1.print(x);


void println(char x)
if (g_use_Serial)
Serial.println(x);
if (g_use_Serial1)
Serial1.println(x);










share|improve this question


























    2















    I am writing an Arduino program that uses Bluetooth on Serial1 to print text to a Bluetooth terninal on an Android phone and also normal Serial to print text to the serial monitor on a laptop. I would like to wrap the Serial.print() and Serial.println() functions so that they work with either or both Serial and Serial1. For example the code below works fine depending on the values of the global variables. But this only works for single chars, but print() and println() can take a very wide variety of datatypes. If I also define overloading functions for int and String types it works fine, but that is a very verbose and maybe fragile solution, it also ignores the optional inputs to the underlying functions. What is the proper way to do this ?



    void print(char x) 
    if (g_use_Serial)
    Serial.print(x);
    if (g_use_Serial1)
    Serial1.print(x);


    void println(char x)
    if (g_use_Serial)
    Serial.println(x);
    if (g_use_Serial1)
    Serial1.println(x);










    share|improve this question
























      2












      2








      2








      I am writing an Arduino program that uses Bluetooth on Serial1 to print text to a Bluetooth terninal on an Android phone and also normal Serial to print text to the serial monitor on a laptop. I would like to wrap the Serial.print() and Serial.println() functions so that they work with either or both Serial and Serial1. For example the code below works fine depending on the values of the global variables. But this only works for single chars, but print() and println() can take a very wide variety of datatypes. If I also define overloading functions for int and String types it works fine, but that is a very verbose and maybe fragile solution, it also ignores the optional inputs to the underlying functions. What is the proper way to do this ?



      void print(char x) 
      if (g_use_Serial)
      Serial.print(x);
      if (g_use_Serial1)
      Serial1.print(x);


      void println(char x)
      if (g_use_Serial)
      Serial.println(x);
      if (g_use_Serial1)
      Serial1.println(x);










      share|improve this question














      I am writing an Arduino program that uses Bluetooth on Serial1 to print text to a Bluetooth terninal on an Android phone and also normal Serial to print text to the serial monitor on a laptop. I would like to wrap the Serial.print() and Serial.println() functions so that they work with either or both Serial and Serial1. For example the code below works fine depending on the values of the global variables. But this only works for single chars, but print() and println() can take a very wide variety of datatypes. If I also define overloading functions for int and String types it works fine, but that is a very verbose and maybe fragile solution, it also ignores the optional inputs to the underlying functions. What is the proper way to do this ?



      void print(char x) 
      if (g_use_Serial)
      Serial.print(x);
      if (g_use_Serial1)
      Serial1.print(x);


      void println(char x)
      if (g_use_Serial)
      Serial.println(x);
      if (g_use_Serial1)
      Serial1.println(x);







      arduino-uno serial c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      Hubert BHubert B

      112




      112




















          1 Answer
          1






          active

          oldest

          votes


















          2














          You can create a class derived from Print that forwards its output to
          either or both Serial and Serial1. The only method you need to
          implement for this to work is write(uint8_t):





          class DualPrint : public Print

          public:
          DualPrint() : use_Serial(false), use_Serial1(false)
          virtual size_t write(uint8_t c)
          if (use_Serial) Serial.write(c);
          if (use_Serial1) Serial1.write(c);
          return 1;

          bool use_Serial, use_Serial1;
          out;


          You would use it like this:



          out.use_Serial = true;
          out.println("Printed to Serial only");
          out.use_Serial1 = true;
          out.println("Printed to both Serial and Serial1");
          out.use_Serial = false;
          out.println("Printed to Serial1 only");


          Note that with this approach, unlike yours, printing number will format
          them as text only once, and the underlying Serial and Serial1 will
          only handle the resulting characters.






          share|improve this answer























          • for advanced users I would override availableForWrite() and flush() too

            – Juraj
            23 mins ago












          Your Answer






          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("schematics", function ()
          StackExchange.schematics.init();
          );
          , "cicuitlab");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "540"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f66136%2farduino-wrap-or-subclass-print-to-work-with-multiple-serial%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You can create a class derived from Print that forwards its output to
          either or both Serial and Serial1. The only method you need to
          implement for this to work is write(uint8_t):





          class DualPrint : public Print

          public:
          DualPrint() : use_Serial(false), use_Serial1(false)
          virtual size_t write(uint8_t c)
          if (use_Serial) Serial.write(c);
          if (use_Serial1) Serial1.write(c);
          return 1;

          bool use_Serial, use_Serial1;
          out;


          You would use it like this:



          out.use_Serial = true;
          out.println("Printed to Serial only");
          out.use_Serial1 = true;
          out.println("Printed to both Serial and Serial1");
          out.use_Serial = false;
          out.println("Printed to Serial1 only");


          Note that with this approach, unlike yours, printing number will format
          them as text only once, and the underlying Serial and Serial1 will
          only handle the resulting characters.






          share|improve this answer























          • for advanced users I would override availableForWrite() and flush() too

            – Juraj
            23 mins ago
















          2














          You can create a class derived from Print that forwards its output to
          either or both Serial and Serial1. The only method you need to
          implement for this to work is write(uint8_t):





          class DualPrint : public Print

          public:
          DualPrint() : use_Serial(false), use_Serial1(false)
          virtual size_t write(uint8_t c)
          if (use_Serial) Serial.write(c);
          if (use_Serial1) Serial1.write(c);
          return 1;

          bool use_Serial, use_Serial1;
          out;


          You would use it like this:



          out.use_Serial = true;
          out.println("Printed to Serial only");
          out.use_Serial1 = true;
          out.println("Printed to both Serial and Serial1");
          out.use_Serial = false;
          out.println("Printed to Serial1 only");


          Note that with this approach, unlike yours, printing number will format
          them as text only once, and the underlying Serial and Serial1 will
          only handle the resulting characters.






          share|improve this answer























          • for advanced users I would override availableForWrite() and flush() too

            – Juraj
            23 mins ago














          2












          2








          2







          You can create a class derived from Print that forwards its output to
          either or both Serial and Serial1. The only method you need to
          implement for this to work is write(uint8_t):





          class DualPrint : public Print

          public:
          DualPrint() : use_Serial(false), use_Serial1(false)
          virtual size_t write(uint8_t c)
          if (use_Serial) Serial.write(c);
          if (use_Serial1) Serial1.write(c);
          return 1;

          bool use_Serial, use_Serial1;
          out;


          You would use it like this:



          out.use_Serial = true;
          out.println("Printed to Serial only");
          out.use_Serial1 = true;
          out.println("Printed to both Serial and Serial1");
          out.use_Serial = false;
          out.println("Printed to Serial1 only");


          Note that with this approach, unlike yours, printing number will format
          them as text only once, and the underlying Serial and Serial1 will
          only handle the resulting characters.






          share|improve this answer













          You can create a class derived from Print that forwards its output to
          either or both Serial and Serial1. The only method you need to
          implement for this to work is write(uint8_t):





          class DualPrint : public Print

          public:
          DualPrint() : use_Serial(false), use_Serial1(false)
          virtual size_t write(uint8_t c)
          if (use_Serial) Serial.write(c);
          if (use_Serial1) Serial1.write(c);
          return 1;

          bool use_Serial, use_Serial1;
          out;


          You would use it like this:



          out.use_Serial = true;
          out.println("Printed to Serial only");
          out.use_Serial1 = true;
          out.println("Printed to both Serial and Serial1");
          out.use_Serial = false;
          out.println("Printed to Serial1 only");


          Note that with this approach, unlike yours, printing number will format
          them as text only once, and the underlying Serial and Serial1 will
          only handle the resulting characters.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          Edgar BonetEdgar Bonet

          25.6k22546




          25.6k22546












          • for advanced users I would override availableForWrite() and flush() too

            – Juraj
            23 mins ago


















          • for advanced users I would override availableForWrite() and flush() too

            – Juraj
            23 mins ago

















          for advanced users I would override availableForWrite() and flush() too

          – Juraj
          23 mins ago






          for advanced users I would override availableForWrite() and flush() too

          – Juraj
          23 mins ago


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Arduino Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f66136%2farduino-wrap-or-subclass-print-to-work-with-multiple-serial%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її