Go Down

Topic: Writing a new library for Serial (Read 983 times) previous topic - next topic

Kevin77

Hey all,
I have looked everywhere on the net but I can't seem to find it and I can't imagine I am the only one looking for this, so I must be looking in the wrong places :~

I probably have to write my own library but I am running into some difficulties with that, but before I go into those problems I thought maybe there is a simpler way to do this...

Code: [Select]

#include <MySerial.h>

void setup()
{
  for (byte i=0; i<3 ; t++)
  {
    MySerial.begin(i,9600); 
    // where i=0 would initiate Serial ; i=1 would initiate Serial1 etc...
  }

...
in my main loop function I then want to be able to things like :
MySerial.print(1,"something");
which would be the equivalent of Serial1.print("something")

Am I missing some information and does something like this already exists?

Thanks for your help.



PaulS

Quote
Am I missing some information and does something like this already exists?

Yes, you are. You are missing a description of how your serial class will differ from the existing hardware serial class.
The art of getting good answers lies in asking good questions.

Kevin77

Hi PaulS,
Sorry if I was not being clear enough. I need the library for my Mega as it is talking to four other arduino's.

I'll try to clarify a bit : the only difference between the original one and MySerial would be that I could have my script  decide which port it would use with an extra variable passed via the separate functions (or via Serial itself?)
It would inherit all the functions, available,begin, end, find, print, println, etc but with an added variable for the port (with port I mean Serial, Serial1,Serial2,Serial3) As far as I know, you have to hard code that into your script now.
Say I want to write some text to all four Serials.
Now I have to do this :

Code: [Select]
Serial.println("hello");
Serial1.println("hello");
Serial2.println("hello");
Serial3.println("hello");


I would like to be able to do this instead to achieve the same result:

Code: [Select]
for (byte port=0 ; port<4 ; port ++ )
{
  MySerial.println(port,"hello");
}


Thanks for the quick reply...

PaulS

Quote
I would like to be able to do this instead to achieve the same result:

Writing a class to do what a function should do is a waste.

But, if you persist, I see no reason why you can't accomplish what you want. What have you tried? What problems did you have?
The art of getting good answers lies in asking good questions.

Kevin77

Hi PaulS,
Man you're fast!
Do I understand you correctly? Would you put everything in conditional functions then? And just pass the strings you would want to send and receive to all four Serials by reference (pointers)?

KeithRB

You could just put all the class variables in an array:
Code: [Select]

serial[0].print("This is zero!");
serial[1].print("This is one!!");

Kevin77

That would be perfect!
Noob alert, how do you do that in code?
Of what type is Serial?


PaulS

Quote
Of what type is Serial?

Serial is an instance of the HardwareSerial class.

You can't create new instance of that class. So, you need an array of pointers to HardwareSerial instances:

Code: [Select]
HardwareSerial *serial[4];

Code: [Select]
serial[0] = &Serial;
serial[1] =  &Serial1;

etc.

The, you use:
Code: [Select]
for(byte i=0; i<4; i++)
{
  serial[i]->print(i);
}
The art of getting good answers lies in asking good questions.

Kevin77


Go Up