Auto Serial Port Type Selection

Here is an idea I had that might be a long shot, but would be extremely useful.

What I would like to do is create a class, let’s call it SerialPort, such that when you initialize it:

SerialPort(RX, TX)

then it checks to see if the RX and TX pins are available for hardware serial. If not, then it uses software serial.

So if I did Device = SerialPort(27, 26) on a Teensy 3.5, then Device.println() will happen over HardwareSerial1 with the alternate pin option.

But if I did Device = SerialPort(27, 26) on an arduino mega, then Device.println() would use SoftwareSerial.

Or perhaps there’s a way to do it as a macro since it doesn’t necessarily have to happen in runtime, but this would be a useful feature. Does this sound at all possible, or would it be a waste of time and/or flash?

I do this sort of thing to automagically choose a serial port for GPS devices (here), a slightly different application than you are asking for, but it might give you some ideas about how to use defined symbols on different platforms to choose some things.

Mapping pins to UARTs is a different problem. I'm not aware of any Arduino core files that identify these pins, except as HardwareSerial instances. It does not use pin numbers for those instances, it uses the registers. There is no mapping from UART registers to pin numbers, either (that I am aware of).

Jonathanese:
Here is an idea I had that might be a long shot, but would be extremely useful.

Seems to me it would only be useful for those people who are too lazy to read the documentation for the Arduino board they are using.

Also, I don't think I would like to find my program using the crap SoftwareSerial when I had intended it (however foolishly) to use HardwareSerial.

...R