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
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
23 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
23 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
23 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
23 mins ago
add a comment |
for advanced users I would overrideavailableForWrite()
andflush()
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
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