naikin:
Now this might have been asked before but because of the numerous update I want to double check. I am writing a custom library for communication between Dynamixel xl320 servos and Arduino Mega. I want to use all instances Serial1, Serial2 and Serial3 corresponding to the hardware ports on the board.Where are these defined? Which headers I need to include in my library? More importantly what is the name of their class? I want to be able to declare a pointer or reference to each Serial so that I can make my library work for any Serial port. To be able to do that, though, I need to specify a type for the pointer.
If I'm understanding you correctly, this may be what you need:
static Stream *_stream_ptr; // global stream pointer
void openit (Stream &str)
{
_stream_ptr = &str; // equate "stream pointer" to serial device
}
void sendit (const char *string)
{
for (int x = 0; x < strlen (string); x++) {
_stream_ptr->write (string[x]); // same as Serial.write()
}
}
void setup (void)
{
Serial.begin (115200);
Serial1.begin (9600);
openit (Serial); // setup stream pointer
sendit ("Hello there\n"); // same thing as Serial.print ("Hello there\n")
openit (Serial1); // now we point to Serial1
sendit ("Hello there serial port 1\n"); // same code now talked to Serial1
}
void loop (void)
{
// nothing
}
If this isn't what you mean, let me know (with a more detailed description) and I'll try to help.
(edit to add): For serial reading, do it the same as if you were using "Serial" directly... that is something like
if (_stream_ptr->available()) {
return _stream_ptr->read();
}
Also, look at this little library I wrote that uses the same technique... may be of help: