Pages: [1]   Go Down
Author Topic: Make Serial.Print() and Client.Print() interchangeable  (Read 456 times)
0 Members and 1 Guest are viewing this topic.
Melbourne
Offline Offline
Jr. Member
**
Karma: 2
Posts: 83
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

To give some context as to what I'm doing (if anyone's interested I'll publish all the code), I am writing a command line server for Arduino. This basically works as <instruction> <params> type thing and designed to establish a generic method for passing info in and out of a running arduino primarily via telnet but should work under serial too for debgugging.

I have this largely working for either Serial and over the network however what I would LOVE to do in order to either thin down my code or not have to maintain two libraries is to make Serial.print / println and Client.print / println totally interchangeable.

What I'd like to do is when I start up my CommandServer object it should take in a parameter which determines whether to use Serial.print() or Client.Print() (and ideally a reference to either one) and then in all of my functions I use this reference.

Serial and Client print functions are identical in function so for what I'm doing they are totally interchangeable so I'd like to be able to just use the defined one in all the functions which will then make that a lot more maintainable.

If anyone can give me any pointers or something in this respect I'd be greatly appreciated.

Cheers
ajfisher


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial is an instance of the HardwareSerial class, which derives from Stream. Client is also derived from Serial. So, by making the class take an instance of Stream as input, you could pass it any instance that derives from Stream, too.

Oh, and Stream derives from Print.
Logged

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If all you need is a one-time flag to decide which print to use, how about a flag and then a function to wrap around a print():

Code:
int useClient=0;
void setup()
{
  // pick your destination
  useClient=1;
}
void Print2(char *text)
{
  if (useClient)
    Client.print(text);
  else
    Serial.print(text);
}
void loop()
{
  Print2("output"); // print to either depending on useClient
}

Of course, if you need a lot more shared functions, the inheritance solution already mentioned will make your code cleaner
Logged

Melbourne
Offline Offline
Jr. Member
**
Karma: 2
Posts: 83
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS thanks for that - I was trying to figure out what the common parent object was. That should do nicely.

@David - thanks for that, yep that's pretty much what I was doing which works nicely but adds some weight to the whole thing.
Logged

Pages: [1]   Go Up
Jump to: