Go Down

Topic: pass a reference(or pointer) of a Serial object to the function() (Read 6766 times) 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.

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);

The art of getting good answers lies in asking good questions.

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.

flodis

In case you want to use a generic reference that works with both the USB serial port 'Serial' and any of the hardware serial ports 'Serial1', 'Serial2' .. you can reference the port as by its ancestor class 'Stream'.

Like in this simple method that echoes back any characters received on the port:


Code: [Select]
void echoport (Stream &port)
{
    while (port.available())
    {
        port.print(port.read());
    }

}



and call like this for each serial port of interest:


Code: [Select]
echoport (Serial);
echoport (Serial1);
   

joaquin0

Can the Stream class serve to pass a reference to a SoftwareSerial instance?
in other words; can one replace
Code: [Select]

void echoport (SoftwareSerial &port)
{
    while (port.available())
    {
        port.print(port.read());
    }

}

with
Code: [Select]

void echoport (Stream &port)
{
    while (port.available())
    {
        port.print(port.read());
    }

}

PaulS

Quote
in other words; can one replace
Yes, as long as you don't expect it to work for all Streams.

The first code will NOT work. You can not read from/write to a SoftwareSerial instance at the same time.
The art of getting good answers lies in asking good questions.

-dev

If this is what you want to do:

Code: [Select]
   while (port.available())
    {
        port.print(port.read());
    }

Then, like PaulS said, SoftwareSerial will not work.  SoftwareSerial is very inefficient, because it disables interrupts for long periods of time, when a character is sent or received.  Disabling interrupts for 1ms is an eternity for a 16MHz Arduino.  It can't do anything else but wait for the character to finish.  It could have executed 10,000 instructions during that time.

Both AltSoftSerial and NeoSWSerial are much more efficient, and they can send and receive at the same time.  AltSoftSerial must be used on 2 specific pins (8 & 9 on an UNO).  NeoSWSerial can use any 2 pins, but only at 9600, 19200 or 38400.

Cheers,
/dev

Go Up