Jump to content
43oh

[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

 

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.

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...

Link to post
Share on other sites
  • 2 months later...
  • 1 year later...

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.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...