Switching from one serial port to another.

Hi,

I have a number of projects that all use GPS, and all use the same GPS library.

Currently the library is hard coded to Serial1.

My latest project requires that I use the interrupt on pin 18 or 19, so I need to move the GPS module to port 2 and edit the GPS library to use Serial2.

I can do a simple global replace of "Serial1" with "Serial2" which would solve my immediate problems, but then my existing projects (which have the GPS module physically connected to port1) won't work until I reverse the changes.

So, what's the simplest way of modifying my GPS library so that the port number is configurable in software?

Thanks

So, what's the simplest way of modifying my GPS library so that the port number is configurable in software?

Make it take a reference to the HardwareSerial instance to use.

Make it take a reference to the HardwareSerial instance to use.

Sounds like a good idea, are you able to provide guidance on how to do this? (maybe point me in the direction of a suitable tutorial - or some examples?)

I have discovered a pair of files HardwareSerial.cpp and HardwareSerial.h which I guess are what control the serial ports, but I have never explicitly included them in any project/library before.

I have discovered a pair of files HardwareSerial.cpp and HardwareSerial.h which I guess are what control the serial ports, but I have never explicitly included them in any project/library before.

The header file is included in Arduino.h, so you don't need to.

are you able to provide guidance on how to do this?

Sure. Add an argument to the constructor of type HardwareSerial &. Then, add a member, of the same type to the class. In the constructor, assign the member the value passed in. Then, in the rest of the code, use the member variable, not Serial1.

Thanks PaulS, nearly there I think.....

Add an argument to the constructor of type HardwareSerial &

My constructor now looks like this.....

MiniGPS::MiniGPS(HardwareSerial *serialX)
{
}

and the library is now invoked from the top-level project like this....

MiniGPS gps1(&Serial1);

Then, add a member, of the same type to the class.

Ok, I've added this line to the 'private' section of MiniGPS.h

HardwareSerial *_portX;

In the constructor, assign the member the value passed in

The constructor now looks like this...

MiniGPS::MiniGPS(HardwareSerial *serialX)
{

  _portX = serialX;
}

Then, in the rest of the code, use the member variable,

I have done a global replace in MiniGPS.cpp to replace "Serial1." with "_portX->" So where I did have lines like this....

Serial1.begin(baud);
Serial1.flush();
Serial1.available();
Serial1.read()

I've replaced them with....

_portX->begin(baud);
_portX->flush();
_portX->available();
_portX->read()

This compiles, uploads, and appears to work.

Can you confirm I've done what you've suggested - I'm aware there's a difference between 'working' and 'working properly'

Thanks

Can you confirm I've done what you've suggested

I can confirm that you haven't. What you've done is what I would have done, though.

HardwareSerial * (what you actually used) is not the same as HardwareSerial & (that I recommended).

The advantage of using references is that the . notation to invoke the members can still be used. But, for me the pointer notation feels more natural, so that's what how I'd have done it.

HardwareSerial * (what you actually used) is not the same as HardwareSerial & (that I recommended).

I did what I normally do when working with pointers. I started with a random sprinkling of "&"s but couldn't get that to work, and changed it to a random sprinkling of "*"s instead.

The problem with pointers / references / dereferences etc etc is that I can understand them for about 5 minutes after I read a tutorial on them, but as time progresses my understanding descends back into the murkiness of my memory.

The advantage of using references is that the . notation to invoke the members can still be used.

I tried ampersands and dots first, but got various error messages, then I switched to astrixes and -> instead and it started working.

P.S. don't you every unplug yourself from this forum?

Cheers