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
What differences exist between adamantine and adamantite in all editions of D&D?
The origin of the Russian proverb about two hares
If there's something that implicates the president why is there then a national security issue? (John Dowd)
Why the output signal of my amplifier is heavily distorted
Why Does Mama Coco Look Old After Going to the Other World?
Ability To Change Root User Password (Vulnerability?)
Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?
How can I remove material from this wood beam?
tabular: caption and align problem
How can one's career as a reviewer be ended?
A map of non-pathological topology?
A word that means "blending into a community too much"
2019 gold coins to share
How do i export activities related to an account with a specific recordtype?
Possible runaway argument using circuitikz
How to write a convincing religious myth?
Why was this person allowed to become Grand Maester?
Who won a Game of Bar Dice?
Derivative of a double integral over a circular region
Write a function that checks if a string starts with or contains something
Do you need to let the DM know when you are multiclassing?
Printing Pascal’s triangle for n number of rows in Python
If I leave the US through an airport, do I have to return through the same airport?
I've been given a project I can't complete, what should I do?
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
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
add a comment |
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
add a comment |
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
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
arduino-uno serial c
asked 8 hours ago
Hubert BHubert B
112
112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
for advanced users I would overrideavailableForWrite()
andflush()
too
– Juraj
28 mins ago
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
for advanced users I would overrideavailableForWrite()
andflush()
too
– Juraj
28 mins ago
add a comment |
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.
for advanced users I would overrideavailableForWrite()
andflush()
too
– Juraj
28 mins ago
add a comment |
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.
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.
answered 7 hours ago


Edgar BonetEdgar Bonet
25.6k22546
25.6k22546
for advanced users I would overrideavailableForWrite()
andflush()
too
– Juraj
28 mins ago
add a comment |
for advanced users I would overrideavailableForWrite()
andflush()
too
– Juraj
28 mins ago
for advanced users I would override
availableForWrite()
and flush()
too– Juraj
28 mins ago
for advanced users I would override
availableForWrite()
and flush()
too– Juraj
28 mins ago
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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