General Serial Object

Hey all,

I am in the process of writing a sketch that utilizes a serial LCD from SparkFun. I found the SLCD library and really like what I see. I have done a bit of ugly hacking to make it also work with the SoftwareSerial library. Since SoftwareSerial is now a standard library (included in Arduino 11 at least), I think it would be desirable to develop a mechanism to use either HardwareSerial or SoftwareSerial in a straightforward way.

The idea I currently have is to create a SerialObject class with virtual functions for all the methods that both HardwareSerial and SoftwareSerial have in common (begin(), print(), read(), etc.) and simply have both HardwareSerial and SoftwareSerial inherit from this base SerialObject class. This way, you could enable your library that needs serial communications to choose between the hardware serial port, or any pair of digital i/o pins available.

I am curious as to the way to go about this possibility being added to the codebase, or at the very least, the recommended way of modifying things in my own code snapshot to achieve this result. If HardwareSerial was an external (but included) library like SoftwareSerial, I would just make a header file in each of the directories called SerialObject.h and create said base class and modify HardwareSerial.h and SoftwareSerial.h to inherit. However, since HardwareSerial is in the core/wiring portion of the codebase, I figured I would post here and ask what would be best given the current layout of the headers.

I am looking forward to hear any comments, appropriate ways to pursue the above being added to the code base, and any ways other than the above mentioned to achieve the desired functionality without breaking backwards compatibility.



Something very similar to this is happening in Arduino 0012. I'm adding a Print class with print() and println() methods that call to a virtual write() method that's overridden by the particular implementation (HardwareSerial, SoftwareSerial, LiquidCrystal, etc.). See: Print.h and Print.cpp. Suggestions welcome.


That sounds very cool. After checking the latest HardwareSerial.h header, it would be awesome if the begin() and read() were also made virtual. SoftwareSerial could inherit from HardwareSerial and just overload write(), begin(), and read() and what I want accomplished is done. Hallelujah! :stuck_out_tongue:

Before I get too excited, are the above changes possible (i.e. are people willing to do them)?