Go Down

Topic: pass a reference(or pointer) of a Serial object to the function() (Read 1 time) previous topic - next topic

astrofrostbyte

Mar 23, 2013, 12:00 pm Last Edit: Mar 23, 2013, 01:36 pm by astrofrostbyte Reason: 1
I want to pass a reference(or pointer) to a Serial object to the function SerialManager().
I can not figure out the type declarations, possible reference http://playground.arduino.cc/Code/Pointer
I think the code below outlines what I would like to do:
Code: [Select]
unsigned long mywait ;
void setup()
{ Serial.begin(115200);
 Serial1.begin(9600);
 Serial2.begin(9600);
 Serial3.begin(9600);  
 mywait = millis();
}
//---------------------------------
void SerialManager(HardwareSerial* port )
{ if ( port->available() )
 { Serial.write( port->read() );  // etc...  do stuff
 }
}
//---------------------------------
void loop()
{
 SerialManager( &Serial1 );
 SerialManager( &Serial2 );
 SerialManager( &Serial3 );
 
 if ( mywait <= millis() )
 { mywait = millis() + 500;
   Serial1.write("Iam1"); // TX1 to Pin RX2
   Serial2.write("Iam2"); // TX2 to Pin RX3
   Serial3.write("Iam3"); // TX3 to Pin RX1
 }
}


Code: [Select]
unsigned long mywait ;
void setup()
{ Serial.begin(115200);
 Serial1.begin(9600);
 Serial2.begin(9600);
 Serial3.begin(9600);  
 mywait = millis();
}
//---------------------------------
void SerialManager(HardwareSerial &port )
{ if ( port.available() )
 { Serial.write( port.read() );  // etc...  do stuff
 }
}
//---------------------------------
void loop()
{
 SerialManager( Serial1 );
 SerialManager( Serial2 );
 SerialManager( Serial3 );
 
 if ( mywait <= millis() )
 { mywait = millis() + 500;
   Serial1.write("Iam1"); // TX1 to Pin RX2
   Serial2.write("Iam2"); // TX2 to Pin RX3
   Serial3.write("Iam3"); // TX3 to Pin RX1
 }
}


note: using Arduino Due, but think should work on mega also for nr of serial ports.
thanks...

UPDATED , Both Code examples Are working and tested on Arduino Due.
Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

PaulS

First thing you need to decide is if you want to pass a pointer or a reference. That determines how to declare the function and how to call it.

Reference:
Code: [Select]
void func(HardwareSerial &refSer)
{
   refSer.print("ref to ser");
}

func(Serial);


Pointer:
Code: [Select]
void func(HardwareSerial *ptrSer)
{
   ptrSer->print("ptr to see");
}

func(&Serial);


astrofrostbyte

#2
Mar 23, 2013, 01:28 pm Last Edit: Mar 23, 2013, 01:33 pm by astrofrostbyte Reason: 1
Thanks,  I think I was mixing those up,  I've updated the top post with working examples , tested on an Arduino Due.
Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy