Passing serial handle to function

Status
Not open for further replies.

FFMan

Active member
I'm trying to write a serial function to talk AT commands to a device.

Naturally I want to pass the function the handle of the serial port in use, so the serial port isn't hard coded.

I'll aim to use hardware ports so not so concerned about softwareserial at this stage, but i can't work out how to do this.

Any help much appreciated. New to Arduino and C though I've been programming in other languages for quite a while.

thanks
 
Is this for a Teensy? Something like a Teensy 3.x?

If hardware serialport, I would do something like:

Code:
void  myFunction(HardwareSerial *pserial) {
    pserial->println("My Function called");
}

void loop() {
    myFunction(&Serial1);
...
}

You could also probably pass by reference instead of pointers...

Note: If I wish to make it more generic. I would use use Stream class instead of HardwareSerial, but then you don't have access to some of the other Serial functions, like available(), or ...
 
i'm prototyping on an LC but the final project will need a 3.6 for speed and number of pins.

i understand i could use stream for hardware or software serial but i intend to avoid software serial to cpu loading.
 
So how would i declare the serial channel in my init routine

so in other words this :-

myFunction(&Serial1);

gets replaced with

myFunction(gpsChan)

and somewhere in the init section Serial1 is assiged to gpsChan. How do i assign Serial1 to gpschan ?
 
I tackled it like this, it compiles but not testedt it yes

Code:
HardwareSerial    *gpsPort

gpsPort = &Serial1

gpsPort->begin(9600)

is that going to work ?
 
There are many ways to do this, and it all depends on your needs...

Example, If I just want to make the code more configurable and not really change much, I might do something like:

Code:
#define GPSPORT Serial1
Then everywhere that I wish to talk to the GPS, is would simply do: GPSPORT.begin(9600); GPSPORT.println("some text");

Other times, if it is just a set of functions, you are wishing to do: I would define them how I mentioned above, and then either
call them like: myFunction(&Serial1); or again could have a #define like above and do myFunction(&GPSPORT);

or maybe I define a class that uses this, and either pass a pointer/reference to the Serial object as part of the constructor or part of an init.
Something like:
Code:
class GPSClass {
  public:
    void init(HardwareSerial &gpsPort);
    void someMethod();

  private:
    _HardwareSerial *_pgpsPort;
};
void GPSClass::init(HardwareSerial &gpsPort) {
    _pgpsPort = &gpsPort;
   // DO stuff with it. like
  _pgpsPort->begin(9600);
}

void GPSClass::someMethod() {
  _pgpsPort->println("Some text");

The in main code you could do something like:
Code:
GPSClass myGPS();

void setup() {
  myGPS.init(Serial1);
...
}

void loop() {
  myGPS.someMethod();
}

Warning typed on fly so probably issues! Note this one I did pass by reference instead of pointers. Could do pointers instead. Change init define to a * instead of & and the assignment would be direct, but the call to init would need to add & before Serial1 as to pass the address.....

Again lots of different ways
 
Status
Not open for further replies.
Back
Top