Pages: [1]   Go Down
Author Topic: pass a reference(or pointer) of a Serial object to the function()  (Read 749 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Full Member
***
Karma: 6
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
« Last Edit: March 23, 2013, 07:36:59 am by astrofrostbyte » Logged

Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49412
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void func(HardwareSerial &refSer)
{
   refSer.print("ref to ser");
}

func(Serial);

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

func(&Serial);
Logged

Belgium
Offline Offline
Full Member
***
Karma: 6
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks,  I think I was mixing those up,  I've updated the top post with working examples , tested on an Arduino Due.
« Last Edit: March 23, 2013, 07:33:30 am by astrofrostbyte » Logged

Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Pages: [1]   Go Up
Jump to: