Serial or Serial1?

WMXZ

Well-known member
This is really a comment to Paul.

On multiple occasions Paul pointed out the difference between Serial and Serial1. The first one using USB the second one RX1,TX1 (right?).

So far so good.
In fact the Teensy3/core has a file serial1.c, BUT, inside the functions are serial_begin, etc (without the 1) and the usb_serial file has the functions with prefix 'usb_'.

While, I have not difficulties to remember the prefix 'usb_serial_available', when I wanted to use USB and 'serial_available' (without the 1), when I wanted to use serial1, or 'serial2_avalable' (with the 2) when using serial2, it is somewhat inconsistent.

I do not know, if backward compatibility forbids the addition of '1' inside the serial1.c file, but sometimes the notation should be consistent.

BTW there is no 'HardwareSerial1' but only a 'HardwareSerial' class (without the 1). so it is not only a 'C' question.

Walter
 
Yeah, much of that is leftover cruft that should probably be deleted.

On Teensy 1.0, about 6 years ago, I started with C code first, using "usb_". Arduino support came about half way through Teensy 1.0's short life. For Teensy 2.0, I ended up moving most development to the Arduino side. The C code languished.

For Teensy 3.0, I decided a better long-term approach would be a single unified code base, where the Arduino C++ classes are mostly thin wrappers around C-only functions. I tried to preserve the old C-only naming conventions, from 6 years ago when starting Teensy 1.0. That seemed like a pretty good idea a couple years ago, but since then, I've heard of very few people using the C-only APIs. That might be due to a lack of documentation on the website and a non-Arduino download made specifically for people who want to code in C without Arduino.

On the Arduino side, I believe things are more consistent. The one exception is class HardwareSerial rather than class HardwareSerial1. Perhaps HardwareSerial should be a pure virtual class, and HardwareSerial1 should inherit from it? I can tell you, the HardwareSerial class (without a number) is absolutely essential for compatibility with some libraries. Things could probably be cleaner and more consistent within the code, but if it's merely internal details that aren't seen by people using Arduino, only seen by people reading the core library source, my general feeling goes something like "ain't broke, don't fix".

Well, the one thing I have considered in the hardware serial code is a private copy constructor. Maybe. You can't assign one serial object to another, but the C compiler lets you try. That subtle point has come up a few times....

At the moment, in the absence of people reporting bugs blocking projects from working, this stuff is really low priority for me. But I've moved this thread to the bugs and suggestions forum, so it'll be around for discussion and reference.
 
Perhaps HardwareSerial should be a pure virtual class, and HardwareSerial1 should inherit from it?

indeed, yes.

Same for future where your MCU may have 3 ADCs, 2-3 SPIs, etc.
 
Back
Top