Jump to content
Sign in to follow this  
Rei Vilo

[Energia Library] Very Basic Software Serial TX Only Library

Recommended Posts

I was looking for a software serial library but with TX only.

 

Using the Saleae logic analyser, I recorded the hardware TX signals and then worked on obtaining the same signals with the library.

 

By very basic, I mean one single speed 9600 and one single configuration is 8N1.

 

The SerialTX class is derived from Stream and thus benefits from all the print() functions:

class SerialTX : public Stream

The most critical part is tweaking the delays 

#define DELAY_START_BIT 90 ///< start bit
#define DELAY_SEND_BIT 88 ///< data bits
#define DELAY_STOP_BIT 88 ///< stop bit

Using it is easy:

#include "serialTX.h"
SerialTX myTX(P2_0); // Serial TX on pin P2_0

void setup() 
{
    myTX.begin();

    myTX.print("Hello!");
}

void loop() 
{
}

Enjoy :smile:

 

 

Updated library release 1.08

SerialTX_108.zip

 

Share this post


Link to post
Share on other sites

Nice and elegant.

read() should return -1 as -1 is defined as the "no characters available" constant. Same for peek()

I don't know if it's necessary to override flush(), but it should return a bool according to Stream, Serial on the other hand claims it returns void. This is kind of confusing.

I like the initializer list for constructors, but this is more of a preference I guess

// Plain implementation
SerialTX::SerialTX(uint8_t pin)
{
    _pin = pin;
}

// Initializer list
SerialTX::SerialTX(uint8_t pin)
: _pin(pin)
{
}

Which would also allow for _pin to be const uint8_t, which in turn can allow compiler optimisations.

I'd put the DELAY_..._BIT defines in the cpp file; the h file should only expose that which is of interest to the user.

Share this post


Link to post
Share on other sites

Thank you for your comments and the correct value -1 for read() and peek().

 

The initialiser list trick doesn't seem to reduce memory size.

 

I've updated the library accordingly. See new release 1.07 on the initial post.

 

Next goal is a software I2C library...

Share this post


Link to post
Share on other sites

I don't say using initializer lists will reduce memory (though in combination with const uint8_t _pin; it could save you 1 or 2 bytes of ram), but it could reduce memory usage on higher optimisation levels.

Share this post


Link to post
Share on other sites

You could use <template> for say an 8E1 or a 7O2 version, but using template for e.g. the pin would not save you at best two bytes of memory, while two instances of SerialTX would double used memory in both ram and flash. I suggest strongly against using template for things that can very well be put in parameters without much added effort.

Maybe using the before suggested initializer list and const private member in combination with a declaration like

const SerialTX myserial(8);

could do the trick, though I doubt that entire objects can be put in flash by the compiler, but you never know...

Share this post


Link to post
Share on other sites

There's a case sensitivity error that triggers a compiler error: In serialTX.cpp it should be #include "serialTX.h" instead of #include "SerialTX.h" 

Share this post


Link to post
Share on other sites

There's a case sensitivity error that triggers a compiler error: In serialTX.cpp it should be #include "serialTX.h" instead of #include "SerialTX.h" 

 

Thank for noticing me. Please find updated library release 1.08 on the leading post.

Share this post


Link to post
Share on other sites

Hi Rei,

 

I just tested your library, I had to tune delays :

 

#define DELAY_START_BIT 95///< start bit
#define DELAY_SEND_BIT 93 ///< data bits
#define DELAY_STOP_BIT 93 ///< stop bit */
 
Maybe because digitalWrite() is a little bit faster since you wrote this.
 
But note that my timing are just base on my experiment, I didn't any measure, just experiment different timings. This ones worked.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×